Why Redis Is Lightning Fast: Deep Dive into Its Core Architecture
This article explores why Redis delivers ultra‑high performance by examining its in‑memory design, single‑threaded event loop, efficient data structures such as hash tables, SDS strings, ziplist, quicklist, and skiplist, as well as its encoding strategies and I/O multiplexing model.
Learning a technology often means encountering scattered points without a complete mental framework; this article builds a systematic view of Redis to understand its speed.
Redis Panorama
The panorama is divided into two dimensions: application (caching, clustering, clever data‑structure usage) and system (high performance, high availability, high scalability).
High performance: thread model, network I/O model, data structures, persistence mechanisms.
High availability: master‑slave replication, Sentinel, Cluster sharding.
High scalability: load balancing.
The Secret of "Speed Unbreakable"
Redis is fast because it is entirely memory‑based and uses a single‑threaded model for command execution, avoiding many overheads of multi‑threading.
Pure Memory Implementation
All read/write operations occur in RAM, giving Redis a massive speed advantage over disk‑based databases.
Memory is directly controlled by the CPU, providing optimal bandwidth.
Efficient Data Structures
Redis supports five primary data types (String, List, Hash, Set, Sorted Set), each backed by specialized low‑level structures.
String : cache, counters, distributed locks.
List : linked list, queue, timeline.
Hash : user info, hash tables.
Set : deduplication, likes, mutual friends.
Zset : ranking, click counts.
Under the hood, Redis uses six core structures:
Redis Hash Dictionary
All keys are stored in a global hash table with O(1) lookup. Collisions are resolved with chaining and progressive rehashing to avoid blocking.
Allocate a larger second hash table.
Gradually migrate entries from the first to the second.
Release the first table.
SDS (Simple Dynamic String)
SDS stores the string length, allowing O(1) length retrieval, pre‑allocation of extra space, lazy free space, and binary safety.
struct ziplist<T> { int32 zlbytes; int32 zltail_offset; int16 zllength; T[] entries; int8 zlend; }Ziplist (Compressed List)
Used for List, Hash, and Sorted Set when data is small; provides O(1) access to head/tail and O(N) for other elements.
Quicklist
A hybrid of linked list and ziplist, where each segment is a ziplist linked together, further improving performance.
Skiplist
Implements ordered access for Sorted Sets with average O(log N) lookup.
Intset (Integer Set)
Optimized for small integer collections, storing elements in a compact array.
Encoding Strategies
Redis objects (redisObject) contain type, encoding, and a pointer to the underlying structure. Different data types choose the most efficient encoding based on size and count thresholds (e.g., ziplist vs. hashtable for Hash, intset vs. hashtable for Set).
Single‑Threaded Model
Redis handles network I/O and command execution in a single thread, while persistence, replication, and asynchronous deletion run in background threads.
Why Not Multi‑Threaded?
Multi‑threading introduces context‑switch overhead, lock contention, and increased complexity.
Benefits of Single Thread
No thread‑creation overhead.
No context‑switch CPU cost.
Avoids lock contention and deadlocks.
Simpler, clearer code.
The bottleneck is often memory size or network bandwidth, not CPU.
I/O Multiplexing Model
Redis uses epoll‑based event multiplexing to handle many connections in one thread, converting accept, recv, send, and close into events.
Basic I/O flow: accept → recv → parse → execute → send. Blocking I/O would stall the single thread, so Redis employs non‑blocking I/O with epoll to avoid this.
Summary of Why Redis Is Fast
Pure in‑memory operations eliminate disk latency.
Global hash table with O(1) lookups and progressive rehashing.
Non‑blocking I/O via epoll and event‑driven architecture.
Single‑threaded execution ensures atomicity and avoids context switches.
Optimized data structures (hash, SDS, ziplist, quicklist, skiplist) accelerate access.
Adaptive encoding selects the most efficient representation per data type.
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.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
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.
