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.

Pay attention to my wechat official number, the first time to get the update of my blog remind, more surprise waiting for you ~

Scan the QR code below or search for wechat account shenshan_laoyuan

! [Deep Mountain ape](p1-jj.byteimg.com/tos-cn-i-t2…

This article by a multiple platform ArtiPub automatic release 6597E5? w=300&h=300&f=png&s=79851)