The master of Kung Fu Panda says that it is impossible to make progress if you only do what you are capable of. If you always choose to do ability within the scope of the matter, I think it is repeated labor, to do something new, no adventure, harvest also must be very insipid, there is a “run” like a dog inside of the unknown road lose courage, true end of youth, young people always have a lot of blood, dare to try, dare to face the unknown things, This is also a kind of expression of young heart.

End of the BB

Now why is single-threaded Redis so fast?

We talked about thread pools, why we use thread pools, and we talked about the Cotter principle, but it’s just a jump, which means that if you change serial to parallel, you don’t have to wait, your program executes faster, and your CPU utilization goes up.

Why is Redis still using single threads? With thread pools, it’s just asking for trouble.

Since every upside has a downside, let’s talk about the downside of multithreading

1 Switching up and down questions

When we buy a computer will see the CPU’s auditing, mononuclear, dinuclear, quad-core, is a common thread corresponding to a nuclear, namely mononuclear single thread, the thread pool is how to allocate a lot of threads, said to be at the same time perform multithreaded, in fact is not, for example, a single core CPU of the single thread he can run multithreaded, how? This process, the truth is that when server to the requesting thread CPU, CPU to provide a time slice for each thread, time is very short, generally is generally dozens of ms, and then the CPU will constantly switching between these time slice, switching time will record the current state of the task after the switch back according to the state continues to perform the task, This process is called context switching and, of course, this switching requires performance.

2 blocks

Using multithreading, we must pay attention to is blocking, we lock to ensure that the data is certain, but locking will also make parallel into serial, using multithreading if there are more cases, the system throughput rate actually does not return significantly increased, that some locks will change the parallel.

Here we talk about throughput, which is the number of transfers per second, say 5 kilobytes per second, and a QPS, which is the number of application accesses per minute

Having said that, the disadvantages of multi-threading, let’s talk about the advantages of redis using single threads

1 it is based on memory, memory IO is very fast

2. Its data structure is simple, HashMap, and the time complexity is O1

It has a multiplexing mechanism

Speaking of this multiplexing mechanism, this is the highlight of Redis, in fact, Redis multiplexing mechanism is wrapped in the operating system multiplexing mechanism.

He has several commands, select, poll, epoll

1) Select a total of 1024 sockets from the Redis end, copy them to the kernel, and then iterate over them. If there is any servicable data in the socet that is traversed, the data is returned to the Redis server for writing. For those sockets that cannot write, it monitors them, that is, iterating over them, waiting for them to write, and then returning them to Redis.

2) Poll means no limit of 1024

3) Epoll this is another implementation idea

Adding an intermediate layer is essentially adding an EP object to the kernel, and then storing the socket in the ep object, so that the socket is never copied from redis to the kernel. Ep also has a bidirectional linked list queue. Where can I find the socket quickly when it is triggered

In addition, redis6.0 already uses multiple threads to handle IO, so it is inaccurate to say that the current redis is single-threaded. Redis still uses a single thread for reading and writing, but network IO is now multi-threaded, because the SELECT itself is blocked, so it will reduce the efficiency of the program.