WHY Redis choose single thread (vs multi threads)

Rich
2 min readOct 30, 2019

--

Background

For instance, using pipelining Redis running on an average Linux system can deliver even 1 million requests per second

Redis is known to be fast and it is using single thread. When back in 2014, Quora: Why isn’t Redis designed to benefit from multi-threading? gives a popular answer:

  1. Ease of programming
  2. Concurrency helps (also refer to Concurrency is not parallelism)
  3. CPU is not bottleneck (also refer to FAQ: Redis is single threaded. How can I exploit multiple CPU / cores?)
  4. Cost effective deployment

However, from FAQ: Redis is single threaded. How can I exploit multiple CPU / cores?

with Redis 4.0 we started to make Redis more threaded. For now this is limited to deleting objects in the background, and to blocking commands implemented via Redis modules. For the next releases, the plan is to make Redis more and more threaded.

Question

What happens? Why Redis 4.0 introduce more threaded and will even more in future?

Answer

Before Redis 4.0

Lazy Redis is better Redis

Redis is *kinda* single threaded, since there are threads in order to perform certain slow operations on disk. So far threaded operations were so focused on I/O that our small library to perform asynchronous tasks on a different thread was called bio.c: Background I/O, basically.

But

Redis DEL operations are normally blocking, so if you send Redis “DEL mykey” and your key happens to have 50 million objects, the server will block for seconds without serving anything in the meantime.

In Redis 4.0

The first release candidate of Redis 4.0 is out

Non blocking DEL and FLUSHALL/FLUSHDB

There is a new command called UNLINK that just deletes a key reference in the database, and does the actual clean up of the allocations in a separated thread

UNLINK not being the default behavior for DEL.

Future

An update about Redis developments in 2019

I/O threading is not going to happen in Redis AFAIK.

A multi-threaded on-disk store is mandatory. A multi-threaded complex in-memory system is in the middle where things become ugly

What instead I *really want* a lot is slow operations threading, and with the Redis modules system we already are in the right direction.

Other Interesting Projects

While Redis is designed to be “single” thread and argues that multi thread may not help a lot. Some people creates a fork of redis (e.g. “KeyDB”) and adds the multi thread feature, such that to convince people “Redis Should Be Multi-threaded”.

--

--