Why Is Redis So Fast? A Deep Dive into Its Data Structures and Architecture
This article explains why Redis achieves high query performance by leveraging in‑memory storage, specialized data structures such as SDS, linked lists, dictionaries, ziplists and skiplists, a single‑threaded event loop with I/O multiplexing, and various optimization techniques that avoid common bottlenecks.
Why Redis Is Fast
Redis delivers extremely low latency because it stores all data in memory, uses compact and purpose‑built data structures, runs a single‑threaded event loop, and relies on efficient I/O multiplexing.
Key Data Structures
Simple Dynamic Strings (SDS) replace C‑style char arrays. SDS stores the length of the string, enabling O(1) length queries, automatic buffer expansion, binary‑safety, and protection against buffer overflows. After version 3.2, SDS selects one of five header types (sdshdr5, sdshdr8, sdshdr16, sdshdr32, sdshdr64) to minimize memory usage.
Linked List – Redis lists use a doubly linked list when the list contains many or long elements.
Dictionary (hash table) – Each Redis database has a primary dict for key‑value pairs and an expires dict for expiration times. Non‑core dicts manage blocking keys, ready keys, and watch keys. Redis resolves hash collisions with chaining.
Skiplist – Ordered sets (zset) are implemented with skiplists, providing O(log n) search, insertion, and deletion by maintaining multiple forward pointers per node.
Integer Set (intset) – Used for small sets of integers; it upgrades the internal representation when a larger integer type is needed, improving flexibility and memory efficiency.
Ziplist (compressed list) – A memory‑compact representation for small lists and hashes. It stores metadata (zlbytes, zltail, zllen) and entries with variable‑length fields. Ziplist entries include previous_entry_length, encoding, and content. Because ziplists are contiguous, inserting many elements may trigger costly reallocations, and they are unsuitable for large collections.
Single‑Threaded Execution
Redis processes all network I/O and command execution in a single thread, eliminating lock contention and simplifying concurrency control. Although a single thread cannot exploit multiple CPU cores for command processing, it avoids the overhead of lock management.
I/O Multiplexing Model
Redis uses the operating system’s I/O multiplexing (epoll, kqueue, evport, or fallback to select) to monitor many sockets with one thread. File events are dispatched to handlers that process reads, writes, accepts, and closures. This model allows Redis to handle tens of thousands of connections efficiently.
Performance Bottlenecks and Mitigations
Even with a single thread, certain operations can block the server:
Big keys that require long copy or delete times.
Commands with high computational complexity.
Massive key expiration processing.
Eviction policies when memory limits are reached.
Always‑on AOF persistence.
Full RDB snapshots during replication.
Redis 4.0 introduced lazy‑free to offload big‑key deletions to background threads, and Redis 6.0 added optional multithreaded I/O for client read/write while keeping command execution single‑threaded.
Conclusion
By combining in‑memory storage, carefully designed data structures, a lock‑free single‑threaded event loop, and OS‑level I/O multiplexing, Redis achieves the high performance that makes it a popular choice for caching, real‑time analytics, and many other latency‑sensitive workloads.
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.
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.
