multithreadingredisproject-reactorsingle-threaded

If Redis is single threaded, how can it be so fast?


I'm currently trying to understand some basic implementation things of Redis. I know that Redis is single-threaded and I have already stumbled upon the following Question: Redis is single-threaded, then how does it do concurrent I/O?

But I still think I didn't understand it right. AFAIK Redis uses the reactor pattern using one single thread. So if I understood this right, there is a watcher (which handles FDs/incoming/outgoing connections) who delegates the work to be done to it's registered event handlers. They do the actual work and set eg. their responses as event to the watcher, who transfers the response back to the clients. But what happens if a request (R1) of a client takes lets say about 1 minute. Another client creates another (fast) request (R2). Then - since Redis is single threaded - R2 cannot be delegated to the right handler until R1 is finished, right? In a multi threaded environment you could just start each handler in a single thread, so the "main" thread is just accepting and responding to I/O connections and all other work is carried out in own threads.

If it really just queues the io handling and handler logic, it could never be as fast it is. What am I missing here?


Solution

  • You're not missing anything, besides perhaps the fact that most operations in Redis complete in less than a ~millisecond~ couple of microseconds. Long running operations indeed block the server during their execution.