1. What is the difference between memcached and Redis?

Redis has more data structures and supports more data manipulation than memcached. In memcached, you need to take the data to the client, change the type, and then set it back. This significantly increases the number of network IO and data volume. In Redis, these operations can be done on the server side, so these complex operations are just as efficient as normal GET/SET operations. So if you need a cache that can support more complex structures and operations, redis is a good choice. Memcached memory usage is higher if you use simple key-value storage, whereas Redis uses hash hash for key-value storage. Because of the combined compression, Memcached memory usage is higher. Since Redis uses only one core and Memcached can use multiple cores, redis performs better than Memcached on average for storing small data on each core. For data over 100K, Memcached performs better than Redis. Memcached has no native cluster mode. You need to rely on the client to write data in fragments in the cluster. Redis supports the cluster mode native and the Redis Cluster mode officially.

Contrast point memcached redis
Whether server operations are supported Does not support support
Data structure type simple Complex and varied
Memory usage Simple key-value storage with high utilization The hash structure is used for storage, providing high memory utilization
performance High performance for storing big data High performance for storing small data
Cluster pattern No native support Native cluster mode is supported

2. Redis thread mode?

To understand the threading model of REDis, we must first understand the following concepts: (1) File Event Handler redis is a network event Handler developed based on reactor model, which is called File Event Handler. This file event handler is single-threaded, so redis is called single-threaded mode. It uses IO multiplexing mechanism to listen on multiple sockets at the same time, and selects the corresponding event handler to process the event according to the event on the socket.

If the socket is ready to accept, read, write, or close, a file event will be generated, and the file handler will call the event handler to handle the event.

③ The file event processor is running in single-thread mode, but through the IO multiplexing mechanism to monitor multiple sockets, can achieve high performance network communication model, and can be connected with other internal single-thread modules, to ensure the simplicity of redis internal threading model.

④ The structure of file event processor consists of four parts: multiple sockets, IO multiplexing program, file event dispatcher, event processor (command request processor, command reply processor, connection reply processor, etc.).

⑤ Multiple sockets may concurrently produce different operations, each corresponding to a different file event, but the IO multiplexer will listen to multiple sockets, but will put the socket in a queue to process, each time from the queue socket to the event dispatcher, The event dispatcher gives the socket to the corresponding event handler.

⑥ The IO multiplexer will send the next socket in the queue to the event dispatcher only after the socket has finished processing the event. The event dispatcher selects the event handler for each socket based on the events currently generated.

(1) When the socket becomes readable (for example, redis performs write operation or close operation), or a new answerable socket appears (Redis performs connect operation), The socket will generate an AE_READABLE event.

(2) When the socket becomes writable (the client performs the read operation on Redis), the socket generates an AE_WRITABLE event.

IO multiplexers can listen for both AE_READABLE and AE_WRITABLE events at the same time. If a socket fails to generate both, the file allocator will process AE_READABLE before AE_WRITABLE.

(3) File event handler If it is the client that wants to connect to Redis, it will associate the connection reply handler for the socket. If the client is writing data to Redis, the socket is associated with a command requesting the handler. If the client is reading data from Redis, the socket will be associated with the command reply processor.


② When a client makes a request to Redis (whether it is a read or write request, it is the same), an AE_READABLE event will be generated on the socket and processed by the corresponding command request handler. The command request handler reads the requested data from the socket and performs the operation and processing.

(3) When redis is ready to send the response data to the client, it associates the AE_WRITABLE event on the socket with the command reply handler. When the client is ready to read the corresponding data, an AE_WRITABLE event is generated on the socket. This is handled by the corresponding command reply handler, which writes the prepared response data to the socket for the client to read.

④ After the command reply processor is complete, the association between AE_WRITABLE events on the socket and the command reply processor is deleted.

3. Why can single-threaded Redis still support high concurrency?

(1) Pure memory operation. (2) Core is based on non-blocking I/O multiplexing mechanism (3) single thread avoids multithreaded context switching overhead.