Before the interview was asked by the interviewer a question, Redis is multithreaded or single-threaded? Vaguely remember Redis as a single-threaded, and its deeper technical principles are completely ignorant force. So this article is intended to explain why Redis is a single thread.
1. Basic Principles
Multi-Channel I/O multiplexing technology allows a single thread to efficiently handle multiple connection requests (minimizing network IO time consumption)
(1) Why not use multi-process or multi-threaded processing?
Multithreading may involve locking
Multithreaded processing involves thread switching and consumes CPU
(2) The disadvantage of single thread processing?
Multi-core CPU performance is not possible, but can be improved by opening multiple Redis instances on a single machine
2. Is there a thread safety problem with Redis?
Redis uses a thread-blocking approach that closes a task to a thread, naturally avoiding thread-safety issues, but still requires a lock for a composite operation that relies on multiple redis operations, and is likely to be a distributed lock
3. What is multi-channel I/O multiplexing (epoll)
(1) The network IO is implemented through the socket, the server continues to listen on a port, the client through the socket (ip+ Port) to establish a connection to the server (serversocket.accept), after a successful connection, you can use the encapsulated InputStream and OutputStream in the socket for IO interaction. For each client, the server creates a new thread dedicated to handling
(2) By default, network IO is blocking mode, where the server thread is in a "blocked" state until data arrives, and the server thread is automatically woken up to proceed with processing. In blocking mode, a thread can handle only one stream of IO events
(3) The following three ways of thinking
(1) Non-blocking "busy polling": polling each stream in a dead loop, if there is an IO event to process, so that a thread can handle multiple streams, but not high efficiency, easy to cause the CPU idling
(2) Select agent (non-differential polling): You can observe the IO events of multiple streams, and if none of the streams have an IO event, the thread goes into a blocking state, and if one or more of the IO events occur, the thread is woken up for processing. But you still have to traverse all the streams to find out which flows need to be processed. If the number of flows is n, the time complexity is O (n)
(3) Epoll proxy: The select agent has a disadvantage that the thread polls all streams after being awakened, or there is an invalid operation. Epoll What stream happens to the I/O event notification processing thread, so the operation of these streams is meaningful and the complexity is reduced to O (1)
Redis has a lot of high concurrency and quick reasons to summarize a few points:
1. Redis is a pure memory database, usually a simple access operation, the thread takes up a lot of time, the time spent mainly on the IO, so read faster.
2. Again, Io,redis uses a non-blocking Io,io multiplexing, using a single-threaded polling descriptor to convert the open, close, read, and write of the database into events, reducing the context switching and contention during thread switching.
3. Redis employs a single-threaded model that guarantees the atomicity of each operation and reduces the context switching and contention of threads.
4. In addition, the data structure has also helped a lot of, redis full use of hash structure, fast reading speed, there are some special data structure, the data storage is optimized, such as compression table, short data compression storage, such as, jump table, using an orderly data structure to speed up the reading speed.
5. Another point is that Redis uses its own event splitter, which is more efficient and uses non-blocking execution in its own way, with a large throughput capacity.
Redis Single Thread Interpretation