What is the single thread ofredis? Why does redis use single thread? Why is single-threaded Redis so fast? The following article will give you an analysis and I hope it will be helpful to you!
![Why does redis use single thread? Why so fast?](https://img.php.cn/upload/article/000/000/024/617f4b50e5523866.jpg)
1. Basic concept
##[Related recommendations:
Redis video tutorial]
2. Why does redis use single thread
- Overhead of multi-threading
- Using multi-threading can increase system throughput (per request) and increase system scalability
- Unlimited increase in the number of threads leads to a decrease in throughput
Shared resources are accessed by multiple threads at the same time, such as shared data structures
- In order to ensure thread safety, resulting in performance sacrifice
Coarse-grained locks cause all serialization, and the system throughput rate increases with the increase of threads
- Therefore, in order to save concurrent resources Management, redis uses a single thread to ensure that all operations are serialized
![163573219225487Why does redis use single thread? Why so fast? Why does redis use single thread? Why so fast?](https://img.php.cn/upload/image/560/839/541/163573219225487Why%20does%20redis%20use%20single%20thread?%20Why%20so%20fast?)
![163573219563993Why does redis use single thread? Why so fast? Why does redis use single thread? Why so fast?](https://img.php.cn/upload/image/995/485/522/163573219563993Why%20does%20redis%20use%20single%20thread?%20Why%20so%20fast?)
3. Single thread Why is redis fast?
Most operations of redis are efficient data structures in memory
- redis adopts a multiplexing mechanism to handle a large number of client requests in the network , to achieve high throughput rate
4.socket
##socket communication process (network IO processing key-value pair read and write network IO processing )
- SimpleKV In order to process a Get request
- needs to listen to the client request (bind/listen)
- Establish a connection with the client (code) (accept)
- Read the request from the socket (recv)
- Parse the request sent by the client (parse)
- Read the key value data according to the request type (get)
- Finally return the result to the client, that is, write data back to the socket (send)
Potential blocking points
-
accept () When the connection fails to be successfully established, it will always block\
recv() When reading data from the client, it will always block\
![163573220217261Why does redis use single thread? Why so fast? Why does redis use single thread? Why so fast?](https://img.php.cn/upload/image/196/382/990/163573220217261Why%20does%20redis%20use%20single%20thread?%20Why%20so%20fast?)
The non-blocking mode of socket
- ensures that the Redis thread will neither wait at the blocking point like in the basic IO model nor It will not cause Redis to be unable to process the actual arriving connection request or data
- IO multiplexing mechanism in Linux
![1635732208821017.png Why does redis use single thread? Why so fast?](https://img.php.cn/upload/image/335/537/899/1635732208821017.png)
5 .Multiplexing
The IO multiplexing mechanism in Linux refers to one thread processing multiple IO streams, select/poll
In a single thread, there are multiple sockets and connected sockets listening at the same time
The specific implementation
-
FD is Multiple sockets
Redis uses the epoll mechanism to allow the kernel to listen to sockets
Redis can connect and process multiple clients request, thereby improving concurrency
select/epoll provides an event-based callback mechanism, calling the corresponding processing function for different events
- First, the event Put it into the event queue, without checking whether the request actually occurs, to avoid wasting CPU resources
- Perform response operations according to the corresponding events
![1635732216472950.png Why does redis use single thread? Why so fast?](https://img.php.cn/upload/image/878/378/465/1635732216472950.png)
6. Summary
Is redis really single-threaded
- on the network IO and data reading and writing operations use a thread.
Why use single thread
- To avoid concurrency control problems in multi-threaded development.
- The multiplexed IO model is closely related.
Why is single thread so fast
For more programming related knowledge, please visit:
Programming Video
! !
The above is the detailed content of Why does redis use single thread? Why so fast?. For more information, please follow other related articles on the PHP Chinese website!