Redis Clustering, Replication & High Availability
Master accepts all writes asynchronously and streams updates to one or more replicas via replication backlog buffer & offsets.
- Asynchronous replication: Master returns WRITE success immediately to client without waiting for replicas.
- Partial Resynchronization (PSYNC): Uses master_repl_offset and replication_id to sync missed stream entries after brief network split.
- Full Resynchronization: Master generates RDB snapshot and streams to replica when offset gap is too large.
replicaof 192.168.1.100 6379
INFO replication # Check master_link_status & offsetScaling Redis beyond a single standalone node requires choosing between two distinct architectural paradigms:
- Redis Sentinel: Provides automated failover and high availability for a single primary node with read-only replicas (vertical scale with automated recovery).
- Redis Cluster: Provides distributed horizontal sharding across multiple primary nodes using 16,384 hash slots with decentralized gossip-based failover.
Operating these architectures under high throughput requires mastering PSYNC 2.0 replication internals, replication backlog sizing formulas, split-brain data loss defenses, and cross-slot transaction constraints.
1. Master-Replica Asynchronous Replication Internals
Redis replication is fundamentally asynchronous. When a primary receives a write command, it writes to memory, appends to the replication backlog, and acknowledges the client before the write is propagated to replicas.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β REDIS MASTER-REPLICA REPLICATION ENGINE β
β β
β CLIENT MASTER (PRIMARY) REPLICA (SECONDARY) β
β ββββββββββ βββββββββββββββββββββββββββββββββ βββββββββββββββββββββ β
β β SET k vβ ββββββββββββ> β 1. Executes in Memory β β β β
β β β <ββββββββββββ β 2. Acks Client (Latency < 1ms)β β β β
β ββββββββββ β 3. Appends to Repl Backlog β β β β
β β Circular Buffer: 64MB β β β β
β βββββββββββββββββ¬ββββββββββββββββ β β β
β β β β β
β β Asynchronous β β β
β β Stream (RESP) β β β
β βΌ β β β
β βββββββββββββββββββββββββββββββββ β β β
β β Socket Output Buffer β β>β 4. Applies Delta β β
β βββββββββββββββββββββββββββββββββ β Offset += len β β
β βββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1.1 Partial Synchronization (PSYNC 2.0)
When a replica temporarily disconnects (e.g. transient network glitch), reconnecting using full database snapshots is catastrophically slow. Redis implements PSYNC 2.0:
- Replication ID (
master_replid): A 40-character pseudo-random string representing the primary's history epoch. - Replication Offset (
master_repl_offset): A monotonically increasing 64-bit byte counter representing the total stream bytes processed. - Secondary Replication ID (
master_replid2): When a replica is promoted to primary during failover, it setsmaster_replid2to the previous master's ID and records the offset where failover occurred. This allows other sibling replicas to partially sync with the newly promoted master without triggering a full RDB resynchronization!
# Handshake on Reconnect
REPLICA -> MASTER: PSYNC <master_replid> <offset>
# Case A: Offset exists within master's circular buffer
MASTER -> REPLICA: +CONTINUE <master_replid> (Streams only delta bytes)
# Case B: Offset fell off circular buffer
MASTER -> REPLICA: +FULLRESYNC <master_replid> <offset> (Triggers full RDB snapshot)
1.2 The Replication Backlog Sizing Formula
The primary maintains an in-memory circular ring buffer: the Replication Backlog (repl-backlog-size). If a disconnected replica cannot reconnect before its offset is overwritten in the ring buffer, Redis is forced into a Full Resynchronization (FULLRESYNC).
A full resync executes a fork (BGSAVE), writes a multi-gigabyte RDB snapshot to disk, saturates the network pipe, and causes the replica to flush its entire datasetβoften triggering cascading failovers.
EXAMPLE CALCULATION:
Peak write volume: 15,000 writes/second @ 1 KB average payload = 15 MB/second
Target resilience window: Survive a 120-second transient network partition
Required repl-backlog-size = 15 MB/s * 120s = 1.8 GB (Default 1 MB is catastrophically inadequate!)
1.3 Diskless Replication (repl-diskless-sync)
In environments with slow disk I/O (e.g. AWS EBS gp2/gp3 volumes):
- Standard replication forks
BGSAVE, writes the RDB file to disk, and then streams the file to the network socket. - Setting
repl-diskless-sync yesinstructs the primary process to spawn a child process that streams RDB serialized bytes directly into replica sockets without touching the local disk, eliminating disk I/O bottlenecks.
2. Redis Sentinel: Quorum-Based High Availability
Redis Sentinel is a distributed monitoring consensus cluster operating alongside Redis instances.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β REDIS SENTINEL FAILOVER CHOREOGRAPHY β
β β
β SENTINEL 1 SENTINEL 2 SENTINEL 3 β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Detects β β Confirms β β Confirms β β
β β SDOWN β ββββββββββ> β SDOWN β βββββββββββββ> β SDOWN β β
β ββββββββ¬ββββββββ ββββββββββββββββ ββββββββββββββββ β
β β β
β βΌ Quorum Reached (2 of 3) β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Transition to ODOWN (Objective Down) β β
β β Sentinels run Raft-like election to choose Leader Sentinel β β
β β Leader Sentinel executes Failover on Redis instances β β
β βββββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββ β
β β β
β βΌ β
β PROMOTION CRITERIA RANKING: β
β 1. Lowest `replica-priority` (0 = never promote) β
β 2. Highest replication offset (least data lost) β
β 3. Lowest run ID (deterministic tie-breaker) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
2.1 The Split-Brain Data Loss Hazard
A classic failure mode occurs when a network partition separates the Master from the Sentinels:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β THE PARTITION SPLIT-BRAIN FAILURE SCENARIO β
β β
β PARTITION A (Isolated Minority) PARTITION B (Majority Quorum) β
β ββββββββββββββββββββββββββββββββββ βββββββββββββββββββββββββββββββββββ β
β β Rogue Client writes to Old M1 β β Sentinels (2 of 3) reach quorum β β
β β (M1 still thinks it is Master) β β Promote Replica R1 -> New M2 β β
β β M1 executes writes in memory β β App Client 2 writes to New M2 β β
β ββββββββββββββββββββββββββββββββββ βββββββββββββββββββββββββββββββββββ β
β β
β WHEN PARTITION HEALS: β
β M1 is demoted to a replica of M2. β
β M1 flushes its entire dataset to sync with M2! β
β ALL WRITES EXECUTED BY CLIENT 1 ON M1 ARE PERMANENTLY DESTROYED! β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Production Defense: Guarding Write Quorumβ
Configure Redis to reject writes if it loses communication with replicas:
# redis.conf on Master nodes
# Require at least 1 replica acknowledged within 10 seconds to allow writes
min-replicas-to-write 1
min-replicas-max-lag 10
If a network partition isolates the primary, after 10 seconds it will reject all subsequent write commands with an error (NOREPLICAS), preventing rogue writes that would later be overwritten.
3. Redis Cluster: Horizontal Sharding across 16,384 Hash Slots
Redis Cluster partitions the dataset horizontally across multiple primary nodes using a deterministic hash slot algorithm:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β REDIS CLUSTER HASH SLOT TOPOLOGY β
β β
β NODE 1 (Primary) NODE 2 (Primary) NODE 3 (Primary) β
β Slots: 0 β 5460 Slots: 5461 β 10922 Slots: 10923 β 16383 β
β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ β
β β Handles 1/3 keysβ β Handles 1/3 keysβ β Handles 1/3 keysβ β
β ββββββββββ¬βββββββββ ββββββββββ¬βββββββββ ββββββββββ¬βββββββββ β
β β Replication β Replication β Replication β
β βΌ βΌ βΌ β
β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ β
β β Node 1 Replica β β Node 2 Replica β β Node 3 Replica β β
β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ β
β β
β CLUSTER BUS: Gossip Protocol over port 16379 (Node PING/PONG, Fail Detection) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
3.1 Why Exactly 16,384 Slots?
While CRC16 can generate unique values, Redis deliberately constrains slots to 16,384:
- Heartbeat Packet Bitmap Overhead: Nodes continually gossip their slot allocation using a bitmap. A 16,384-slot bitmap takes exactly (). A 65,536-slot bitmap would require 8 KB per heartbeat packet, saturating cluster network interfaces.
- Cluster Scale Limits: Redis clusters rarely exceed 1,000 master nodes. 16,384 provides roughly 16 slots per node at maximum scale, preserving optimal distribution.
3.2 The Hash Tag Mechanism ({hashtag})
By default, operations spanning multiple keys (such as transactions MULTI/EXEC or MGET) fail with a CROSSSLOT Keys in request don't hash to the same slot error if keys map to different slots.
- The Solution: Wrap the co-locating identifier in curly braces
{}:# Key 1: user:{1004}:profile -> CRC16 runs ONLY on "1004" -> Slot 8812# Key 2: user:{1004}:orders -> CRC16 runs ONLY on "1004" -> Slot 8812# Now atomic Lua scripts and multi-key commands execute safely on the same node!
3.3 Redirection Mechanics: MOVED vs. ASK
When a client contacts a cluster node with a key that belongs elsewhere:
MOVED <slot> <ip>:<port>: Permanent redirect. The slot has moved permanently. Smart clients (e.g. Lettuce, Jedis) intercept this, execute the command on the target node, and update their local client-side slot routing cache table.ASK <slot> <ip>:<port>: Temporary redirect during active online slot migration. The client must send anASKINGcommand to the target node, followed immediately by the query, but does not update its permanent slot cache.
4. Architectural Comparison: Standalone vs. Sentinel vs. Cluster
| Dimension | Standalone Redis | Redis Sentinel | Redis Cluster |
|---|---|---|---|
| Primary Focus | Simplicity, caching | High Availability (Automated failover) | Horizontal write scalability & HA |
| Max Memory Capacity | Limited to single host RAM | Limited to single host RAM | Aggregated RAM across all masters (TBs) |
| Failover Mechanism | Manual human intervention | Sentinels achieve quorum consensus | Decentralized node gossip voting |
| Multi-Key Operations | Fully supported | Fully supported | Supported only if keys share {hashtag} |
| Network Architecture | 1 port (6379) | 2 tiers (6379 for Redis, 26379 for Sentinel) | 2 ports per node (6379 client, 16379 bus) |
| Client Complexity | Simple single-connection client | Sentinel-aware connection pool | Smart routing client with slot caching |
