The original link

Can insist on others can not insist, to have others can not have. Pay attention to programming avenue public number, let us stick to what we think in mind, grow up together!

The thread model of Redis Why is single thread efficiency so high?

In this series, I have compiled some interview questions to share with you to help students who want to change jobs in Jinsan Yinsi. Consolidate, surprise interviewers often ask some interview questions, come on!!

1. Interview questions

What’s the difference between Redis and Memcached?

What is the threading model of Redis?

Why is Redis single-threaded but still able to support high concurrency?

2. Interviewer psychological analysis

The time to ask this is to ask you the principle of Redis, to see if you have thought about, research. One of the most basic internal principles and features of Redis is that Redis is actually a single-threaded working model. If you don’t know this, then you don’t know what to do when you use Redis.

Chances are your interviewer will ask you the difference between Redis and Memcached. But let’s be honest, in recent years, interviewers have become less inclined to ask this question. Memcached was a popular caching solution for Internet companies in the early years, but now it’s mostly Redis, and few companies use memcached anymore.

3, warm reminder

If you don’t already know what Redis and memcached are, Google Redis starter and Memcahced Starter, find two blog tutorials and start them up. Then try some simple operations. Can get a preliminary understanding and entry. And then keep coming back.

As a bonus note, you need to understand the basics of socket networking to understand the Redis threading model. If you don’t know socket, then I think you haven’t learned Java well. Beginners should learn Java socket network communication related knowledge.

4, interview question analysis

What is the difference between Redis and memcached

This question, in fact, can be compared to a lot of differences, but here is still to take a few redis author to compare it, after all, the interview is not to recite the blog, not to say the more the better, you just answer the key points in fact it is ok. But that’s not to say you don’t need to know more than a few listed here. You can say: There are a lot of differences between the two, and I’ll just pick a few of the most typical ones.

Redis has more data structures and supports richer data operations than Memcached. In Memcached, you need to take the data to the client, make similar changes, and set it back. This greatly increases the number of network IO and data volume. In Redis, these complex operations are usually just as efficient as regular GET/SET. So if you need a cache that can support more complex structures and operations, Redis is a good choice.

Memcached is more efficient using simple key-value storage than Redis using hash hash for key-value storage because of its combined compression.

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 outperforms Redis, which has recently been optimized for storing large data, but still lags behind Memcached.

There is no native cluster mode. You need to rely on the client to write data in fragments to the cluster. However, Redis currently supports clustering natively. Redis officially supports Redis Cluster mode, which is better than Memcached.

In fact, the second and third can be said or not, the key is 1 and 4.

(2) Thread model of Redis

Ask this rational question, in fact, you can combine the picture to tell the interviewer this question, draw a picture while speaking the most convincing, the interviewer will give you a silent thumbs-up in mind.

1) File event handler

Redis developed a network event handler based on Reactor model, which is called file Event Handler. This file event handler, it is single-threaded, so Redis is called single-threaded model, it uses IO multiplexing mechanism to listen to multiple sockets at the same time, according to the Socket event type to select the corresponding event handler to process the event.

If the monitored Socket is ready to perform accept, read, write, or close operations, a file event corresponding to the operation will be generated. At this time, the file event handler will call the associated event handler to process the event.

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 handler consists of four parts: multiple sockets, IO multiplexer, file event dispatcher and event handler (command request handler, command reply handler, connection reply handler, etc.).

Multiple sockets may concurrently produce different operations, each corresponding to a different file event, but the IO multiplexer listens for multiple sockets, queues them in a queue, and fetches one Socket at a time to the event dispatcher. The event dispatcher gives the Socket to the corresponding event handler.

The IO multiplexer then sends the next Socket in the queue to the event dispatcher after the Socket has finished processing the event. The file event dispatcher selects the event handler for each Socket based on the events currently generated.

2) File events

The Socket generates an AE_READABLE event when the Socket becomes readable (for example, when the client performs a write operation on Redis, or a close operation), or when a new Sccket that can be answered appears (when the client performs a connect operation on Redis).

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

IO multiplexers can listen for both AE_REABLE and AE_WRITABLE events. If a Socket produces both, the file event dispatcher processes AE_READABLE events first, before AE_WRITABLE events.

3) File event handler

If the client is connecting to Redis, the connection reply handler is associated with 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.

4) A communication process between the client and Redis

On initialization, Redis associates the connection acknowledgment handler with AE_READABLE events. Then, if a client initiates a connection with Redis, an AE_READABLE event is generated, which is then handled by the connection acknowledgment handler to establish a connection with the client. Create the Socket for the client and associate the AE_READABLE event for that Socket with the command request handler.

When a client makes a request to Redis (whether it is a read request or a write request, it is the same), an AE_READABLE event is first generated on the Socket and then processed by the corresponding command request handler. The command request handler reads the request data from the Socket and executes and processes it.

When Redis is ready to send response data to the client, it associates the Socket AE_WRITABLE event with the command reply handler. When the client is ready to read response data, an AE_WRITABLE event is generated on the Socket. This is handled by the corresponding command reply processor, which writes the prepared response data to the Socket for the client to read.

After the command reply handler is written, the association between AE_WRITABLE events on the Socket and the command reply handler is removed.

(3) Why is Redis single threaded model so efficient?

1) Pure memory operation

Redis puts all the data in memory, and the memory response time is about 100 nanoseconds, which is an important basis for Redis’s QPS of over 10,000.

2) Core is based on non-blocking IO multiplexing mechanism

Having a non-blocking IO means that the thread doesn’t have to block when it reads or writes an IO, the read or write can be done instantaneously and the thread can move on to something else.

Redis needs to process multiple IO requests and return the results of each request to the client. Because Redis is a single-threaded model, it can only handle one IO event at a time, so Redis needs to suspend the processing of one IO event at the appropriate time and switch to another IO event. This requires THE use of IO multiplexing technology, just like a manager. Manage THE I/O events of each socket. When a socket is selected, the I/O events on the socket are processed, and the processing of other I/O events is suspended.

3) Single threading instead avoids the performance problems caused by multiple threads’ frequent context switching. (Baidu multi-threaded context switch)

First, single threading simplifies the implementation of data structures and algorithms. Concurrent data structures are difficult to implement and difficult to develop and test

Second, single threading avoids the cost of thread switching and races, which are often performance killers for server-side development.

Single-threaded problems: There is a requirement for the execution time of each command. If one command is executed too long, it will block other commands, so Redis is suitable for scenarios that require fast execution.

This series of articles is an interview shock, not a tutorial. If you dig deep, you can talk a lot about the Redis threading model, and you just need to explain the principle of the interview. This series of articles is quick assault, quick pick up, review.

Please give it a thumbs up


Welcome your support and attention