Why Redis Is Fast: In‑Memory Storage, Specialized Data Structures, Single‑Threaded Design, and I/O Multiplexing
This article explains why Redis delivers high query speed by leveraging its in‑memory architecture, custom data structures such as SDS, linked lists, hash tables, skip‑lists, and intsets, a single‑threaded execution model combined with efficient I/O multiplexing, and recent multithreaded I/O enhancements.
Redis achieves remarkable query speed primarily because it operates as an in‑memory database, eliminating disk latency for most operations.
Key factors include:
Memory‑resident storage.
Optimized data structures: Simple Dynamic Strings (SDS) replace C strings, providing O(1) length access, buffer‑overflow protection, and reduced allocation overhead; linked lists, hash tables (dict), skip‑lists for sorted sets, integer sets (intset), and ziplist for compact list/hash storage.
Single‑threaded execution avoids lock contention, while I/O multiplexing (epoll/kqueue/evport) enables a single thread to handle thousands of client connections efficiently.
Data structure details:
struct sdshdr {
long len; // used bytes (string length)
long free; // unused bytes
char buf[]; // actual data
};SDS variants (sdshdr5, sdshdr8, sdshdr16, sdshdr32, sdshdr64) adapt size fields to save memory, and strategies like pre‑allocation and lazy free reduce allocation frequency and prevent buffer overflows.
Redis also uses hash tables with progressive rehashing, skip‑lists offering O(log n) lookups, and integer sets that upgrade encoding based on element size.
Although Redis is single‑threaded for command processing, it employs I/O multiplexing to serve many clients; starting with Redis 6.0, a multithreaded I/O path was added to parallelize client read/write while keeping command execution single‑threaded.
Potential performance bottlenecks arise from long‑running commands, big keys, massive expirations, eviction policies, and synchronous AOF flushing. Mitigations include lazy‑free for big keys, careful command design, and configuring eviction thresholds.
In summary, Redis’s high performance stems from its in‑memory design, specialized data structures, single‑threaded command processing, and efficient I/O multiplexing, complemented by optional multithreaded I/O in newer versions.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
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.