Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the threading model of Redis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what is the threading model of Redis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Interview questions

What's the difference between Redis and Memcached?

What is the threading model of Redis?

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

2. Psychological analysis of the interviewer

When I ask you this, I am asking you about the principle of Redis to see if you have thought about it or studied it. 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 even know this, when you use Redis later, don't you know anything if something goes wrong and you don't know how to start?

It is also possible that the interviewer will ask you the difference between Redis and Memcached. But to be honest, interviewers don't like to ask this question in recent years. Because memcached is the cache scheme commonly used by major Internet companies in the early years, but now it is basically Redis in recent years, and few companies use memcached.

3. Warm reminder

If you do not know what redis and memcached are, then you quickly Baidu to get started with redis and memcahced, find two blog tutorials to simply start, and then try a few simple operations, first feel, follow this blog to follow the tutorial to do a demo program, within an hour to get started, you will be able to have a preliminary understanding and entry. And then come back and keep looking.

Another friendly reminder is that to understand redis's threading model, you need to know the basics of socket networking. If you don't know socket, then I don't think you learned java well. Beginners should learn the knowledge related to socket network communication of java.

4. Analysis of interview questions (1) what's the difference between redis and memcached

In fact, there are many differences in this question, but here we should take a few of the redis authors to compare. After all, the interview is not about reciting a blog, and the more you say, the better. You just have to answer the key points. But that's not to say that you don't need to know anything other than the ones listed here. You can say: there are a lot of differences between the two, so I'll just pick a few of the most typical ones.

1) Redis supports server-side data manipulation: compared with Memcached, Redis has more data structures and supports richer data operations. Usually in Memcached, you need to take the data to the client to make similar modifications and then set back. This greatly increases the number of network IO and data volume. In Redis, these complex operations are usually as efficient as normal GET/SET. So, if you need caching to support more complex structures and operations, then Redis would be a good choice.

2) comparison of memory usage efficiency: if you use simple key-value storage, the memory utilization of Memcached is higher, but if Redis uses hash structure to do key-value storage, its memory utilization will be higher than Memcached because of its combined compression.

3) performance comparison: since Redis only uses a single core, while Memcached can use multiple cores, Redis has higher performance than Memcached in storing small data on each core. In the data of more than 100k, the performance of Memcached is higher than that of Redis. Although Redis has recently optimized the performance of storing big data, it is still slightly inferior to Memcached.

4) Cluster mode: memcached does not have the native cluster mode and needs to rely on the client to write data to the cluster. However, redis natively supports cluster mode, and redis officially supports redis cluster 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) threading model of redis

Ask this question of principle, in fact, you can combine the picture to tell the interviewer this question, while drawing the most persuasive, the interviewer will silently give you a thumbs up.

1) File event handler

Redis developed a network event handler based on the Reactor pattern, which is called the file event handler file event handler. This file event handler is single-threaded, so Redis is called a single-threaded model. It uses IO multiplexing mechanism to listen to multiple Socket at the same time, and selects the corresponding event handler to handle the event according to the event type on the Socket.

If the listening Socket is ready to perform accept, read, write, close, etc., the file event corresponding to the operation will be generated, and the file event handler will call the previously associated event handler to handle the event.

The file event processor runs in single-thread mode, but by monitoring multiple Socket through IO multiplexing mechanism, we can achieve a high-performance network communication model and interface with other internal single-threaded modules, which ensures the simplicity of the thread model within Redis.

The structure of file event processor consists of four parts: multiple Socket, IO multiplexer, file event dispatcher and event processor (command request processor, command reply processor, connection response processor, etc.).

Multiple Socket may produce different operations concurrently, and each operation corresponds to a different file event, but the IO multiplexer listens to multiple Socket and queues the Socket in a queue, taking one Socket from the queue to the event dispatcher at a time, and the event dispatcher gives the Socket to the corresponding event handler.

Then after an Socket event is processed, the IO multiplexer sends the next Socket in the queue to the event dispatcher. The file event dispatcher selects the corresponding event handler to handle based on the events currently generated by each Socket.

2) File event

When the Socket becomes readable (for example, when the client performs a write operation on the redis, or a close operation), or when a new replicable Sccket appears (the client performs a connect operation on the redis), the Socket generates an AE_READABLE event.

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

The IO multiplexer can listen for both AE_REABLE and AE_WRITABLE events, and if both events are generated by a Socket, the file event dispatcher gives priority to the AE_READABLE event, followed by the AE_WRITABLE event.

3) File event handler

If the client wants to connect to the redis, it will associate the reply processor for the Socket.

If the client wants to write data to redis, the processor is requested for the associated command for Socket.

If the client wants to read data from redis, it will reply to the processor for the Socket associated command.

4) A process of communication between client and redis

When Redis initializes, Redis associates the connection response handler with the AE_READABLE event, and then if a client initiates a connection with Redis, an AE_READABLE event is generated, and then the connection response processor processes the establishment of a connection with the client, creates the corresponding Socket of the client, and associates the AE_READABLE event of the Socket with the command request processor.

When the client initiates a request to the Redis (whether it is a read request or a write request), an AE_READABLE event is first generated in the Socket and then handled by the corresponding command request processor. This command request processor reads the data related to the request from the Socket and then executes and processes it.

Then, after the Redis prepares the response data to the client, it will associate the AE_WRITABLE event of Socket with the command reply handler. When the client side is ready to read the response data, an AE_WRITABLE event will be generated on the Socket, which will be handled by the corresponding command reply processor, that is, the prepared response data will be written to Socket for the client to read.

After the command reply processor has finished writing, the association between the AE_WRITABLE event of the Socket and the command reply processor is deleted.

(3) Why can the Redis single-thread model be so efficient?

1) Pure memory operation

Redis keeps all the data in memory, and the response time of memory is about 100 nanoseconds, which is an important basis for redis's QPS of more than 10,000.

2) the core is the non-blocking IO multiplexing mechanism.

Having a non-blocking IO means that the thread doesn't have to block when reading and writing the IO, and the read and write can be done instantly and then the thread can move on to something else.

Redis needs to process multiple IO requests and return the result of each request to the client. Because redis is a single-threaded model, only one IO event can be handled at a time, so redis needs to pause the processing of one IO event at an appropriate time and turn to deal with another IO event, which requires IO multiplexing technology, just like a manager who can manage the IO event of a socket. When the socket is selected, the IO event on the socket is processed, and the other IO events are suspended.

3) single thread avoids the performance problems caused by frequent context switching of multiple threads. (Baidu multithreaded context switching)

First, single threading can simplify the implementation of data structures and algorithms. The implementation of concurrent data structures is not only difficult, but also difficult to develop and test.

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

Single-threaded problem: there is a requirement for the execution time of each command. If one command executes too long, it will block other commands, so redis is suitable for scenarios that need to be executed quickly.

That's all for "what is the threading model of Redis?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report