Home > Database > Redis > Why does Redis choose single thread?

Why does Redis choose single thread?

PHPz
Release: 2023-05-30 13:01:33
forward
1891 people have browsed it

1. Redis version iteration

Why does Redis choose single thread?

##Redis2.6, supports lua script;

Redis3.0, supports cluster;

Redis4.0, hybrid persistence, multi-threaded asynchronous deletion;

Redis5.0, core code reconstruction;

Redis6.0, multi-threaded IO;

Redis7. 0, Function, Multi-part-AOF;

2. Why did Redis always use single thread before Redis 4.0?

1. Redis adopts a single-threaded model to facilitate development and maintenance;

2. The single-threaded model can also handle multiple client requests concurrently through IO multiplexing and non-blocking IO;

3. For Redis, the main performance bottleneck is memory and network, not CPU;

3. Redis6.0 introduces multi-threading

Before Redis6.0, Redis From network IO processing to actual read and write command processing, it is all single-threaded, but multi-threading is used for data deletion and data persistence.

The performance bottleneck of Redis is mainly network IO. Therefore, starting from Redis 6.0, multiple IO threads are used to process network requests to improve the parallelism of network request processing.

4. How do the Redis main thread and IO thread complete the request?

Why does Redis choose single thread?

1. Establish a socket connection between the server and the client.
The main thread is responsible for establishing the connection and putting the socket into the global waiting queue. The main thread passes the round The query method allocates the socket connection to the IO thread.

2. The IO thread reads and parses the request
Once the main thread assigns the socket to the IO thread, it will enter the blocking state and wait for the IO thread to complete the client request. At this time, multiple IO threads are processed in parallel.

3. The main thread executes the request command
After the IO thread parses the request, the main thread will still execute these commands in a single-threaded manner.

4. The IO thread will write back to the socket and the main thread will clear the global queue
When the main thread finishes executing the request command, the result will be written into the buffer, and the main thread will enter the blocking state and wait for IO The thread writes the result back to the socket and returns it to the client.

After writing back the socket, the main thread clears the global queue.

5. What is IO multiplexing?

IO multiplexing, a synchronous IO model, implements a thread to monitor multiple file handles. Once a file handle is ready, it can notify the corresponding application to perform corresponding read and write operations. When no file handle is ready, the program will enter a blocking state and release CPU resources.

1. IO, operating system level refers to the reading and writing operations of data between kernel mode and user mode;

2. Multi-channel, multiple client socket connections;

3. Multiplexing, multiplexing threads;

4. IO multiplexing, using a single thread to handle multiple client socket connections at the same time;

Client socket correspondence The file descriptor FileDescriptor is registered in epoll, and epoll will monitor which sockets have messages to avoid a large number of useless operations.

At this time, the socket adopts non-blocking mode. The entire process will only block when selecting, poll, and epoll are called. It will not block when receiving client messages, and the process will be fully utilized. This mode It is generally called event-driven, which is the reactor reaction mode.

Using epoll, the ultimate goal is to improve the throughput of the server.

IO multiplexing and the epoll function are the direct reasons for **"Why is Redis so fast?"**.

The above is the detailed content of Why does Redis choose single thread?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template