Databases 19 min read

What Makes Redis So Fast? Inside Its Data Structures and Single‑Threaded Design

Redis achieves remarkable speed by operating as an in‑memory database, leveraging specialized data structures like SDS, linked lists, hash tables, skip lists, and ziplists, while using a single‑threaded event loop with I/O multiplexing, incremental rehashing, and optimized memory management to minimize latency.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
What Makes Redis So Fast? Inside Its Data Structures and Single‑Threaded Design

Why Redis Is Fast

Redis query speed is fast because it is an in‑memory database, uses efficient data structures, runs single‑threaded, and employs I/O multiplexing.

Redis Data Structures

Redis stores data using a variety of specialized structures that are optimized for speed and memory usage.

1. Simple Dynamic Strings (SDS)

Redis uses Simple Dynamic Strings (SDS) instead of C char arrays to store strings and integers, providing O(1) length retrieval, preventing buffer overflows, reducing memory allocations, and supporting binary data.

struct sdshdr {
    // number of bytes used in buf
    long len;
    // number of unused bytes in buf
    long free;
    // byte array for the string
    char buf[];
};

After version 3.2, SDS chooses different header types (sdshdr5, sdshdr8, sdshdr16, sdshdr32, sdshdr64) to save memory.

SDS advantages over C strings

Constant‑time length retrieval (O(1) vs O(N)).

Automatic resizing prevents buffer overflow.

Pre‑allocation and lazy free reduce allocation frequency.

Binary‑safe storage.

2. Linked List

Redis list uses a doubly linked list for large or long‑string elements, providing fast insertion and deletion.

3. Dictionary (Hash Table)

Redis db contains core and auxiliary dictionaries; the main dict stores key‑value pairs, and an expires dict stores expiration times. Hash tables use chaining and incremental rehashing to handle collisions efficiently.

4. Skip List

Ordered sets are implemented with skip lists, offering O(log n) lookup performance.

5. Integer Set (intset)

Intset stores only integers efficiently; it upgrades its encoding (int16, int32, int64) as needed.

typedef struct intset{
    // encoding method: 16, 32, or 64 bit
    int32 encoding;
    // number of elements
    int32 length;
    // array of elements
    int<T> contents;
} intset;

6. Ziplist (Compressed List)

Ziplist is used for small lists and hashes, storing entries compactly to save memory. It consists of zlbytes, zltail, zllen, entries, and a terminating byte.

Why Single‑Threaded Can Still Be Fast

Redis handles network I/O and key‑value operations in a single thread, avoiding lock contention and simplifying concurrency.

High‑Performance I/O Model Based on Multiplexing

Redis uses the operating system’s I/O multiplexing (epoll, kqueue, etc.) so one thread can monitor many sockets, delivering events without blocking.

Single‑Threaded I/O Bottlenecks

Long‑running commands, big keys, massive expirations, eviction policies, AOF flushing, and full‑sync RDB generation can block the thread; Redis 4.0 introduced lazy‑free for big‑key cleanup, and Redis 6.0 added optional multi‑threaded I/O for client reads/writes.

Conclusion

Redis’s high performance stems from being an in‑memory database, using optimized data structures (hash tables, skip lists, SDS, etc.), a single‑threaded event loop, and I/O multiplexing.

References

Redis Core Technology and Practice – https://time.geekbang.org/column/intro/100056701

Why Redis 6.0 Introduced Multi‑Threading? – https://juejin.cn/post/7004683161695158309

Redis Design and Implementation – https://book.douban.com/subject/25900156/

SDS detailed analysis – https://blog.csdn.net/u010765526/article/details/89065607

Why Redis Queries Are Fast – https://boilingfrog.github.io/2022/01/07/为什么redis的查询比较快/

Redis knowledge points – https://github.com/boilingfrog/Go-POINT/tree/master/redis

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

redisI/O MultiplexingSingle‑threadedIn-Memory Database
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

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.