DEV Community


Posted on • Originally published at on

Why Redis is Single-Threaded

Redis is designed to run in a single thread, but the performance is excellent, why?

4 reasons for Redis is single-threaded

  1. CPU is not bottleneck : All operations of Redis are memory-based, and CPU is not the bottleneck of Redis. In most cases, the bottleneck of Redis is most likely the size of machine memory or network bandwidth. If we want higher performance, with single-threaded Redis, we could use a cluster(multiple processes) solution.
  2. Concurrency : Parallelism is not the only strategy to support multiple clients. Redis uses epoll and event-loop to implement a concurrency strategy and save much time without context switching.

  3. Easy to implement : Writing a multi-threaded program can be harder. We need to add locks and sync mechanism for threads.

  4. Easy to deploy : Single-threaded application could be deployed on any machine having at least a single CPU core.

Concurrency vs. Parallelism

As for the difference between concurrency and parallelism , please refer to this presentation from Rob Pike:

Concurrency vs. Parallelism

Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.

Not the same, but related.

Concurrency is about structure; parallelism is about execution.

Concurrency provides a way to structure a solution to solve a problem that may (but not necessarily) be parallelizable.

We can use an analogy of the restaurant waiter:

What is concurrency

A waiter can provide service to several customers while he can only prepare dishes to only one customer at a time.

Because there will be some intervals between the dishes provided by the kitchen, one waiter could usually handle when the number of customers is less than 5.

What is parallelism

Suppose the kitchen could provide dishes for 20 customers at a time. If the number of customers is too large for one waiter, we need more waiters. In this scenario, multiple waiters are working at the same time; we call it parallelism.

The post Why Redis is Single Threaded appeared first on Coder's Cat.

Top comments (1)

rhymes profile image

Hi Nick! Nice intro.

Redis 6 is going to add optional multi threaded I/O to its codebase.

Unfortunately I can't find a direct link to public release notes / roadmap but if you download the latest 6.0 release, 6.0-rc3, you'll find a file called 00-RELEASE_NOTES which contains the following:

Redis can now optionally use threads to handle I/O, allowing to serve 2 times as much operations per second in a single instance when pipelining cannot be used.

You can find further evidence of this in the THREADED I/O section of the redis.conf:

# Now it is also possible to handle Redis clients socket reads and writes
# in different I/O threads. Since especially writing is so slow, normally
# Redis users use pipelining in order to speedup the Redis performances per
# core, and spawn multiple instances in order to scale more. Using I/O
# threads it is possible to easily speedup two times Redis without resorting
# to pipelining nor sharding of the instance.
# By default threading is disabled, we suggest enabling it only in machines
# that have at least 4 or more cores, leaving at least one spare core.
# Using more than 8 threads is unlikely to help much. We also recommend using
# threaded I/O only if you actually have performance problems, with Redis
# instances being able to use a quite big percentage of CPU time, otherwise
# there is no point in using this feature.

Haven't tried it yet though :)

Redis 6 is due to be released at the end of April 2020