What you will learn
- How primary-replica replication works, and what
WAITgives you. - How Sentinel provides automatic failover.
- How Redis Cluster shards data into 16,384 hash slots, and how hash tags keep keys together.
Replication
A replica connects to a primary, receives a full snapshot, then streams every write. Replicas are read-only and replication is asynchronous: the primary acknowledges a write before replicas have it.
#!/usr/bin/env bash
# A primary and a replica on one machine: replication, read-only replicas and WAIT.
set -euo pipefail
P=${P:-6393}; R=${R:-6394}
DIR=$(mktemp -d)
redis-server --port "$P" --dir "$DIR" --dbfilename p.rdb --save "" --daemonize yes --logfile "$DIR/p.log"
redis-server --port "$R" --dir "$DIR" --dbfilename r.rdb --save "" --daemonize yes --logfile "$DIR/r.log" --replicaof 127.0.0.1 "$P"
until redis-cli -p "$R" INFO replication | grep -q 'master_link_status:up'; do sleep 0.2; done # initial sync
redis-cli -p "$P" SET greeting "hello from primary" >/dev/null
echo "WAIT (replicas that acked): $(redis-cli -p "$P" WAIT 1 1000)"
echo "replica reads: $(redis-cli -p "$R" GET greeting)"
echo "replica write: $(redis-cli -p "$R" SET x 1 2>&1)"
redis-cli -p "$P" INFO replication | grep -E '^(role|connected_slaves)'
redis-cli -p "$R" INFO replication | grep -E '^(role|master_link_status)'
redis-cli -p "$R" SHUTDOWN NOSAVE >/dev/null 2>&1 || true
redis-cli -p "$P" SHUTDOWN NOSAVE >/dev/null 2>&1 || true
rm -rf "$DIR"
Output:
WAIT (replicas that acked): 1
replica reads: hello from primary
replica write: READONLY You can't write against a read only replica.
role:master
connected_slaves:1
role:slave
master_link_status:up
WAIT numreplicas timeout blocks until that many replicas have acknowledged the previous writes. It reduces (but doesn’t eliminate) the chance of losing a write during failover. Reading from replicas scales reads, but those reads can be slightly stale.
Sentinel: automatic failover
Sentinel is a separate process (run at least three) that monitors a primary and its replicas:
- When a quorum of Sentinels agrees the primary is down, they elect a leader Sentinel, which promotes the best replica and reconfigures the others.
- Clients ask Sentinel for the current primary address instead of hard-coding it (redis-py has
redis.sentinel.Sentinel).
Sentinel gives high availability for one dataset that fits on one machine. It doesn’t shard.
Redis Cluster: sharding
Cluster splits the keyspace into 16,384 hash slots. Each key maps to CRC16(key) mod 16384, and each primary node owns a range of slots (with its own replicas for failover). Clients learn the slot map and send commands straight to the right node; a MOVED reply tells them when the map changed.
This Python function reproduces the slot calculation:
"""Redis Cluster key slots: CRC16(key) mod 16384, with {hash tags}."""
def crc16_xmodem(data: bytes) -> int:
crc = 0
for byte in data:
crc ^= byte << 8
for _ in range(8):
crc = ((crc << 1) ^ 0x1021) if crc & 0x8000 else crc << 1
crc &= 0xFFFF
return crc
def key_slot(key: str) -> int:
start = key.find("{")
if start != -1:
end = key.find("}", start + 1)
if end > start + 1: # non-empty tag: hash only what's inside the braces
key = key[start + 1:end]
return crc16_xmodem(key.encode()) % 16384
if __name__ == "__main__":
for k in ["user:42", "user:43", "cart:{user:42}", "orders:{user:42}", "{}weird"]:
print(f"{k:18} -> slot {key_slot(k)}")
Output, which matches CLUSTER KEYSLOT on a cluster-enabled Redis 8 node:
user:42 -> slot 15880
user:43 -> slot 11817
cart:{user:42} -> slot 15880
orders:{user:42} -> slot 15880
{}weird -> slot 7351
Hash tags: if a key contains {...} with something inside, only that part is hashed. cart:{user:42} and orders:{user:42} land in the same slot as user:42, so multi-key operations (MGET, transactions, Lua scripts touching both) work. Commands touching keys in different slots fail with CROSSSLOT. Empty braces {} don’t count as a tag.
Use tags deliberately: putting too much under one tag creates a hot slot.
Choosing a topology
| Need | Topology |
|---|---|
| Dev / small cache | Single instance |
| HA for data that fits on one node | Primary + replicas + Sentinel (or a managed service) |
| More data or writes than one node handles | Redis Cluster |
Managed services (cloud Redis offerings) handle failover and scaling for you, but the same concepts apply: replication lag, failover windows and slot-based key placement. For how Redis fits into larger architectures, see the System Design course.
Course wrap-up
You’ve covered the data structures, the patterns built on them (caching, rate limiting, queues, locks), atomicity (transactions and Lua), and running Redis safely (persistence, replication, Cluster). Next steps: build the rate limiter and chat system designs with real Redis behind them.
Key takeaways
- Replication is asynchronous; replicas are read-only and may be slightly stale;
WAITnarrows the loss window. - Sentinel monitors, agrees by quorum, and promotes a replica; clients discover the primary through it.
- Cluster maps keys to 16,384 slots via CRC16; hash tags keep related keys in one slot.
- Multi-key commands in Cluster need all keys in one slot.
Exercises
- Run the replication demo, then stop the primary and promote the replica with
REPLICAOF NO ONE. - Set up three Sentinels for the demo pair and watch a failover when you kill the primary.
- Create a 3-primary cluster locally with
redis-cli --cluster createand observe aMOVEDredirect withredis-cli(without-c).
Quiz
- Can a write acknowledged by the primary be lost after failover? Why?
- How many hash slots does Redis Cluster have, and how is a key’s slot chosen?
- Why would
MGET user:42 user:43fail in Cluster, and how do you fix it?
Show answers
- Yes. Replication is asynchronous, so the primary may fail before replicas receive the write;
WAITreduces this risk. - 16,384; the slot is CRC16 of the key (or its hash tag) modulo 16384.
- The keys hash to different slots (15880 and 11817), giving
CROSSSLOT; use hash tags such asuser:{42}andorders:{42}so related keys share a slot.