How do you tell whether a Redis node is a master or a replica?
There are several ways to find out whether a Redis node is a master or a replica:
1. Via the INFO replication command
This is the main and most reliable way.
Example:
bash
redis-cli INFO replicationOutput:
javascript
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=1203,lag=0or
javascript
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:upThe role: field shows what this node is:
role:master, this is the primary node.role:slave(orrole:replicain newer versions), this is a replica.
2. Via the ROLE command
A more concise alternative.
Example:
bash
redis-cli ROLEPossible responses:
-
For a master:
javascript1) "master" 2) (integer) 12345 3) (array) ... -
For a replica:
javascript1) "slave" 2) "127.0.0.1" 3) (integer) 6379 4) "connected" 5) (integer) 12345
3. Via Redis Sentinel (if it's in use)
If you have Sentinel configured:
bash
redis-cli -p 26379 SENTINEL mastersThis shows the current master and its replicas.
In short:
→ The fastest way is redis-cli INFO replication and checking the role: field.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.