Databases 18 min read

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

Redis achieves high performance by storing all data in memory, using compact data structures such as SDS, hash tables, skip‑lists and zip‑lists, running a single‑threaded event loop with I/O multiplexing, and applying optimisations like lazy‑free, progressive rehashing and optional multi‑threaded I/O for network traffic.

Top Architect
Top Architect
Top Architect
Why Redis Is Fast: In‑Memory Design, Specialized Data Structures, Single‑Threaded Execution and I/O Multiplexing

Redis is fast because it is an in‑memory database, all operations are performed directly on RAM without disk latency.

It uses specialised data structures:

Simple Dynamic Strings (SDS) replace C strings, storing length and free space for O(1) length queries, preventing buffer overflows and reducing reallocations.

Linked lists are used for lists when the number of elements is large.

Hash tables (dict) store key‑value pairs; Redis employs chained hashing and progressive rehashing to minimise pause times.

Skip‑lists implement ordered sets, providing O(log n) search.

Integer sets (intset) store small integer collections efficiently, upgrading the encoding when needed.

Compressed lists (ziplist) are used for small hashes and sorted sets, saving memory by storing entries in a contiguous byte array.

Key code examples:

struct sdshdr {
    long len;   // used bytes
    long free;  // free bytes
    char buf[]; // actual string
};
typedef struct intset {
    int32 encoding;   // 16/32/64‑bit
    int32 length;      // number of elements
    int<T> contents;   // element array
} intset;

Redis runs a single thread for network I/O and command execution, avoiding lock contention. It relies on I/O multiplexing (epoll/kqueue/evport) so one thread can monitor thousands of sockets simultaneously.

When the single thread becomes a bottleneck, Redis 6.0 introduces optional multi‑threaded I/O for reading/writing client data, while command processing remains single‑threaded.

Performance bottlenecks arise from big keys, complex commands, massive expirations, eviction policies, AOF fsync, and full‑sync RDB generation. Redis mitigates some of these with lazy‑free, progressive rehash, and configurable limits for ziplist size.

In summary, Redis combines an in‑memory model, compact and adaptive data structures, a lock‑free single‑threaded core, and efficient I/O multiplexing (with optional multi‑threaded network handling) to deliver extremely low latency and high throughput.

performanceRedisdata-structuresI/O multiplexingIn-Memory Databasesingle thread
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn 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.