Replication in Redis
Redis replication allows you to copy data from one Redis server (the master) to one or more other Redis servers (the replicas). This master-replica architecture is essential for building scalable and highly available systems.
In this setup, the master handles all write operations. Replicas receive a copy of the data and can serve read requests, which helps distribute the load and improve performance. When you set up a replica, it connects to the master and performs a full synchronization. The master sends a snapshot of its current data to the replica, which loads it into memory. After this initial sync, the master streams any new write commands to the replica, keeping the data up-to-date in real time.
Replication ensures that if one server fails, your data is still available on other nodes. In a failover scenario, if the master becomes unavailable, one of the replicas can be promoted to act as the new master. This process can be handled manually or automated using Redis Sentinel, which monitors servers and manages the failover process. Maintaining this architecture helps you avoid downtime and ensures that your Redis deployment remains responsive and reliable, even during unexpected outages.
Practical Example
Suppose you want to set up a Redis replica to increase data availability and distribute read traffic. You have two Redis servers running on the same network:
- Primary server: IP
192.168.1.10, port6379; - Replica server: IP
192.168.1.20, port6379.
Step 1: Connect to the Replica Server
Start by connecting to the replica server using the Redis CLI:
redis-cli -h 192.168.1.20 -p 6379
Step 2: Configure the Replica
Run the following command on the replica server to make it replicate the primary:
REPLICAOF 192.168.1.10 6379
- Purpose: This command tells the replica to start copying data from the primary at
192.168.1.10:6379. - Effect: The replica establishes a connection to the primary, receives a full data snapshot, and then keeps itself updated with any new changes.
Step 3: Verify Replication Status
Check the replication status on the replica:
INFO replication
- Purpose: This command displays the current replication role and status.
- Effect: You see details like
role:slave(orrole:replica) and the primary's address, confirming successful replication.
Step 4: Promoting the Replica (Optional)
If the primary server fails, you can promote the replica to become a standalone primary:
REPLICAOF NO ONE
- Purpose: This command breaks the replication link, making the replica a primary.
- Effect: The server stops replicating and accepts write commands directly.
Note: In Redis 5.0 and earlier, use SLAVEOF instead of REPLICAOF. The purpose and syntax are the same; only the command name differs.
By using these commands, you can flexibly manage Redis replication to support scaling and high availability in your deployments.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how Redis Sentinel automates failover?
What are the best practices for monitoring Redis replication?
How do I handle replication lag in Redis?
Awesome!
Completion rate improved to 9.09
Replication in Redis
Swipe to show menu
Redis replication allows you to copy data from one Redis server (the master) to one or more other Redis servers (the replicas). This master-replica architecture is essential for building scalable and highly available systems.
In this setup, the master handles all write operations. Replicas receive a copy of the data and can serve read requests, which helps distribute the load and improve performance. When you set up a replica, it connects to the master and performs a full synchronization. The master sends a snapshot of its current data to the replica, which loads it into memory. After this initial sync, the master streams any new write commands to the replica, keeping the data up-to-date in real time.
Replication ensures that if one server fails, your data is still available on other nodes. In a failover scenario, if the master becomes unavailable, one of the replicas can be promoted to act as the new master. This process can be handled manually or automated using Redis Sentinel, which monitors servers and manages the failover process. Maintaining this architecture helps you avoid downtime and ensures that your Redis deployment remains responsive and reliable, even during unexpected outages.
Practical Example
Suppose you want to set up a Redis replica to increase data availability and distribute read traffic. You have two Redis servers running on the same network:
- Primary server: IP
192.168.1.10, port6379; - Replica server: IP
192.168.1.20, port6379.
Step 1: Connect to the Replica Server
Start by connecting to the replica server using the Redis CLI:
redis-cli -h 192.168.1.20 -p 6379
Step 2: Configure the Replica
Run the following command on the replica server to make it replicate the primary:
REPLICAOF 192.168.1.10 6379
- Purpose: This command tells the replica to start copying data from the primary at
192.168.1.10:6379. - Effect: The replica establishes a connection to the primary, receives a full data snapshot, and then keeps itself updated with any new changes.
Step 3: Verify Replication Status
Check the replication status on the replica:
INFO replication
- Purpose: This command displays the current replication role and status.
- Effect: You see details like
role:slave(orrole:replica) and the primary's address, confirming successful replication.
Step 4: Promoting the Replica (Optional)
If the primary server fails, you can promote the replica to become a standalone primary:
REPLICAOF NO ONE
- Purpose: This command breaks the replication link, making the replica a primary.
- Effect: The server stops replicating and accepts write commands directly.
Note: In Redis 5.0 and earlier, use SLAVEOF instead of REPLICAOF. The purpose and syntax are the same; only the command name differs.
By using these commands, you can flexibly manage Redis replication to support scaling and high availability in your deployments.
Thanks for your feedback!