Skip to main content
Let’s walk through a system-design view of Redis to understand why it serves requests in sub-millisecond time in many real-world workloads. Imagine a user opens a product page on an online store. A typical relational database must parse SQL, plan the query, and read pages from disk — disk reads (even from SSDs) cost tens to hundreds of microseconds. Redis, by contrast, often replies in under one millisecond. The main reasons are where data lives, how Redis executes commands, how it handles many connections, and how its data structures and protocol are optimized for speed.

Where does the data live?

  • Traditional databases read from disk (SSD/HDD), so individual reads cost tens to hundreds of microseconds.
  • Redis keeps the working dataset in RAM. RAM access is on the order of 100 nanoseconds — roughly a thousand times faster than disk.
Because Redis stores data in-memory, it eliminates disk I/O as the dominant latency source for most operations. That choice drives the rest of the design trade-offs (memory use, persistence options, etc.).

Threading model: one main thread for command execution

Redis executes commands serially on a single main thread. This avoids locks, context switches, and synchronization overheads that arise with a multithreaded execution model.
  • If two clients modify the same key (for example, product:42 stock), Redis runs one command, then the next — no locks needed.
  • Small, deterministic commands complete quickly, so the single-threaded model achieves very high throughput.
  • Since Redis 6, I/O threads can help with network read/write, but command execution still runs on the main thread.
A dark-themed infographic showing a "One Thread" Redis dashboard with sections labeled "Main Thread," I/O sliders for Read and Write, and a RAM visualization. Pink user icons and a Redis logo appear at the top, with teal blocks and a pink "product:42" tag in the RAM area.

How does one thread listen to thousands of clients?

Redis uses an event loop and I/O multiplexing so a single thread can efficiently manage many connections.
  • If 10,000 clients are connected but only 50 have data ready, Redis asks the OS which sockets are ready.
  • On Linux, this leverages epoll; the OS returns only sockets with data, Redis processes those, and then polls again.
  • This pattern (event loop + I/O multiplexing) avoids creating one thread per connection and minimizes context switching.
A stylized infographic of an "Event Loop" showing the OS (Linux penguin labeled "epoll") connected by arrows and an "I/O MULTIPLEXING" block to a Redis component with sections labeled "Connections," "Main Thread," and "RAM." Bright icons and colored blocks illustrate event flow and memory/connection handling.
Redis achieves low latency by combining in-memory data with an event-driven loop. For network-heavy workloads, make sure client libraries and OS tuning (file descriptors, TCP buffers) are aligned with your connection count and throughput goals.

Data structures and protocol: predictable, compact, and fast

Redis stores and accesses data using structures designed for common access patterns:
  • Keys are stored in a hash table for ~O(1) lookups regardless of dataset size.
  • Ordered collections (like sorted sets) use structures such as skip lists to maintain order without expensive re-sorts.
  • Small values and objects are stored in compact encodings to reduce memory overhead and CPU cache misses.
  • There is no SQL parsing or query planning stage — clients send simple commands using Redis’ RESP protocol, which is explicit about lengths and data layout.
A small RESP example for requesting product:42:
This means an array of 2 elements (GET and product:42), with each bulk string preceded by its length. Because lengths are explicit, parsing is fast and deterministic.
A stylized diagram of an app server sending a GET to Redis, showing a 3‑byte command and a 10‑byte key labeled "product:42." Below, a Redis box visualizes the key stored in RAM as colored blocks.

Quick comparison: disk-based DB vs Redis (in-memory)

Trade-offs and pitfalls

The design choices that make Redis very fast also introduce trade-offs you must consider:
  • Memory: all working data must fit in RAM (or be sharded).
  • Single-threaded execution: long-running commands block the main thread and delay other clients.
    • Example: KEYS on millions of keys walks the entire keyspace and will block Redis while it runs.
    • Prefer non-blocking alternatives like SCAN, background processing, or running heavy commands on replicas.
Avoid long-running commands (like KEYS on a very large dataset) on the main Redis instance; use safer patterns such as SCAN, background jobs, or running such commands on replicas to prevent blocking the primary.
A stylized dashboard titled "Trade-off" showing Redis' main thread with a running "KEYS" operation that is blocked by A, B, C. Below is a RAM usage bar and a grid of small squares representing many keys (labeled 10M keys).

Summary

Redis is fast because:
  • Data lives in RAM (eliminating disk I/O latency).
  • Command execution is single-threaded, avoiding lock contention.
  • An event loop and I/O multiplexing let one thread efficiently serve many connections.
  • Data structures and the RESP protocol are optimized for predictable, low-latency access.
These design decisions favor extremely low latency and high throughput for many workloads, at the cost of memory usage and sensitivity to long-running commands.

Watch Video