r/PythonLearning • • Feb 25 '26

If Redis is single-threaded, how does it actually hit millions of RPS?

I’ve been diving into Redis architecture lately, and the "single-threaded" nature of it feels like a paradox when you look at its performance benchmarks.

I understand that it avoids context switching and lock contention, but I’m struggling to visualize how it handles massive concurrency without getting choked by a few heavy requests. Is it all down to the event loop (IO multiplexing), or is there more "magic" happening under the hood with how it handles memory?

Would love a breakdown of why this design choice actually makes it faster rather than being a bottleneck.

73 Upvotes

32 comments sorted by

9

u/Waste_Grapefruit_339 Feb 25 '26

Great question - this seems paradoxical at first, but the key is that Redis isn't "single-threaded" in the way people usually imagine.

The command execution loop itself is single-threaded, yes. But that actually makes it fast, not slow, because it avoids locks, thread contention, and context switching overhead. Instead of multiple threads fighting over shared memory, one thread processes commands sequentially - which is extremely efficient when each command is very small and fast (which Redis commands usually are).

The real reason it can handle millions of requests/sec is:

  • I/O is multiplexed (epoll/kqueue/select), so one thread can manage thousands of connections

  • operations are in-memory -> no disk latency

  • commands are intentionally simple and atomic

  • network + kernel do a lot of the heavy lifting

  • newer Redis versions even offload some I/O to background threads

So the trick is: single-threaded execution + non-blocking I/O + in-memory data = extremely high throughput.

It's less "magic" and more "carefully constrained design."

3

u/ArtisticFox8 Feb 26 '26

Are you a bot?

2

u/Waste_Grapefruit_339 Feb 26 '26

If I were a bot, I’d probably optimize my answers faster 😅

2

u/bbqroast Feb 27 '26

Some people just know things

3

u/ArtisticFox8 Feb 27 '26

I was hinting at the style of his reply

1

u/DocHolligray Feb 27 '26

I have been called a bot a few times because sometimes I use words like….lexicon or loquacious…usually not in the same sentence…but you get the idea…I think people are looking for ai and are trying to force patterns that might not be there…

1

u/_mick_s Feb 27 '26

Honestly aside from the 'great question ' start, which seems to often show up in LLM responses it just seemed like a well written response.

1

u/toooskies Feb 28 '26

I find myself, having read too many AI-written prompts, starting to accidentally use them in my own writing. It disturbs me.

1

u/kimovitch7 Feb 28 '26

It's the less "magic" thing at the end for me

1

u/iPlayNL Mar 01 '26

No this is fully either a bot or someone using AI to generate responses at least, it's painfully obvious, especially if you check their history.

2

u/RobotChad100 Jun 30 '26

Most definitely an AI generated comment. Insane that people are gaslighting this guy into believing it is human written.

1

u/robhanz Feb 25 '26

I've worked on MMOs most of my career - most of them followed the same model for their servers. Single-threaded, but I/O was async. It's quite efficient.

1

u/Waste_Grapefruit_339 Feb 25 '26

That’s a great real-world example. Game servers are probably one of the best proofs that this model scales really well in practice.

It’s fascinating how reducing contention often beats adding more threads.

1

u/robhanz Feb 25 '26 edited Feb 25 '26

Unless you're really computation-bound. Even in that case, a model of a main thread that spins off isolated threads to do the heavy computation and returns the result is probably where I'd lean.

Most apps are I/O bound, so just avoiding blocking I/O gets you most of the results. Maybe even more efficiently, since using async/polling I/O isn't going to cause the overhead of threads (switching, stack space, etc.).

Having multiple threads running over shared mutable state? Ugh, gross. Even if you gain a small efficiency, that's a great way to build a bug farm.

Which is another good point - this design tends to be much, much more stable and easy to debug.

2

u/Waste_Grapefruit_339 Feb 25 '26

Exactly - that distinction between CPU-bound vs I/O-bound is really the key.

I like the main-loop + worker model you described, because it keeps the core deterministic while still allowing heavy tasks off the critical path.

And yeah, shared mutable state is where complexity tends to explode fast.

1

u/thingerish Feb 27 '26

Or sharding the load out to many single threaded pipelines.

1

u/griffin1987 Feb 27 '26

I'd even go as far as to say that ALL apps are I/O bound. Displaying something is I/O and generating a network reply is I/O, as is handling Keyboard/Mouse etc.

In my experience, the reason that single threaded servers work so well is that, once you do it efficient enough, your code is I/O bound by the speed you can write UDP or TCP packets (which in itself might be limited by your kernel or the driver again, but to your software that's also usually part of I/O as you can't do much about that besides optimizing a few settings for your use-case).

1

u/robhanz Feb 27 '26

Ehhhhhhhh. FFMPEG is generally cpu bound. Rendering is cpu bound. Compilers are typically cpu bound.

So you’re generally correct, but there are some apps that are compute bound. All is an overstatement.

But it’s a good starting assumption.

1

u/griffin1987 Feb 27 '26

Yeah, you're right, I was thinking about software that you typically create for some type of enterprise or for a client project.

1

u/robhanz Feb 27 '26

Your point generally stands. You’re almost always bound by I/O or user input. CPU is rarely the actual issue.

Which is ironic given how people obsess about it.

1

u/griffin1987 Feb 27 '26

Never underestimate how bloated and inefficient the sum of modern software stacks can be - I'm pretty sure my windows is currently using more compute when "Idle" than my first 286 even had back in the day.

1

u/94358io4897453867345 Feb 28 '26

It doesn't scale beyond a few hundreds of people

1

u/Alborak2 Feb 27 '26

Something doesnt quite add up for me. Is millions here 1 to 2 million or like closer to 10? 1 million requests per sec is 1 us per operation. Thats about 10 full cache misses of budget. I just wrote something that handles 2 million requests per sec single threaded and all it can do is read a few cache lines of each packet its reading off a user mode polled network driver and hand off to side threads with a lock free queue, no syscalls anywhere and a lot of batching / prefetching.

Epoll alone with thousands of sockets is over 1us call time with the context switch.

I can see where it works, but they have to be handing off io tasks to side threads in batches and/or using io uring to kill the context switch overhead.

1

u/vbcodep Jun 07 '26

The reason you can get millions of operations is because Redis uses an event queue to process commands. The Redis kernel process each command without having to deal with locking which is where the speed comes from. We know from simple queuing theory that the servicing of this queue must be faster than the number of commands being queued or the queue will be in a death spiral. This is why you can millions of operation ONLY if you use commands that are efficient. Commands like HGETALL, SINTER, KEYS etc. all block the shard while they are processing so if you don't properly model your data you are never going to get millions of ops. Also by horizontal scaling (i.e. running multiple shards via clustering) you can scale up operations as well.

Performance is not guaranteed. It has to be engineered into the application and the data needs to be modeled properly to ensure the queue can keep up.

1

u/nuclearmeltdown2015 Feb 26 '26

Damn I feel like I am on pre COVID again seeing questions like this pop up.

1

u/astronomikal Feb 26 '26

This is similar to how I’ve structured my agent memory system!

1

u/_gianlucag_ Feb 27 '26 edited Feb 27 '26

Multi threading makes sense when you want to perform multiple expensive tasks at the same time. Or the same task as parallel instances of it. If the task is trivial, there's little to no gain in multithreading, and you are better of with a single thread "roundrobin-ing" over the jobs.

In Redis, the real bottleneck is not the cpu, but the I/O. Redis is an in-memory data store, the cpu tasks are extremely simple and short. Consuming a huge list of such simple tasks using a single thread is much more efficient than orchestrating multiple threads over the list (due to synchronization overhead).

1

u/mmacvicarprett Feb 28 '26

I do not think you will realistically get in the millions of qps without getting into cluster mode. I normally see tenths of thousands, probably can get in the hundreds on a very good machine and simple operations.

2

u/[deleted] Feb 25 '26

[removed] — view removed comment

1

u/GamieJamie63 Feb 27 '26

very cool, thanks

0

u/[deleted] Feb 25 '26

[deleted]

1

u/Swimming-Diet5457 Feb 26 '26

I think that for redis is not really tha case, it is REALLY only 1 thread, just iterating over a eterogenic queue of commands