1. Basic concepts

  • What is Redis single threaded (core functions on single threads, not all functions)

    • Redis network IO and key pair reads and writes are done by a single thread (redis core service)

    • Additional redis functions are performed by additional threads

      • persistence
      • Asynchronous delete
      • Cluster Data Synchronization

2. Why does Redis use single thread

  • Overhead of multithreading

    • Using multiple threads can increase system throughput (per request) and increase system scalability

    • Increasing the number of threads infinitely, causing throughput to decline

      • Shared resources, such as shared data structures, are accessed simultaneously by multiple threads
      • Performance is sacrificed to ensure thread safety
      • Coarse-grained locks cause all serials, and the throughput of the system increases with the number of threads
  • Therefore, to save the management of concurrent resources, Redis uses a single thread to ensure that all operations are serialized

\

3. Why is single-threaded Redis fast

  • Redis does most of its operations in memory + efficient data structures
  • Redis uses multiplexing mechanism to handle a large number of client requests in the network and achieve high throughput

4.socket

  • Socket communication process (network IO processing + key/value pair reading and writing + network IO processing)

    • SimpleKV to process a Get request
    • Need to listen for client requests (bind/listen)
    • Establish a connection with the client (code) (Accept)
    • Read request from socket (RECV)
    • Parse client send request (parse)
    • Read key value data based on request type (GET)
    • Write data back to the socket.
  • Potential choke points

    • Accept () blocks \ until the connection is successfully established

    • Recv () blocks \ as it reads data from the client

  • The non-blocking mode of the socket
  • Ensure that Redis threads do not wait at choke points, as in the basic IO model, nor do they cause Redis to fail to process connection requests or data that actually arrive
  • IO multiplexing mechanism in Linux \

5. Multiplexing

  • The IO multiplexing mechanism in Linux means that one thread processes multiple IO streams, select/poll

  • Under a single thread, there is a simultaneous listening for multiple sockets and connected sockets

  • The specific implementation

    • FD is multiple sockets

    • Redis uses the epoll mechanism to let the kernel listen for sockets

    • Redis can connect to multiple clients and process requests to improve concurrency

    • Select /epoll provides an event-based callback mechanism that calls handlers for different events

      • The event is first put into the event queue, so that there is no need to tell whether the request actually happened, thus avoiding wasting CPU resources
      • Perform the action in response to the corresponding event

6. Summary

  • Is Redis really single-threaded

    • A thread \ is used for network IO and data read and write operations
  • Why single thread

    • Avoid concurrency control problems in multi-threaded development
    • The IO model of multiplexing is closely related \
  • Why is a single thread so fast

\