Module F-4·8 min read

Why Redis is fast despite being single-threaded, the event loop model in plain terms, and the one rule to never break — KEYS * blocks every client. Production tuning and the full failure-mode playbook continue in A-14.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

F-4 — Redis's Single-Threaded Event Loop

Who this module is for: You know Redis is fast, but you're not sure why it's fast, or how a single-threaded process can handle millions of requests per second. This module covers the core I/O multiplexing model and when a single thread becomes a bottleneck.


More Threads Isn't Automatically Faster

If you've worked with a database or web server before, your instinct probably says: more threads, more CPU cores, more work done in parallel, more throughput. Redis breaks that intuition on purpose.

Think of a small coffee shop with one incredibly fast barista instead of three average ones. Three baristas sound faster — until they all need to reach into the same till at once, tell each other who's making what, and occasionally bump into each other at the espresso machine. One barista who already knows exactly where everything is, moving without ever needing to check in with anyone, can outpace all three combined for high-volume, simple orders.

That's Redis. Almost every command it runs — GET, SET, HSET, ZADD — is a handful of in-memory pointer lookups. There is no disk seek to wait for, no complex computation to perform. The actual "work" per command takes a fraction of a microsecond. The one barista is plenty.


The Event Loop, in Plain Terms

So if one thread does all the work, how does Redis serve 100,000+ requests per second without clients queuing up behind each other for whole seconds at a time?

The trick is that Redis's single thread never sits around waiting on any one client. It uses an operating system mechanism (epoll on Linux, kqueue on macOS) that lets it watch thousands of client connections at once and get tapped on the shoulder the instant any of them has something ready to read:

text

Nobody is idle-waiting for anybody else. The thread just keeps cycling through whichever connections have data ready, executes each command fully, and moves to the next — all before a human would even perceive a delay.


What This Buys You: Atomicity for Free

Because only one command can ever be "in progress" at a time, there is no window where two clients can interfere with the same key mid-operation. INCR, HINCRBY (which you used in F-3 for per-user counters), ZADD — every one of these is atomic by construction, with no locks anywhere in the picture. You never have to reason about race conditions the way you would with two threads reading and writing the same variable in most programming languages.

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.