Replication
Replication streams writes from master to one or more replicas — read scaling and failover candidates.
Introduction
Replication streams writes from master to one or more replicas — read scaling and failover candidates. Replicas are async by default; a read immediately after write on replica may be stale ( eventual consistency ).
PSYNC partial resync avoids full RDB transfer on brief disconnect. Chain replication (replica of replica) adds lag — prefer star topology from master.
Never run slow commands or KEYS on replicas serving production reads without understanding lag impact.
Understanding the topic
Key concepts
- REPLICAOF host port — configure replica.
- Async replication — master doesn't wait replica ack (unless WAIT).
- INFO replication — lag, offset, role.
- READONLY on replica rejects writes.
- Replica serves reads to offload master.
- WAIT numreplicas timeout — sync wait rare.
flowchart LRMaster -->|stream| Replica1Master -->|stream| Replica2
Step-by-step explanation
- Replica connects sending REPLCONF.
- Full sync: master BGSAVE RDB to replica.
- Partial sync: backlog offset catch-up.
- Master propagates command stream to replicas.
- Replica applies commands in order on event loop.
Syntax reference
Common commands
- master_repl_offset vs slave repl offset — lag metric.
- READONLY default on replicas.
- Use WAIT only when strong need — hurts latency.
INFO replicationROLEREPLICAOF redis-master 6379CONFIG SET replica-read-only yes
Informative example
Check replication lag during incident:
redis-cli INFO replication# master_repl_offset:1000000# slave_repl_offset:999500 ← 500 bytes behindredis-cli -h replica.internal GET hot:key
Large lag — network, slow replica CPU, or huge write burst. Don't read-your-writes from replica without routing logic.
Real-world use
Real-world use cases
- Read scaling for cache-heavy GET traffic.
- Hot standby for manual promotion.
- Analytics SCAN on replica spare master.
- Cross-AZ read local replica.
- Backup BGSAVE from replica node.
Best practices
- Monitor repl lag alert >10MB or seconds.
- Route sticky read-after-write to master.
- Dedicated replicas for backup not prod reads.
- min-replicas-to-write on master prevent split-brain writes.
- Test promote replica drill.
- Avoid KEYS on lagging replica.
Common mistakes
- Read-your-writes from replica after POST.
- Writing to replica — fails or breaks topology.
- Ignoring lag during incident debugging.
- Too many chained replicas.
Advanced interview questions
Q1BeginnerReplication async?
Q2BeginnerReplica purpose?
Q3IntermediateReplication lag causes?
Q4IntermediateWAIT command?
Q5AdvancedPromote replica to master?
Summary
Master streams writes to replicas async.