Comprehensive Overview of Redis: Architecture, Data Structures, Persistence, Replication, Clustering and Advanced Features
This article provides an in‑depth technical guide to Redis, covering its single‑threaded model, core data structures (String, Hash, List, Set, ZSet) with practical commands, persistence mechanisms (RDB, AOF, hybrid), master‑slave replication, Sentinel and cluster architectures, cache eviction policies, memory management, progressive rehash, skiplist implementation, bitmap statistics, and consistency challenges when coupling Redis with MySQL.
Redis is an in‑memory key‑value store known for its speed. Although its network I/O and basic read/write operations run on a single thread, auxiliary tasks such as persistence, asynchronous deletion and cluster synchronization use additional threads.
Core data structures include String, Hash, List, Set and ZSet. Typical commands are:
SET key value GET key MSET user_name boom user_age 25 MGET user_name user_age INCR counter DECR counter INCRBY counter 10 SETNX lock_key valueHash is often used for object caching and shopping‑cart scenarios, e.g., HSET cart_123 product_456 2, HINCRBY cart_123 product_456 1, HGETALL cart_123.
List enables queue (FIFO) and stack (FILO) operations: LPUSH list_key value + RPOP list_key for queues, LPOP list_key for stacks, and BRPOP for blocking queues. It is also used for message‑push patterns such as publishing articles to a public account.
Set supports set algebra: SINTER set1 set2, SUNION set1 set2, SDIFF set1 set2. It powers use‑cases like social‑graph follow relationships.
ZSet (sorted set) provides ordered collections with scores, supporting commands like ZADD key score member, ZREM key member, ZRANGE key 0 -1 WITHSCORES, and is used for leaderboards and hot‑search rankings.
Persistence options are:
RDB – snapshotting to dump.rdb via SAVE (synchronous) or BGSAVE (asynchronous).
AOF – appends every write command to appendonly.aof. The appendfsync policy can be always, everysec (default) or no.
Hybrid – combines an RDB pre‑amble with incremental AOF writes, enabled by aof-use-rdb-preamble yes.
Replication and High Availability :
Master‑slave replication uses SYNC (pre‑2.8) or PSYNC (2.8+). The master creates an RDB snapshot, streams it to slaves, and buffers subsequent writes. Partial synchronization reuses a replication offset cache to avoid full resync when possible.
Sentinel monitors master instances, performs automatic failover, and elects a leader among sentinels. A minimum of three sentinel nodes is recommended for quorum‑based elections.
Redis Cluster shards the keyspace into 16384 slots, distributes slots across multiple master nodes, and uses a gossip protocol for metadata propagation. Clients receive slot mappings and are redirected with MOVED or ASK responses when accessing the wrong node.
Cache Eviction :
When maxmemory is reached, Redis applies policies such as noeviction, allkeys-lru, volatile-lru, allkeys-random, etc. It also runs a periodic lazy‑expiration scan that randomly checks 20 keys per cycle.
Progressive Rehash :
Redis dictionaries use two hash tables ( ht[0] and ht[1]) and migrate entries incrementally during CRUD operations, avoiding long pauses.
Skiplist Implementation (used by ZSet):
The skiplist consists of up to 64 levels, with random level assignment. Search, insertion and deletion run in O(log n) time. Example struct definitions are shown below:
struct zslnode {</code>
<code> string value;</code>
<code> double score;</code>
<code> zslnode* forwards[]; // multi‑level pointers</code>
<code> zslnode* backward; // back‑pointer</code>
<code>};</code>
<code>struct zsl {</code>
<code> zslnode* header;</code>
<code> int maxLevel;</code>
<code> map<string, zslnode*> ht; // hash table for O(1) lookup</code>
<code>};Search descends levels until the target is found; insertion chooses a random level and updates forward pointers; deletion removes the node from each level.
Bitmap Statistics :
Bitmaps store billions of bits efficiently (≈11 MB for 100 M users). Commands SETBIT key offset 1 and BITCOUNT key count daily active users. BITOP AND/OR/XOR/NOT combine multiple day‑bitmaps to compute weekly active users or continuous‑login users.
MySQL‑Redis Consistency Issues :
Dual‑writes can cause stale cache data. Solutions include adding TTLs, using read‑write locks, or employing change‑data‑capture tools like Alibaba Canal to stream binlog events to update Redis.
Overall, the article serves as a practical reference for engineers designing, tuning, and operating Redis in production environments.
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.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.
