Production Redis

Persistence, Replication & Cluster

Understand Redis persistence options (RDB, AOF), replication, and Redis Cluster for production deployments.

Redis Persistence

By default, Redis is in-memory — data is lost on restart. For production, you need persistence.

RDB (Redis Database Backup)

Point-in-time snapshots saved to disk at configured intervals.

bash
# In redis.conf:
save 900 1      # Save after 900s if at least 1 key changed
save 300 10     # Save after 300s if at least 10 keys changed
save 60 10000   # Save after 60s if at least 10000 keys changed

dbfilename dump.rdb
dir /var/lib/redis

Pros: Compact file, fast restarts, good for backups.

Cons: Data between snapshots can be lost in a crash.

AOF (Append Only File)

Logs every write operation. On restart, Redis replays the log to reconstruct the dataset.

bash
# In redis.conf:
appendonly yes
appendfsync everysec    # Sync to disk every second (recommended)
# appendfsync always    # Sync on every write (slowest, safest)
# appendfsync no        # Let OS decide (fastest, least safe)

Pros: Much less data loss (at most 1 second).

Cons: Larger files, slower than RDB.

AOF + RDB (Recommended for Production)

Use both. RDB for fast restarts, AOF for minimal data loss.

Replication

Redis supports primary-replica (master-slave) replication. Replicas receive a continuous stream of write commands from the primary.

bash
# On replica, in redis.conf:
replicaof 192.168.1.100 6379

# Or at runtime:
REPLICAOF 192.168.1.100 6379

Replicas are read-only by default. Useful for:

  • Read scaling (distribute reads across replicas)
  • High availability (promote replica if primary fails)
  • Backup without impacting primary

Redis Sentinel

Redis Sentinel provides automatic failover. It monitors primaries and replicas, and automatically promotes a replica if the primary goes down.

bash
# sentinel.conf
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000

Redis Cluster

For horizontal scaling beyond a single node, Redis Cluster shards data across multiple nodes.

  • Data is split into 16,384 hash slots
  • Each node owns a subset of slots
  • A minimum of 3 primary nodes is required
  • Supports automatic sharding and rebalancing
bash
# Create a cluster (3 primaries + 3 replicas)
redis-cli --cluster create \
  127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 \
  127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 \
  --cluster-replicas 1

Managed Redis

For most teams, a managed Redis service is preferable to self-hosting:

  • Redis Cloud (official) — Fully managed, enterprise support
  • AWS ElastiCache — Redis-compatible, deeply integrated with AWS
  • Upstash — Serverless Redis, pay-per-request, ideal for edge/serverless
  • Azure Cache for Redis — Managed Redis on Azure

In AI applications, Upstash is particularly popular because it works seamlessly with serverless and edge functions that need Redis without maintaining a persistent connection.

Example

bash
# Check replication status
redis-cli INFO replication
# role:master
# connected_slaves:2
# slave0:ip=192.168.1.2,port=6379,state=online,offset=12345

# Monitor Redis in real time
redis-cli MONITOR

# Check memory usage
redis-cli INFO memory
# used_memory_human:45.23M

# Slow log (commands taking > 10ms)
redis-cli SLOWLOG GET 10

# Key space analysis
redis-cli --scan --pattern "user:*" | wc -l

Want to run this code interactively?

Try in Compiler