The interview questions
What’s the difference between Redis and Memcached? What is the threading model of Redis? Why can Redis single thread support high concurrency?
Interviewer psychoanalysis
Redis is actually a single-threaded working model. If you don’t know this, you won’t know anything about it when you play Redis.
You may also be asked about the difference between Redis and Memcached, but Memcached was a popular caching solution for Internet companies in the early years, but now it’s mostly Redis, and Memcached is not used by many companies anymore.
Analysis of interview questions
What’s the difference between Redis and Memcached?
Redis supports complex data structures
Redis has more data structures than Memcached and can support richer data operations. If you need a cache that can support more complex structures and operations, Redis is a good choice.
Redis natively supports the cluster mode
In redis3. x, cluster mode is supported. Memcached has no native cluster mode and relies on the client to write data in fragments to the cluster.
The performance comparison
Because Redis uses only one core, while Memcached can use multiple cores, Redis performs better than Memcached at storing small data on average per core. For data over 100K, Memcached performs better than Redis. Redis has recently been optimized for storing big data, but it still lags behind Memcached.
Redis thread model
Redis uses the file event handler internally. This file event handler is single-threaded, so Redis is called the single-threaded model. It uses the IO multiplexing mechanism to monitor multiple sockets at the same time, and pushes the socket that generates the event into the memory queue. The event dispatcher selects the corresponding event processor for processing according to the event type on the socket.
The structure of the file event handler consists of four parts:
- Multiple socket
- IO multiplexing procedures
- File event dispatcher
- Event handler (connection reply handler, command request handler, command reply handler)
Multiple sockets may concurrently produce different operations, each corresponding to a different file event, but the IO multiplexer listens for multiple sockets, queues the socket that generated the event, and the event dispatcher retrieves the socket from the queue one at a time. According to the event type of the socket, it is sent to the corresponding event handler for processing.
Let’s look at a communication process between the client and Redis:
To understand, communication is through socket to complete, do not understand the students can first take a look at socket network programming.
First, when the Redis server process initializes, it associates the AE_READABLE event of the Server socket with the connection reply handler.
The socket01 client requests the Server socket of the Redis process to establish a connection. When the Server socket generates an AE_READABLE event, the IO multiplexer listens for the event generated by the server socket. Push the socket into the queue. The file event dispatcher takes the socket from the queue and gives it to the connection reply handler. The connection reply handler creates a Socket01 that can communicate with the client and associates the AE_READABLE event for that Socket01 with the command request handler.
If the client sends a set key value request, the SOCket01 in Redis will generate an AE_READABLE event, and the IO multiplexer will queue the socket01. At this point, the event dispatcher gets AE_READABLE events from socket01 from the queue. Since AE_READABLE events from Socket01 have already been associated with the command request handler, the event dispatcher hands the event to the command request handler for processing. The socket01 key value command asks the processor to read the socket01 key value and set the key value in its own memory. When the operation is complete, it associates the AE_WRITABLE event of socket01 with the command reply handler.
If the client is ready to receive the result, then Socket01 in Redis generates an AE_WRITABLE event, which is also queued. The event dispatcher finds the associated command reply handler, which injects a result of this operation into socket01. For example, ok, and then disassociate socket01’s AE_WRITABLE event from the command reply handler.
A communication is thus completed. As for the communication process of Redis, we recommend readers to read “Redis Design and Implementation — Huang Jianhong” for systematic study.
Why is the Redis single-threaded model so efficient?
- Pure memory operation.
- The core is IO multiplexing mechanism based on non-blocking.
- C implementation, in general, C implementation of the program “closer” to the operating system, relatively faster execution.
- On the contrary, single thread avoids the frequent context switch problem of multithreading and prevents the competition problem that multithreading may cause.
Redis 6.0 began to introduce multithreading
Attention! Versions of Redis after 6.0 abandoned the single-threaded model, and Redis, which used to run with a single thread, began to selectively use a multi-threaded model.
After emphasizing the efficiency of Redis single-threaded model, why introduce multi-threading now? This shows that there are some aspects of Redis where single threading is no longer an advantage. Because Read/Write system calls on Read/Write networks consume most of the CPU time during Redis execution, making network reads and writes multithreaded can greatly improve performance.
The multi-threaded part of Redis is only used to handle network data reading and writing and protocol parsing, and the execution of commands is still single-threaded. The reason for this design is that Redis does not need to be complicated by multi-threading, controlling concurrency for keys, LUA, transactions, LPUSH/LPOP, etc.
conclusion
Redis chooses to use single-thread model to process client requests mainly because CPU is not the bottleneck of Redis server, so the performance improvement brought by using multi-thread model cannot offset the development cost and maintenance cost brought by it, and the performance bottleneck of the system is mainly in the network I/O operation. The introduction of multi-threaded operation in Redis is also for the consideration of performance. For the deletion operation of some large key value pairs, the non-blocking release of memory space through multi-threading can also reduce the blocking time on the main thread of Redis and improve the efficiency of execution.