Comprehensive Guide to Redis: Architecture, Data Structures, Persistence, Clustering, and Advanced Features
This article provides an in‑depth overview of Redis, covering its single‑threaded design, core data structures, persistence mechanisms (RDB, AOF, hybrid), master‑slave replication, Sentinel and Cluster architectures, eviction policies, progressive rehash, bitmap usage for massive analytics, skiplist implementation, and strategies for handling MySQL‑Redis write inconsistencies.
Redis is a fast, in‑memory key‑value store whose performance stems from a single‑threaded network I/O model and memory‑level operations.
Core Data Structures : Redis supports five primary types—String, Hash, List, Set, and ZSet—each with common use cases such as simple caching, object caching, counters, distributed locks, and social‑graph modeling. Example commands include set key value, hmset user name boom age 25, and sinter set1 set2 set3.
Persistence : Redis offers three persistence options. RDB creates binary snapshots (e.g., save 60 1000), AOF logs every write command (configurable fsync policies), and hybrid persistence combines RDB snapshots with incremental AOF writes for faster restarts.
Replication and High Availability : Master‑slave replication uses SYNC/PSYNC commands and background saves. Sentinel monitors masters, performs leader election, and handles failover. Redis Cluster shards data across 16384 slots, uses CRC16 hashing for slot calculation, and relies on a gossip protocol for metadata propagation.
Eviction Policies : When memory limits are reached, Redis can evict keys based on policies such as volatile‑LRU, allkeys‑LFU, etc., with both periodic and lazy expiration mechanisms to prevent cache snowball effects.
Progressive Rehash : To avoid blocking during hash table resizing, Redis incrementally rehashes entries from the old table to a new one during regular CRUD operations, temporarily using two tables and ensuring memory usage spikes are managed.
Bitmap Analytics : Bitmaps store billions of bits efficiently; commands like setbit key user_id 1 and bitcount key enable fast daily active user counts, while BITOP AND/OR combine multiple days for weekly or continuous‑login statistics.
Skiplist (ZSet) Implementation : Redis ZSets combine a hash map with a skiplist for ordered score access. The skiplist uses random level assignment (probability 0.5 per level) and supports O(log n) search, insertion, and deletion. Sample struct definitions are shown in code blocks.
MySQL‑Redis Double‑Write Consistency : In high‑concurrency scenarios, write‑through caching can cause stale data. Solutions include short TTLs, read‑through patterns, distributed locks, or using binlog listeners like Alibaba’s Canal to synchronize cache updates.
Overall, the article equips readers with practical Redis knowledge—from basic commands to advanced clustering and performance tuning—enabling effective use of Redis in modern backend systems.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
