A brief introduction to the Redis threading model

The article briefly summarizes the Redis thread model and file event handler.

Redis threading model

Redis has developed its own network event processor based on the Reactor model: this processor is called a file event processor.
It is composed of 4 parts: multiple sockets, IO multiplexing program, file event dispatcher, and event handler. Because the consumption of the file event dispatcher queue is single-threaded, Redis is called the single-threaded model.
Insert picture description here

Message processing

  • File event handlers use I/O multiplexing (multiplexing) procedures to monitor multiple sockets at the same time, and associate different event handlers for the sockets according to the tasks currently performed by the sockets.
  • When the monitored socket is ready to perform connection response (accept), read (read), write (write), close (close) and other operations, the file event corresponding to the operation will be generated. At this time, the file The event handler will call the event handler associated with the socket to handle these events.

A file event is an abstraction of socket operations. Whenever a socket is ready to perform operations such as accept, write, read, and close, a file event will be generated. Because a server usually connects to multiple sockets, multiple file events may appear concurrently.

The I/O multiplexer is responsible for monitoring multiple sockets and sending those sockets that generated the event to the file event dispatcher.

Although multiple file events may appear concurrently, the I/O multiplexing program always pushes all event-generating sockets into a queue, and then through this queue, in order, synchronization, every Send the socket to the file event dispatcher one socket at a time: when the event generated by the previous socket has been processed, the I/O multiplexer will continue to send the next one to the file event dispatcher Socket.

File event handler

The I/O multiplexer can monitor the ae.h/AE_READABLE event and the ae.h/AE_WRITABLE event of multiple sockets. If a socket is both readable and writable, the server will read the socket first, and then write the socket.

The most commonly used servers are the connection response processor, command request processor, and command reply processor that communicate with the client.

Client-server connection event example

When the Redis server is initialized, the program associates the connection response processor with the AE_READABLE event of the server listening socket.

When a client uses the sys/socket.h/connect function to connect to the server to monitor the socket, the socket will generate an AE_READABLE event, trigger the connection response processor to execute, and perform the corresponding socket response operation.

When a client successfully connects to the server through the connection response processor, the server associates the AE_READABLE event of the client socket with the command request processor. When the client sends a command request to the server, the socket will Generate the AE_READABLE event, trigger the command to request the processor to execute, and execute the corresponding socket read operation.

When the server has a command reply that needs to be sent to the client, the server associates the AE_WRITABLE event of the client socket with the command reply processor. When the client is ready to receive the command reply from the server, the AE_WRITABLE event will be generated. , Cause the command to reply to the processor to execute, and execute the corresponding socket write operation.

Three factors of Redis high performance

  1. Pure memory storage
  2. Non-blocking I/O: epoll is used as the realization of IO multiplexing technology, and Redis uses an event processing model internally to convert connections, reads and writes into events, and will not waste too many events on IO.
  3. Single-threaded architecture to avoid consumption caused by thread switching and race conditions
  4. Implemented in C language

Due to the single-threaded architecture, each command needs to be executed quickly, otherwise Redis may be blocked. For operations such as keys, hgetall, sembers, and zrange with high complexity, the impact of data size needs to be considered.

Reference

  • http://redisbook.com/preview/event/file_event.html
  • https://www.jianshu.com/p/8f2fb61097b8

Guess you like

Origin blog.csdn.net/LIZHONGPING00/article/details/115220797