Database Management
Redis
Subjective
Oct 05, 2025
Explain Redis replication setup and best practices.
Detailed Explanation
Redis replication provides data redundancy and read scaling:
**Master Configuration:**
# redis-master.conf
bind 192.168.1.100
port 6379
requirepass master_password
masterauth master_password
repl-diskless-sync no
repl-ping-replica-period 10
**Replica Configuration:**
# redis-replica.conf
bind 192.168.1.101
port 6379
replicaof 192.168.1.100 6379
masterauth master_password
requirepass replica_password
replica-read-only yes
**Dynamic Replication:**
# Make server a replica
REPLICAOF 192.168.1.100 6379
CONFIG SET masterauth master_password
# Stop replication
REPLICAOF NO ONE
# Check replication status
INFO replication
**Python Client with Read/Write Splitting:**
class RedisCluster:
def __init__(self, master_host, replica_hosts, password):
self.master = redis.Redis(host=master_host, password=password)
self.replicas = [redis.Redis(host=h, password=password) for h in replica_hosts]
self.replica_index = 0
def write(self, key, value, **kwargs):
return self.master.set(key, value, **kwargs)
def read(self, key):
if not self.replicas:
return self.master.get(key)
replica = self.replicas[self.replica_index]
self.replica_index = (self.replica_index + 1) % len(self.replicas)
try:
return replica.get(key)
except redis.ConnectionError:
return self.master.get(key)
**Best Practices:**
• Use dedicated network for replication
• Configure appropriate timeouts
• Monitor replication lag
• Use diskless replication for fast networks
• Test failover scenarios regularly
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts