Redis Replication Setup: Master-Replica Configuration and Troubleshooting
Quick Answer
- What it does: Redis replication creates real-time copies of a master dataset on one or more replica nodes, enabling read scaling, data redundancy, and failover capability.
- First checks: Verify the master is reachable from the replica, confirm authentication credentials match, and ensure the replica's
replicaofdirective points to the correct master host and port. - Minimal setup: Start a replica with
redis-server --port 6380 --replicaof localhost 6379or addreplicaof localhost 6379toredis.conf. - Key boundary: Redis replication is asynchronous by default—data loss is possible on master failure. Use
WAITfor synchronous acknowledgment or accept the trade-off. - Version note: Applies to Redis 5.0+ where
replicaofreplaces the legacyslaveofcommand (still accepted for backward compatibility).
What Problem It Solves
Redis replication addresses three production concerns:
- High availability: If the master fails, a replica can be promoted to master, minimizing downtime.
- Read scaling: Replicas serve read traffic, offloading the master for write-heavy workloads.
- Data redundancy: Replicas maintain independent copies of the dataset, protecting against data loss from disk failures or accidental deletion.
For AI applications using MCP hosts like Claude Desktop or Cursor, Redis replication provides a low-latency state store that can serve as memory, cache, or session storage with failover capability.
Installation and Quick Start
Redis replication requires no additional packages—it's built into the core server. Install Redis on each node:
BASH# Debian/Ubuntu apt-get install redis-server # RHEL/CentOS yum install redis # macOS (Homebrew) brew install redis
Start a master on the default port:
BASHredis-server --port 6379
Start a replica on port 6380:
BASHredis-server --port 6380 --replicaof localhost 6379
Verify replication status:
BASHredis-cli -p 6380 INFO replication
Look for role:slave (or role:replica in Redis 7+) and master_link_status:up.
Minimal Working Configuration
For a production-ready replica, create a dedicated configuration file:
CONF# /etc/redis/replica.conf port 6380 replicaof localhost 6379 # Authentication (if master requires a password) masterauth your-master-password # Persistence on replica dir /var/lib/redis-replica dbfilename dump.rdb appendonly yes appendfilename "appendonly.aof" # Replication tuning repl-backlog-size 64mb repl-timeout 60
Start with the config file:
BASHredis-server /etc/redis/replica.conf
For MCP host integration, the equivalent JSON configuration:
JSON{ "mcpServers": { "redis-replica": { "command": "redis-server", "args": [ "--port", "6380", "--replicaof", "localhost", "6379", "--dir", "/data", "--dbfilename", "dump.rdb" ] } } }
Parameters and Environment Variables
Key configuration directives for replication:
| Directive | Default | Purpose |
|---|---|---|
replicaof <host> <port> | (none) | Sets the master address; replica starts replicating immediately |
masterauth <password> | (none) | Password for authenticating to the master |
repl-backlog-size | 1mb | Size of the replication backlog buffer; larger values allow longer disconnects before full resync |
repl-timeout | 60s | Timeout for master-replica communication; increase for slow networks |
replica-read-only | yes | Rejects writes on replicas; disable only for special use cases |
replica-priority | 100 | Used by Redis Sentinel for replica promotion ordering (lower = higher priority) |
repl-diskless-sync | no | Enables diskless full sync (direct socket transfer) for faster initial replication |
min-replicas-to-write | 0 | Minimum replicas that must acknowledge writes before master accepts them |
min-replicas-max-lag | 10 | Max lag (seconds) for the min-replicas check |
Root Cause Analysis
Redis replication operates in two phases:
- Full resync: The replica sends
PSYNCto the master. The master forks a child process, creates an RDB snapshot, and streams it to the replica. The replica loads the snapshot, then applies buffered commands. - Incremental propagation: After the initial sync, the master streams every write command to the replica in real time.
The critical failure modes:
- Asynchronous loss: The master acknowledges writes to clients before replicas confirm receipt. A master crash between acknowledgment and replication means those writes are lost.
- Backlog overflow: If a replica disconnects longer than the backlog buffer can hold, a full resync is required. A large
repl-backlog-sizereduces this risk. - Persistence trap: If the master has persistence disabled and restarts, it starts empty. Replicas then sync from the empty master, wiping their data. Always disable auto-restart for masters without persistence.
Common Errors and Fixes
| Error | Root Cause | Fix |
|---|---|---|
NOAUTH Authentication required | Replica lacks the master's password | Set masterauth in the replica config |
Connection reset by peer during full resync | Network issue or master timeout | Check firewall rules, increase repl-timeout, verify memory availability on master |
Can't SYNC while not connected with my master | replicaof misconfigured or master unreachable | Verify host/port, test with redis-cli -h <master> -p <port> PING |
Replication backlog buffer limit exceeded | Backlog too small for disconnect duration | Increase repl-backlog-size (e.g., to 64mb or 128mb) |
MASTER aborted replication | Master rejected the replica connection | Check requirepass on master matches masterauth on replica |
Production Notes and Security Checks
Security hardening:
- Enable
requirepasson the master and mirror it withmasterauthon replicas. - Use TLS for replication traffic: configure
tls-replication yesand provide certificates. - Restrict network access with firewall rules—only allow replica nodes to reach the master's port.
- Run replicas on separate hosts or availability zones for true redundancy.
Operational safeguards:
- If the master has persistence disabled, disable its auto-restart (
systemctl disable redis-serveror equivalent). Otherwise, a restart creates an empty master that propagates emptiness to replicas. - Monitor replication health with
INFO replication—watchmaster_link_status,master_repl_offset, andslave_repl_offset. Offsets should converge. - Set up alerting for
master_link_status:downand for replica lag exceeding acceptable thresholds. - Test failover procedures regularly: promote a replica, verify writes, then rebuild the failed node as a new replica.
Data consistency caveats:
- Redis replication is eventually consistent, not strongly consistent.
- Use
WAIT numreplicas timeoutafter critical writes to block until replicas acknowledge, reducing (but not eliminating) loss risk. - For strict consistency requirements, consider Redis Cluster with
WAITor an external consensus system.
FAQ
Q: Does Redis replication guarantee strong data consistency?
A: No. Redis uses asynchronous replication by default—the master doesn't wait for replica acknowledgment before confirming writes to clients. A master failure can lose recent writes. The WAIT command forces synchronous acknowledgment for specific writes, but it only reduces the loss window; it doesn't provide strong consistency guarantees.
Q: How do I avoid data loss when the master has persistence disabled?
A: Disable automatic restart for the master. If a master without persistence restarts, it loads an empty dataset, and replicas will sync from it, wiping their data. Ensure at least one replica has persistence enabled (appendonly yes or RDB snapshots), and promote a replica before restarting a failed master.
Q: How do I monitor replication health?
A: Run redis-cli INFO replication on both master and replicas. Key fields: master_link_status (up/down), master_repl_offset and slave_repl_offset (should match or converge), master_last_io_seconds_ago (time since last communication). Set up monitoring alerts for link-down events and offset divergence.