Redis
maang.io System Design Series
Redis is an in-memory data-structure server: not just a cache, but a toolbox of strings, hashes, lists, sets, sorted sets, streams, and more, served from RAM by a single-threaded event loop in sub-milliseconds. Add persistence (RDB/AOF), replication, and cluster sharding, and it becomes the swiss-army knife behind caches, rate limiters, leaderboards, queues, and session stores.
This deep dive comes in four parts:
- Foundations & Core Concepts — the data types and the single-threaded model.
- Internals — persistence (RDB/AOF), replication, and Redis Cluster hash slots, with worked numbers.
- Advanced Internals — eviction, pipelining, transactions, and Lua scripting.
- Interview — Staff/Principal Q&A: use-case fit, persistence trade-offs, and scaling.
Redis — Part 2: Internals & The Single Thread
maang.io System Design Series
What is this?
Back to our counter clerk. In Part 1 we watched them serve requests at a blur, one at a time, from a wall of labeled bins. Now let's actually walk behind the counter and see the machinery. Two questions puzzle every newcomer: how can one clerk possibly be fast enough? and what's actually inside those bins? A bin labeled "leaderboard" isn't magic — under the label there's a specific physical arrangement (a ranking rack) chosen so that "add a score" and "who's in the top 10" are both quick. Redis obsesses over this: for every data type it picks a memory layout tuned for the job, and it will even switch layouts automatically as a bin grows, trading compactness for speed at exactly the right moment.
That's this chapter: why one thread wins, and how the bins are really built.
The one-line idea: Redis is fast for two reasons that reinforce each other. First, one thread executes commands to completion, so there are no locks, no context switches, and every operation is atomic for free. Second, every value is a hand-tuned data structure with a compact "small" encoding and a fast "big" encoding, and Redis flips between them automatically — so you get both low memory and low latency without thinking about it.
Let's start with the thread, because it colors everything else.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson