Databases 19 min read

The Secrets Behind Redis’s Speed: Architecture, Data Structures, and Single‑Threaded Model

This article explains why Redis is exceptionally fast by detailing its in‑memory design, global hash table with O(1) lookups, incremental rehashing, specialized data structures such as SDS, ziplist, quicklist, skiplist and intset, as well as its single‑threaded event loop and epoll‑based I/O multiplexing.

Wukong Talks Architecture
Wukong Talks Architecture
Wukong Talks Architecture
The Secrets Behind Redis’s Speed: Architecture, Data Structures, and Single‑Threaded Model

Redis Overview

Redis is an in‑memory key‑value store that achieves high performance through a combination of memory‑based operations, a single‑threaded event loop, and carefully chosen data structures.

Three‑High Principles

Performance, high availability, and scalability are addressed by thread model, network I/O model, data structures, persistence mechanisms, replication (master‑slave, sentinel, cluster), and load balancing.

Why Redis Is Fast

It runs entirely in memory, avoiding disk I/O bottlenecks; the core hash table provides O(1) lookups; specialized structures such as SDS strings, ziplist, quicklist, skiplist, and intset optimize storage and access for different data types.

Hash Table

All keys are stored in a global hash table with O(1) access; rehashing is performed incrementally to prevent blocking.

SDS (Simple Dynamic String)

SDS stores the string length separately, allowing O(1) length queries, pre‑allocation of unused space, lazy free of memory, and binary‑safe storage.

Compressed List (ziplist)

Used for small lists, hashes, and sorted sets; stores entries compactly with header fields for quick access. Example definition:

struct ziplist<T> {
    int32 zlbytes; // total bytes of the ziplist
    int32 zltail_offset; // offset of the last entry
    int16 zllength; // number of entries
    T[] entries; // array of entries
    int8 zlend; // end marker (0xFF)
}

Quicklist

Combines linked‑list nodes with ziplist segments to provide fast list operations while saving memory.

Skiplist

Implements ordered sets with average O(log N) lookup and supports range queries efficiently.

Integer Set (intset)

Optimized for sets of integers, storing them in a sorted array. Example definition:

typedef struct intset {
    uint32_t encoding; // encoding type
    uint32_t length;   // number of elements
    int8_t contents[]; // array of integers
} intset;

Encoding Strategies

Redis chooses the most efficient internal representation based on data size and type, switching between ziplist, hashtable, intset, and zkiplist according to configurable thresholds (e.g., list‑max‑ziplist‑entries, zset‑max‑ziplist‑entries).

Single‑Threaded Event Loop

The network I/O and command execution run in a single thread, eliminating context‑switch overhead and lock contention; persistence, cluster synchronization, and asynchronous deletion use auxiliary threads.

I/O Multiplexing

Redis uses epoll (or select/poll) to monitor many sockets within the single thread, achieving non‑blocking, high‑concurrency handling. Typical request handling steps:

accept a new connection ( accept )

read request data ( recv )

parse the command ( parse )

execute the command (e.g., get )

send the response back to the client

Summary

Redis’s speed stems from pure memory access, O(1) hash lookups, incremental rehashing, optimized data structures, single‑threaded atomic execution, and efficient I/O multiplexing.

performanceRedisdata structuresI/O multiplexingsingle-threadedhash tableIn-Memory Database
Wukong Talks Architecture
Written by

Wukong Talks Architecture

Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.

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.