Replication helps copy data from one Redis server to another. This keeps data safe and allows sharing work between servers.
Replication configuration in Redis
Introduction
Syntax
Redis
slaveof <masterip> <masterport>
This command sets the current Redis server as a replica (slave) of the master server.
Use replicaof instead of slaveof in Redis 5.0 and later for clearer naming.
Examples
Redis
replicaof 192.168.1.100 6379
Redis
replicaof no one
Sample Program
This command configures the current Redis server to replicate data from the master server running on the same machine at port 6379.
Redis
# Connect to Redis server and run: replicaof 127.0.0.1 6379 # This makes the current server copy data from the master at localhost port 6379.
Important Notes
Replication is asynchronous, so replicas may be slightly behind the master.
Use replicaof no one to stop replication and promote the replica to master.
Summary
Replication copies data from a master Redis server to replicas.
Use replicaof <ip> <port> to set replication.
Use replicaof no one to stop replication.
Practice
1. What is the primary purpose of Redis replication?
easy
Solution
Step 1: Understand replication concept
Replication means copying data from one server (master) to others (replicas) automatically.Step 2: Identify the main goal
This copying helps keep data safe and allows multiple servers to serve read requests.Final Answer:
To copy data from a master server to replicas automatically -> Option CQuick Check:
Replication = Data copying [OK]
Hint: Replication means copying data from master to replicas [OK]
Common Mistakes:
- Confusing replication with data deletion
- Thinking replication encrypts data
- Assuming replication compresses data
2. Which Redis command is used to configure a server as a replica of another?
easy
Solution
Step 1: Recall Redis replication commands
Redis uses the command REPLICAOF (formerly SLAVEOF) to set a server as a replica.Step 2: Check command correctness
Only REPLICAOF accepts master IP and port to configure replication.Final Answer:
REPLICAOF <master_ip> <master_port> -> Option AQuick Check:
Replica config command = REPLICAOF [OK]
Hint: Use REPLICAOF to set replication target server [OK]
Common Mistakes:
- Using CONNECT which is not a Redis replication command
- Confusing SYNC with configuration command
- Inventing COPYFROM which does not exist
3. Given this Redis config snippet:
What is the effect of
replicaof 192.168.1.10 6379 replica-read-only yes
What is the effect of
replica-read-only yes?medium
Solution
Step 1: Understand replica-read-only setting
Setting replica-read-only to yes means the replica will not accept writes from clients.Step 2: Identify behavior of replica
The replica only allows read commands and rejects any write commands to keep data consistent with master.Final Answer:
The replica rejects write commands and only allows reads -> Option AQuick Check:
replica-read-only yes = read-only replica [OK]
Hint: replica-read-only yes means no writes allowed on replica [OK]
Common Mistakes:
- Thinking replica can write data with this setting
- Assuming replication stops with this option
- Believing replica becomes master automatically
4. You run
replicaof 192.168.1.20 6379 on a Redis server but replication does not start. What is a likely cause?medium
Solution
Step 1: Check network and master details
If replication does not start, often the master IP or port is wrong or the replica cannot reach the master.Step 2: Verify command correctness
The command replicaof with IP and port is correct; no 'start' keyword is needed.Final Answer:
The master IP or port is incorrect or unreachable -> Option BQuick Check:
Replication fails if master unreachable [OK]
Hint: Check master IP and port if replication fails [OK]
Common Mistakes:
- Using wrong command syntax like replicaof start
- Assuming replication needs data on replica first
- Thinking Redis does not support replication
5. You want to set up a Redis replica that can serve read requests but also allow some writes during maintenance. Which configuration is best?
hard
Solution
Step 1: Understand replica-read-only option
Setting replica-read-only to no allows writes on the replica, useful during maintenance.Step 2: Combine with replication setup
Using replicaof configures replication; setting replica-read-only no allows writes while still replicating.Final Answer:
Use replicaof <master_ip> <master_port> and set replica-read-only no -> Option DQuick Check:
replica-read-only no allows writes on replica [OK]
Hint: Set replica-read-only no to allow writes on replica [OK]
Common Mistakes:
- Assuming replica-read-only yes allows writes
- Thinking manual copy replaces replication
- Confusing replica with master roles
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
