🟥 Redis

Updated at 2024-04-03 12:10

Redis is single-threaded. This means that Redis server can only execute one command at a time.

But this isn't effectively correct.

While the main command executor is a single-threaded process, Redis uses multiple threads for different tasks.

For example, KEYS command can seem to take ages to run but other clients still can run commands without a delay. What is actually happening is that Redis moves the keys to a different thread that handles the networking where that particular client will start receiving the keys. This way, the main thread can continue to execute other commands.

Redis has a few blocking commands. For example, BLPOP will block the client until a new element is pushed into the list or the timeout is reached. This does not mean that other clients will be blocked. They can still run commands. In simple terms, Redis records that "this client should be notified when X changes" and moves on to the next command.

In a sense, all Redis commands "lock the whole database" for the duration of the command. But this is generally not a problem because individual Redis commands are quick and all non-essential operations are offloaded to non-main task threads. Problems might still arise if you create long-running scripts or functions that take more than a second to execute.