What is replication in Redis? Why is replication needed?
Replication in Redis is a mechanism where data is automatically copied from one server (the master) to other servers (replicas). The main goal is redundancy, read scaling, and fault tolerance.
1. What replication is
Redis lets you build a hierarchy of servers:
Master → Replica1
→ Replica2- Master, the main node, where all write operations go (
SET,DEL,INCR, etc.). - Replica (Slave), a copy of the master, receiving all its updates and staying in sync.
A replica updates in real time, every change that happens on the master is automatically streamed to the replicas.
2. Why replication is needed
1. Fault tolerance
If the master fails, a replica holds a full copy of it and can quickly become the new master. This avoids losing data, and clients can simply switch to the new node.
2. Read scaling
Reads (GET, HGETALL, ZRANGE) can be spread across several replicas, reducing load on the master.
That's how horizontal scaling for read operations is achieved.
3. Backups
A replica can persist data to disk independently of the master (RDB/AOF), giving you a safe backup with no load on the primary system.
3. How replication works in Redis
Initial synchronization
When a new replica connects to the master:
- The replica sends the command:
REPLICAOF <master_ip> <port>- The master creates an RDB snapshot and sends it to the replica.
- The replica loads that snapshot into memory and starts applying the commands that arrive after the snapshot's point in time.
Ongoing (incremental) synchronization
After the initial load, the master sends the replica every new write command, in the same order it executes them itself. That's what keeps the state fully in sync.
On brief disconnects
Redis uses a replication backlog, a ring buffer of recent changes. If a replica briefly loses its connection, it can catch up with the master by reading the missing commands from that log, with no full resync needed.
4. Types of replication
| Type | Description |
|---|---|
| Asynchronous (the default) | the master doesn't wait for the replica's acknowledgment, maximally fast, but data can be lost on a failure |
| Semi-synchronous (PSYNC2) | the replica can acknowledge receiving the data, improving reliability |
5. Configuration
In a replica's config file (redis.conf):
replicaof 192.168.1.10 6379Checking the status:
INFO replicationShows:
role:master
connected_slaves:2
slave0:ip=...,state=online6. Switching roles
A replica can become a master:
REPLICAOF NO ONEThis is used during a failover or in automated clusters (e.g. Redis Sentinel or Redis Cluster).
7. Replication and performance
- Replication doesn't slow down the master, since it's asynchronous.
- Replicas read data from their own memory, without disturbing the master.
- Network load grows with a large number of replicas, so a limit,
repl-backlog-size, is used.
8. Tools for managing it
- Redis Sentinel, an automatic failover system: it watches the master and promotes a replica to master on failure.
- Redis Cluster, a distributed system where every node can be a master with its own replicas.
9. Summary
Replication in Redis is:
- a mechanism for copying data from a master to replicas,
- that provides reliability, read scaling, and resilience to failures.
It's needed to keep Redis available and consistent even under load or failures, with no single point of failure.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.