Suggest an editImprove this articleRefine the answer for “What is replication in Redis? Why is replication needed?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)Replication in Redis is a mechanism where data is automatically copied from one server (the master) to other servers (replicas) in real time; the main goals are redundancy, read scaling, and fault tolerance. **Key point:** replication is asynchronous by default (the master doesn't wait for a replica's acknowledgment), and if a connection drops briefly, a replica can "catch up" with the master through the replication backlog without a full resync; automatic failover is handled by Redis Sentinel or Redis Cluster.Shown above the full answer for quick recall.Answer (EN)Image**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: ```javascript 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: 1. The replica sends the command: ```javascript REPLICAOF <master_ip> <port> ``` 2. The master creates an **RDB snapshot** and sends it to the replica. 3. 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`): ```bash replicaof 192.168.1.10 6379 ``` Checking the status: ```bash INFO replication ``` Shows: ```javascript role:master connected_slaves:2 slave0:ip=...,state=online ``` ## 6. Switching roles A replica can become a master: ```bash REPLICAOF NO ONE ``` This 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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.