Why Redis Is So Fast: Memory‑Based Design, Efficient Data Structures, and Single‑Threaded I/O Model
This article explains how Redis achieves high performance by storing data in memory, using optimized data structures like SDS, linked lists, ziplists, hash tables, and skip lists, and employing an I/O multiplexing single‑threaded reactor model to minimize latency and overhead.
Redis’s speed primarily stems from its in‑memory implementation, which eliminates the disk I/O bottleneck that traditional disk‑based databases face. By keeping all data in RAM, Redis avoids costly read/write operations and can serve requests with minimal latency.
Efficient Data Structures
Redis supports multiple data types, each backed by specialized data structures designed for speed:
Simple Dynamic Strings (SDS) : Unlike C strings that require scanning for a terminating '\0', SDS stores the length explicitly, allowing O(1) length retrieval and reducing memory reallocations through pre‑allocation and lazy free strategies.
Doubly Linked List : Used for lists, providing O(1) access to head/tail nodes and constant‑time insertion/removal, with a stored len field for O(1) length queries.
Ziplist (Compressed List) : A compact, contiguous memory encoding for small lists or hashes, reducing memory overhead and improving traversal speed.
Hash Table (Dictionary) : Implements Redis’s key‑value store, offering O(1) average lookup and insertion.
Skip List : Powers sorted sets (zset), adding multi‑level pointers to a linked list to achieve O(log N) search complexity.
Appropriate Data Encoding
Each Redis data type selects an encoding based on size and content:
String: integers use integer encoding; otherwise raw encoding.
List: small lists use ziplist; larger ones switch to linked list.
Hash: small key/value strings use ziplist; otherwise hash table.
Set: integer elements or small sets use intset; larger sets use hash table.
Zset: small sorted sets use ziplist; otherwise skip list.
Suitable Thread Model
Redis achieves further performance gains through its I/O multiplexing and single‑threaded reactor architecture:
I/O Multiplexing : A single thread monitors many client sockets, queuing events and processing them sequentially, which avoids the overhead of context switches.
No Context Switching : By staying single‑threaded, Redis eliminates CPU time spent on thread scheduling, keeping operations on the same CPU core.
Reactor Model : Incoming requests are placed in a queue, dispatched by an event loop, and handled without spawning additional threads.
Summary
Memory‑based storage removes disk I/O latency.
Optimized data structures (SDS, linked list, ziplist, hash table, skip list) keep operation complexity low.
Dynamic encoding adapts structures to data size, saving memory.
The I/O multiplexing single‑threaded model minimizes context‑switch overhead and maximizes 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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
