Why Redis Is Fast: In‑Memory Storage, Efficient Data Structures, Single‑Threaded Model, and I/O Multiplexing
Redis achieves high performance by storing all data in memory, using compact data structures like SDS, linked lists, hash tables, skip lists and integer sets, running a single‑threaded event loop, and handling thousands of client connections with efficient I/O multiplexing.
Redis achieves high speed because it is an in‑memory database, uses specialized data structures, runs a single‑threaded event loop, and employs I/O multiplexing.
Memory‑efficient data structures
Redis replaces C strings with Simple Dynamic Strings (SDS) that store length, free space, and a buffer, allowing O(1) length queries and preventing buffer overflows. The SDS header is defined as:
struct sdshdr {
long len; // used bytes (string length)
long free; // unused bytes
char buf[];
}SDS also supports multiple header types (sdshdr5, sdshdr8, sdshdr16, sdshdr32, sdshdr64) to minimize memory usage.
Other core structures include:
Linked lists for list objects.
Hash tables for the main key‑space and expiration tracking.
Skip lists for sorted sets, providing O(log n) look‑ups.
Integer sets ( intset ) for small integer collections, defined as:
typedef struct intset {
int32 encoding; // 16/32/64‑bit indicator
int32 length; // number of elements
int contents[];
}Redis resolves hash collisions with chained hashing and performs incremental rehashing to avoid blocking.
Single‑threaded execution
All command processing and key‑value operations run in a single thread, eliminating lock contention. Lengthy commands (big keys, complex operations, massive expirations, eviction, AOF sync, or full RDB sync) can block the server, but mechanisms such as lazy‑free and optional multi‑threaded I/O mitigate these issues.
I/O multiplexing
Redis uses the operating‑system’s multiplexing APIs (epoll, kqueue, evport, or select) to handle thousands of client sockets in one thread, allowing it to accept new connections and read/write data without blocking the event loop.
In summary, Redis’s speed stems from being an in‑memory store, employing compact and efficient data structures, a single‑threaded event model, and high‑performance I/O multiplexing.
IT Architects Alliance
Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.