Redis Data Structures Under the Hood
Exploring how Redis implements its data structures — from simple dynamic strings to skip lists and hash tables — and why these choices matter for performance.
What I Learned
Redis isn't just a key-value store — it's a data structure server. Understanding the implementation behind each type reveals why certain operations have surprising performance characteristics.
Strings (SDS - Simple Dynamic Strings)
- Redis doesn't use C strings. SDS pre-allocates space and tracks length, making
APPENDO(1) amortized instead of O(n). - Binary safe — can store any bytes, not just text.
Hash Tables
- Redis uses chained hashing with incremental rehashing. When a hash table needs to grow, Redis creates a new table and migrates entries gradually across multiple commands, avoiding a single expensive resize operation.
Sorted Sets (Skip Lists)
- The most interesting data structure. A skip list provides O(log n) insert, delete, and search — similar to a balanced BST but simpler to implement and more cache-friendly.
- Redis uses a skip list + hash table combination for sorted sets, enabling both score-based range queries and O(1) member lookups.
Streams
- Radix tree-based storage for efficient time-series data with consumer groups inspired by Kafka's design.
Connection to My Work
My distributed cache project uses similar patterns. Understanding Redis's approach to incremental rehashing influenced my design for handling hash table resizes without blocking client requests.
Still Learning
I'm currently studying Redis Cluster's gossip protocol and slot migration mechanism for resharding. This connects directly to my distributed cache project's need for online rebalancing.