Single Threading Model:
The data structures in Redis are not all simple kv, but also a list, hash and other complex structures , these structures are likely to be fine-grained operations, such as in a long list of even Cotton added an element, In the hash or delete an object , such an operation will add a lot of locks, resulting in a significant increase in synchronization costs, Redis trade-offs after choosing to use a single thread, highlighting the flexibility of their own functions , Any atomic operation on a single-threaded basis can be implemented without cost , and complex data structures can be easily applied
each invocation of the Redis client to the server undergoes a three-step process of sending commands, executing commands, and returning results, in which the command stage is executed, and since Redis is a single-threaded process for commands, each command that arrives at the server is not executed immediately, and all commands go into a queue , and then executed one after the other, and the order in which the commands are sent by multiple clients is indeterminate, but it is certain that no two commands are executed concurrently, and that there is no concurrency problem, which is the single-threaded basic model of Redis
Reasons for single-threaded model processing capacity per second:
1 Pure memory Access , data stored in memory, memory response time is about 100ns, which is the important basis for Redis access every second
2 non-blocking I/O, Redis uses epoll as the implementation of I/O multiplexing technology, and Redis's own event-handling model transforms connection, read-write, and shut-down in epoll to time, without wasting too much time on I/O
3 single threading avoids thread switching and race generation consumption
4 Redis uses a single-threaded model, and each command executes if it takes a lot of time to block other threads, which is deadly for high-performance services such as Redis, so Redis is data for high-speed execution
Redis FAQs:
Why is Redis single-threaded?
because the CPU is not a redis bottleneck, Redis's bottleneck is most likely the machine memory or network bandwidth , since the single-threaded easy to implement, and the CPU does not become a bottleneck, it is Cheng Zhang the use of single-threaded scenario
What if the CPU becomes your Redis bottleneck, or if you don't want the server to be idle for other cores?
It's also easy, you can get a few more redis processes. Redis is a keyvalue database, not a relational database, and there is no constraint between the data. As long as the client distinguishes which key to put on which Redis process. Redis-cluster can help you do better.
redis-Single Thread architecture