Databases 18 min read

Why Redis Is Fast: In‑Memory Storage, Specialized Data Structures, Single‑Threaded Model, and I/O Multiplexing

Redis achieves exceptional speed by operating as an in‑memory database, leveraging compact data structures like SDS, linked lists, hash tables, skip lists and ziplists, running a single‑threaded event loop with I/O multiplexing, and employing optimizations such as lazy‑free, progressive rehashing, and multithreaded network I/O.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Why Redis Is Fast: In‑Memory Storage, Specialized Data Structures, Single‑Threaded Model, and I/O Multiplexing

Why Redis Is Fast

Redis achieves high performance because it stores all data in memory, uses highly optimized data structures, runs a single‑threaded event loop, and employs efficient I/O multiplexing.

Data Structures

Redis uses Simple Dynamic Strings (SDS) instead of C strings. SDS records the length and free space, enabling O(1) length queries, preventing buffer overflows, and reducing memory allocations. Different header types (sdshdr5, sdshdr8, sdshdr16, sdshdr32, sdshdr64) are selected based on string length to save memory.

struct sdshdr {
    long len;   // used bytes (excluding '\0')
    long free;  // unused bytes
    char buf[]; // actual string data
};

SDS also supports binary‑safe storage and compatibility with many C string functions.

Other core structures include:

Linked lists for list objects (double‑linked list implementation).

Hash tables for the main key‑space and expiration dictionary, using chain hashing and progressive rehashing.

Skip lists for sorted sets ( zset ), providing O(log n) search.

Integer sets ( intset ) for small collections of integers, automatically upgrading encoding when needed.

Compressed lists (ziplist) for small hashes and sorted sets, using fields such as zlbytes , zltail , zllen , and zlend to store data compactly.

typedef struct intset {
    int32 encoding; // 16, 32 or 64‑bit
    int32 length;   // number of elements
    int
contents; // array of integers
} intset;

Single‑Threaded Execution

Redis processes network I/O and command execution in a single thread, eliminating lock contention and simplifying concurrency control. Although a single thread can become a bottleneck for long‑running commands (big keys, complex operations, massive expirations, eviction, or synchronous AOF writes), Redis mitigates these issues with lazy‑free, progressive rehashing, and optional multithreaded I/O introduced in version 6.0.

I/O Multiplexing Model

Redis relies on the operating system’s I/O multiplexing (epoll, kqueue, evport, or select) to handle thousands of client connections with a single thread. The event loop receives file events, dispatches them to appropriate handlers, and processes commands sequentially.

Performance Bottlenecks and Mitigations

When the server faces very high concurrency, the single‑threaded read/write path can become limited by CPU, memory, or network bandwidth. Redis addresses this by:

Using lazy‑free to offload big‑key deletions to background threads.

Introducing multithreaded network I/O (read/write) while keeping command execution single‑threaded.

Optimizing memory allocation with SDS pre‑allocation and lazy free.

Conclusion

By combining in‑memory storage, efficient data structures, a single‑threaded event loop, and OS‑level I/O multiplexing, Redis delivers the high throughput and low latency that make it one of the fastest key‑value stores available.

performance optimizationRedisData StructuresI/O multiplexingIn-Memory DatabaseSingle Thread
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.