Master-replica architecture helps keep data safe and available by copying data from one main server (master) to others (replicas).
Master-replica architecture in Redis
To set a Redis server as a replica:
SLAVEOF <master-ip> <master-port>
To stop being a replica and become a master again:
SLAVEOF NO ONEThe SLAVEOF command tells a Redis server to copy data from the master server.
Use SLAVEOF NO ONE to make a replica stop copying and become a master.
SLAVEOF 192.168.1.100 6379
SLAVEOF NO ONE
This example sets the current Redis server as a replica of the master on localhost. Then it checks the server role.
# On replica server terminal SLAVEOF 127.0.0.1 6379 # Now the replica copies data from the master running on localhost port 6379 # To check role: ROLE
Replicas only copy data from the master; they do not accept writes directly.
When the master changes data, replicas update automatically to match.
Using replicas can improve read speed by spreading requests across servers.
Master-replica architecture copies data from one main server to others for safety and speed.
Use SLAVEOF to set a replica and SLAVEOF NO ONE to make it a master again.
Replicas help keep your app working even if the master has problems.
Practice
Solution
Step 1: Understand the role of master and replicas
The master server holds the original data, and replicas copy this data to keep backups and improve read speed.Step 2: Identify the main goal of this setup
This setup is mainly for safety (backup) and speed (load distribution).Final Answer:
To copy data from one main server to others for safety and speed -> Option AQuick Check:
Master-replica = data copy for safety and speed [OK]
- Thinking replicas delete data
- Confusing master-replica with encryption
- Believing replicas run unrelated databases
Solution
Step 1: Recall the Redis command for setting a replica
The correct command isSLAVEOFfollowed by the master's IP and port.Step 2: Check the options for exact command syntax
Only SLAVEOF <port> uses the correct commandSLAVEOF.Final Answer:
SLAVEOF <master-ip> <port> -> Option CQuick Check:
Set replica with SLAVEOF command [OK]
- Using REPLICAOF instead of SLAVEOF
- Trying non-existent commands like SETREPLICA
- Confusing COPYTO with replication commands
SLAVEOF 192.168.1.10 6379 GET user:100
What will happen if the master at 192.168.1.10 is down?
Solution
Step 1: Understand replica behavior when master is down
The replica keeps the last synced data even if the master is offline.Step 2: Predict the GET command result on replica
GET will return the last known value for the key from the replica's local data.Final Answer:
The replica will return the last synced value for user:100 -> Option BQuick Check:
Replica serves last synced data if master is down [OK]
- Assuming replica errors out when master is down
- Thinking replica auto-promotes to master
- Believing replica deletes keys on master failure
SLAVEOF NO ONE on a Redis replica server but it still behaves like a replica. What is the likely problem?Solution
Step 1: Understand what SLAVEOF NO ONE does
This command tells the server to stop replicating and become a master.Step 2: Identify why the server still acts as replica
If the server is still connected or replication not stopped properly, it remains a replica.Final Answer:
The server is still connected to the master and did not switch roles -> Option AQuick Check:
Server must disconnect from master to become master [OK]
- Typing SLAVE OF instead of SLAVEOF
- Thinking restart is always needed
- Believing SLAVEOF NO ONE only works on masters
Solution
Step 1: Identify the goal of continuous operation
To keep Redis working if master fails, replicas must be ready to take over automatically.Step 2: Evaluate options for failover and replication
Set multiple replicas and configure automatic failover to promote a replica to master uses multiple replicas and automatic failover, which is the best practice.Final Answer:
Set multiple replicas and configure automatic failover to promote a replica to master -> Option DQuick Check:
Multiple replicas + auto failover = high availability [OK]
- Relying on manual data copy only
- Running multiple masters without replicas
- Disabling replication and trusting backups alone
Ready to prove it?
Pass the test and this pattern moves unknown
