Redis is a NoSQL database based on key-value pairs (key-value), which contains a variety of data structures. The number given on official web is read and write performance can reach 100,000/sec, visible speed.
As a simple example, open 5 Redis clients simultaneously, and execute the following command:
INCR number
Each command is executed in the same way, sending commands, executing commands, and returning results.
In the case of multithreading, no processing is likely to cause problems with the final result of the data. But Redis will not, Redis is a single-threaded approach, the new command will not be executed immediately, but unified into the queue, a piece of execution.
There is no concurrency problem with such a process. Then why is it so fast in a single thread? Generally speaking, single-threaded processing is less than multithreading, for example: 10000 kg of cargo, each vehicle carrying capacity is 1000 kg, then if only one car will need 10 times to complete, but if there are 10 cars, one can be completed.
Then why does Redis use a single-threaded model to achieve a per-second level of processing capacity. Boils down to the following three points:
1, pure memory access, Redis all the data into the memory, the memory response time of about 100 nanoseconds, which is redis can have millions processing capacity of the important basis.
2, there is also a point is that Redis adopted the epoll as I/O multiplexing technology implementation. At the same time, Redis's own event-handling model converts the connection, read-write, and shutdown in Epoll to events and consumes too much time on network I/O. This will be explained in more detail in the following blogs.
3, single-threaded reduce the thread switching and the consumption of the resulting state.
But a single thread also has a problem: it is a requirement for the execution time of each command, and if one of the commands is too long, it can cause other commands to block, which is fatal to redis this high-performance service. So Redis is a database for fast-executing scenarios.