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 principle of redis multiplexing and what are the common problems?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article shares with you what redis multiplexing is all about and what the common questions are. Xiao Bian thinks it is quite practical, so share it for everyone to make a reference. Let's follow the editor and have a look.

1. Single thread understanding of Redis

Each call from Redis client to server goes through three processes: sending commands, executing commands, and returning results. In the command execution phase, since Redis is a single thread to process commands, all commands arriving at the server will not be executed immediately, all commands will enter a queue, and then executed one by one, and the execution order of commands sent by multiple clients is uncertain, but it can be determined that there will be no two commands executed at the same time, and there will be no concurrency problem.

Redis servers connect to clients or other Redis servers through sockets, and file events are abstractions of server operations on sockets. The server communicates with clients or other servers to generate file events, and the server performs a series of network communication operations by listening for and processing these events.

Redis has developed its own network event handler based on the Reactor pattern-file event handler, which uses I/O multiplexing programs to listen to multiple sockets simultaneously (I/O multiplexing technology is described below) and associate different event handlers for sockets according to the tasks currently performed by the sockets. When a listening socket is ready to connect, read, write, close, etc., file events corresponding to the operation are generated, and the file event handler calls the event handler associated with the socket to handle these events.

File event handler consists of:

Note: where the I/O multiplexer sends sockets to the file event dispatcher via queues

II. I/O multiplexing technology

The first thing to understand about I/O multiplexing (also known as "event-driven") is that the operating system provides you with a feature that notifies you when one of your sockets is readable or writable. This way, when used with a non-blocking socket, I can only perform the read operation if the system tells me which descriptor is readable, which ensures that I can read valid data every time without doing pure return-1 and EAGAIN.

This function of the operating system is implemented by system call functions such as select/poll/epoll/kqueue. These functions can monitor the read-write readiness of multiple descriptors at the same time. In this way, I/O operations of multiple descriptors can be completed alternately and concurrently in a thread. This is called I/O multiplexing. Here,"multiplexing" refers to multiple network connections, and "multiplexing" refers to multiplexing the same Redis processing thread. (as shown above)

Multi-channel I/O multiplexing technology allows a single thread to efficiently process multiple connection requests (minimizing the time consumption of network I/O), and Redis operates data very fast in memory, that is, in-memory operations will not become bottlenecks affecting Redis performance, all Redis have high throughput.

III. Frequently Asked Questions

1. Why is Redis single-threaded so fast?

1. It's all memory-based, and the vast majority of requests are purely memory operations, which are very fast. The data is stored in memory, similar to HashMap, the advantage of HashMap is that the time complexity of lookup and operation is O(1);

2. The data structure is simple, and the data operation is also simple. The data structure in Redis is specially designed.

3. Single thread is adopted to avoid unnecessary context switching and race conditions, there is no multi-process or multi-thread switching and CPU consumption, there is no need to consider various lock problems, there is no lock release operation, and there is no performance consumption caused by possible deadlock.

4. Using a multiplexed I/O model, non-blocking I/O;

5. Redis directly built its own VM mechanism, because the general system calls system functions, it will waste a certain amount of time to move and request;

Why not multi-process or multi-thread?

1. Multithreading may involve locking

2. Multithreading involves thread switching and consumes CPU

3. Disadvantages of single thread processing?

1. Time-consuming commands cause concurrency to decline, not just for reads but also for writes

2. Multi-core CPU performance cannot be utilized, but it can be improved by opening multiple Redis instances on a single machine.

4. Redis does not have thread safety problems?

Redis uses a thread-closed approach, enclosing tasks in a thread, which naturally avoids thread safety issues, but for composite operations that rely on multiple redis operations (i.e., multiple Redis operation commands), locks are still required, and possibly distributed locks.

Thank you for reading! What is the principle of redis multiplexing and what are the common questions to share here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge. If you think the article is good, you can share it so that more people can see it!

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

Servers

Wechat

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

12
Report