Redis · lesson 8 of 14
Beginner
4Intermediate
7- 5 MULTI/EXEC Transactions and the WATCH-Based Optimistic Lock
- 6 Pub/Sub: Channels, Patterns, and the Message That Never Arrives
- 7 RDB vs AOF: What Survives a Hard Kill and What Doesn't
- 8 Replication: A Read-Only Replica, and What WAIT Actually Guarantees
- 9 ACL: a User Restricted to One Key Pattern and One Command
- 12 maxmemory and Eviction: What Redis Throws Away When It's Full
- 13 Cache-Aside with Redis: Stale Reads, Races and Stampedes
Advanced
3Also part of Redis
3Replication: A Read-Only Replica, and What WAIT Actually Guarantees
Writing directly to a replica fails with READONLY by design — and WAIT doesn't error when it can't reach the number of replicas you asked for, it just tells you how many actually acknowledged in time.
By SkillByExample · Published
Two containers, one replicating from the other — and a couple of guarantees that turn out to be narrower than they sound.
Setting up a replica
docker network create redis-net
docker run -d --name redis-lesson --network redis-net -p 6379:6379 redis:8-alpine
docker run -d --name redis-replica --network redis-net -p 6380:6379 redis:8-alpine
docker exec redis-replica redis-cli REPLICAOF redis-lesson 6379
OK
docker exec redis-lesson redis-cli INFO replication | head -3
# Replication
role:master
connected_slaves:1
The master sees one connected replica. Writing to the master and reading from the replica confirms the link actually works:
docker exec redis-lesson redis-cli SET synced "from the master"
docker exec redis-replica redis-cli GET synced
OK
from the master
The bug: writing directly to the replica
docker exec redis-replica redis-cli SET direct "trying to write to replica"
READONLY You can't write against a read only replica.
replica-read-only is on by default precisely to prevent this — a replica is meant to be an
exact copy, and accepting its own independent writes would let it drift from the master
instead of mirroring it.
WAIT: not an error, just an honest count
docker exec redis-lesson redis-cli SET important "must be replicated"
docker exec redis-lesson redis-cli WAIT 1 1000
OK
1
WAIT 1 1000 asked for at least 1 replica to acknowledge, within 1000ms — got it immediately,
since the one replica was already caught up. Now ask for more replicas than actually exist:
docker exec redis-lesson redis-cli SET another "value"
time docker exec redis-lesson redis-cli WAIT 2 1000
OK
1
real 0m1.618s
WAIT 2 1000 can never reach 2 acknowledgments with only one replica connected — it waits out
the full ~1 second timeout (the real 0m1.618s confirms it actually blocked, not returned
instantly) and then reports 1, the real number that acknowledged. WAIT never raises an
error for “not enough replicas”; it always returns the true count it observed at the deadline,
and it’s the caller’s job to decide whether that count was good enough.
Promoting a replica
docker exec redis-replica redis-cli ROLE
slave
redis-lesson
6379
connected
281
docker exec redis-replica redis-cli REPLICAOF NO ONE
docker exec redis-replica redis-cli ROLE
OK
master
281
REPLICAOF NO ONE detaches the replica and makes it an independent master immediately,
keeping every write it had already replicated (offset 281, unchanged) — this is the command
a failover process runs once it’s decided the original master is gone for good.
Practice
1. Check a server's replication role without reading the full INFO output.
docker exec redis-lesson redis-cli ROLE
master
281
ROLE is the compact, script-friendly version of what INFO replication reports in prose —
for a master it’s role, offset, and a list of connected replicas (empty here, since the former
replica already promoted itself away).
2. Confirm the promoted former-replica now accepts writes of its own.
docker exec redis-replica redis-cli SET independent "no longer read-only"
docker exec redis-replica redis-cli GET independent
OK
no longer read-only
No READONLY error this time — once promoted via REPLICAOF NO ONE, it behaves exactly like
any other standalone master.
3. Check replication info from the (now-independent) former replica's own perspective.
docker exec redis-replica redis-cli INFO replication | head -3
# Replication
role:master
connected_slaves:0
role:master and connected_slaves:0 confirm it: fully independent now, no longer reporting
to or replicating from redis-lesson at all.
Next: ACL and security — users, selectors, and key-level read/write permissions.