why Redis is single threaded(event driven)
Asked Answered
H

3

12

I am trying to understanding basics of Redis. One that that keep coming everywhere is, Redis is single threaded that makes things atomic.But I am unable to imagine how this is working internally.I have below doubt.

Don't we design a server Single thread if it is IO bound application(like Node.js),where thread got free for another request after initiating IO operation and return data to client once IO operation is finished(providing concurrency). But in case of redis all data are available in Main Memory,We are not going to do IO operation at all.So then why Redis is single threaded?What will happen if first request is taking to much time,remaining request will have to keep waiting?

Heeltap answered 28/7, 2017 at 3:11 Comment(1)
This answer explains it pretty well.Parasitic
H
27

TL;DR: Single thread makes redis simpler, and redis is still IO bound.

Memory is I/O. Redis is still I/O bound. When redis is under heavy load and reaches maximum requests per second it is usually starved for network bandwidth or memory bandwidth, and is usually not using much of the CPU. There are certain commands for which this won't be true, but for most use cases redis will be severely I/O bound by network or memory.

Unless memory and network speeds suddenly get orders of magnitude faster, being single threaded is usually not an issue. If you need to scale beyond one or a few threads (ie: master<->slave<->slave setup) you are already looking at Redis Cluster. In that case you can set up a cluster instance per CPU core if you are somehow CPU starved and want to maximize the number of threads.

I am not very familiar with redis source or internals, but I can see how using a single thread makes it easy to implement lockless atomic actions. Threads would make this more complex and doesn't appear to offer large advantages since redis is not CPU bound. Implementing concurrency at a level above a redis instance seems like a good solution, and is what Redis Sentinel and Redis Cluster help with.

What happens to other requests when redis takes a long time?

Those other requests will block while redis completes the long request. If needed, you can test this using the client-pause command.

Hybris answered 28/7, 2017 at 6:32 Comment(4)
Please correct me if I am wrong. Suppose redis gets two requests Req1 which IO bound(taking too much time) then Req2 comes and Req2 requires very little IO then Req2 will be served immediately but if Req1 is CPU intensive(Searching in some large list in Redis) Req2 will have to wait till Req1 is not finished.Heeltap
@carl great and correct - please see my addendum (or perhaps prelude to a new era?) thoughSwearingen
@Heeltap you are correct but this will probably change in future versionsSwearingen
Sorry for necrocommenting. To my current experience lots of keys operations can kill CPU easily. Whether it's cluster or not but as written in the doca use scans if possible.Airspeed
S
20

The correct answer is Carl's, of course. However.

In Redis v4 we're seeing the beginning of a shift from being mostly single threaded to selectively and carefully multi threaded. Modules and thread-safe contexts are one example of that. Another two are the new UNLINK command and ASYNC mode for FLUSHDB/FLUSHALL. Future plans are to offload more work that's currently being done by the main event loop (e.g. IO-bound tasks) to worker threads.

Swearingen answered 28/7, 2017 at 13:31 Comment(1)
Namely, it is possible for a very long running request to allow other requests to run in the same time.Position
M
1

From redis website

Redis uses a mostly single threaded design. This means that a single process serves all the client requests, using a technique called multiplexing. This means that Redis can serve a single request in every given moment, so all the requests are served sequentially. This is very similar to how Node.js works as well. However, both products are not often perceived as being slow. This is caused in part by the small amount of time to complete a single request, but primarily because these products are designed to not block on system calls, such as reading data from or writing data to a socket.

I said that Redis is mostly single threaded since actually from Redis 2.4 we use threads in Redis in order to perform some slow I/O operations in the background, mainly related to disk I/O, but this does not change the fact that Redis serves all the requests using a single thread.

Memory is no I/O operation

Mathematics answered 12/6, 2022 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.