Why Redis Is So Fast: Inside Its Core Architecture
This article explains why Redis achieves exceptional speed by examining its memory‑only architecture, single‑threaded event loop, and the specialized data structures—global hash tables, SDS strings, ziplist, quicklist, skiplist, and intset—that provide O(1) or O(log N) operations, along with non‑blocking I/O multiplexing.
Redis Overview
Redis is an in‑memory key‑value store whose speed stems from several design choices. It stores all data in RAM, uses a single‑threaded event loop, and employs specialized data structures that give O(1) or O(log N) complexity for common operations.
Memory‑only operation vs disk
Because reads and writes happen directly in memory, Redis avoids the latency of disk I/O. The CPU accesses memory through its internal memory controller, providing the highest possible bandwidth. Benchmarks from the official Redis repository show up to 100 000 QPS on a single instance.
Core data structures
Redis supports five logical data types—String, List, Hash, Set, Sorted Set—each backed by one or more low‑level structures:
String: raw byte array or integer encoding.
List: linked list or ziplist (later replaced by quicklist).
Hash: hash table or ziplist.
Set: intset for small integer sets or hash table.
Sorted Set: skiplist (zkiplist) or ziplist for tiny sets.
Global hash table
All keys are stored in a global hash table whose lookup is O(1). When the table grows, Redis performs incremental rehashing to avoid long pauses.
Simple Dynamic String (SDS)
SDS wraps a C string with a length field, allowing O(1) length queries, pre‑allocation of spare space, lazy freeing of unused bytes, and binary‑safe storage.
struct sds {
int len; // used length
int free; // free space at the end
char buf[]; // actual bytes, null‑terminated
}Compressed list (ziplist)
Ziplist stores a sequence of entries in a contiguous memory block, suitable for short strings or small integers. It provides O(1) access to the head and tail but O(N) for interior elements.
struct ziplist<T> {
int32 zlbytes;
int32 zltail_offset;
int16 zllength;
T[] entries;
int8 zlend;
}Quicklist
Quicklist combines linked‑list nodes with embedded ziplist segments, giving the memory efficiency of ziplist and the O(1) head/tail operations of a linked list.
Skiplist
Sorted sets use a skiplist (zkiplist) that provides average O(log N) search and ordered traversal.
Integer set (intset)
Intset stores only integer elements in a sorted array, used by Set when all members are integers and the set is small.
typedef struct intset {
uint32_t encoding;
uint32_t length;
int8_t contents[];
} intset;Object encoding
Each key/value is represented by a redisObject structure that records the logical type, the chosen encoding, and a pointer to the underlying data structure. Encoding decisions are driven by size thresholds defined in redis.conf (e.g., list-max-ziplist-entries 512).
typedef struct redisObject {
unsigned type:4;
unsigned encoding:4;
void *ptr;
} robj;Single‑threaded event loop
Redis processes network I/O and command execution in a single thread, avoiding context‑switch overhead and lock contention. Persistence, replication and asynchronous deletion run in background threads, but the core request path remains single‑threaded.
I/O multiplexing
The server uses epoll (or select/poll on older platforms) to monitor many sockets with one thread. Events such as accept, read, write, and close are turned into callbacks, eliminating blocking I/O and allowing thousands of concurrent connections.
Why the “fast and unbreakable” myth holds
Combining memory‑only storage, O(1) hash lookups, incremental rehashing, compact encodings (SDS, ziplist, intset), skiplist‑based sorted sets, a single‑threaded event loop, and non‑blocking epoll gives Redis its characteristic low latency and high throughput.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
dbaplus Community
Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.
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.
