Redis 📂 Persistence and reliability · 1 of 1 25 min read

Redis Persistence, Replication, Sentinel, and Cluster

A practical Redis operations tutorial. Learn how Redis saves to disk with RDB snapshots and AOF, how to back up and restore, how replication with a primary and replicas works, automatic failover with Sentinel, and scaling out with Redis Cluster using hash slots. Includes animated diagrams, config, and golden rules.

Section 01

Keeping Redis Safe and Always On

The Notepad, the Photocopy, and the Backup Clerk
Redis keeps its data on a fast notepad (memory). But a notepad can be lost if the desk is cleared. So Redis also takes photocopies to a drawer (saving to disk), keeps a second clerk writing the same notes at the same time (a replica), and has a supervisor who promotes the second clerk the moment the first one is out (Sentinel). When one desk is not enough, the work is split across many desks (a cluster).

This tutorial covers all of it: saving to disk, backups, replication, high availability, and scaling out.

Section 02

How Redis Saves to Disk — RDB vs AOF

Redis lives in memory, but it can write to disk so data survives a restart. There are two ways. RDB takes a snapshot of everything every so often. AOF writes down every change command as it happens. You can use one, the other, or both together.

RDB saves a full picture now and then. AOF logs every write as it happens, so it loses less on a crash but the file is larger.

💾 RDB Snapshot vs AOF Log
RDB — a full snapshot every few minutes (small file, fast restart) Redis (memory) all your data BGSAVE every N min dump.rdb one full copy ⓘ a crash may lose writes since the last snapshot AOF — append every write command to a log (loses less, bigger file) Redis (memory) every write append each command appendonly.aof SET user:1 "Asha" INCR views
📸 RDB snapshot
Small file, fast restart
Great for backups
Can lose recent writes
Snapshot uses some CPU/RAM
📝 AOF log
Loses at most ~1 second
Bigger file, slower restart
Rewritten to stay compact
Safer for durability
# redis.conf — RDB: snapshot if enough changes happened in a time window
save 900 1        # after 900s if at least 1 key changed
save 300 10       # after 300s if at least 10 keys changed
save 60 10000    # after 60s if at least 10000 keys changed

# AOF: log every write; sync to disk about once a second
appendonly yes
appendfsync everysec

# trigger a snapshot or rewrite the AOF by hand (runs in the background)
BGSAVE
BGREWRITEAOF
💡
The Common Choice: Use Both

Turn on AOF for safety (lose at most a second), and keep RDB snapshots for quick backups and fast restarts. On restart, Redis rebuilds from the AOF because it is the most complete. If you use Redis purely as a cache you can rebuild, you may turn persistence off entirely.


Section 03

Backups and Recovery

A backup is just a safe copy of the RDB file. Because dump.rdb is a single file, backing up is a copy, and restoring is putting that file back and starting Redis.

💾 Back Up and Restore, Step by Step
Back up
Run BGSAVE to write a fresh dump.rdb, then copy that file somewhere safe (another disk, cloud storage).
Schedule
Copy the RDB on a schedule (for example hourly with cron). Keep several days of copies, not just the latest.
Restore
Stop Redis, put the backup dump.rdb in the data directory, then start Redis. It loads the file on boot.
Test
Restore into a spare server now and then. A backup you have never restored is not a backup you can trust.
# 1) make a fresh snapshot in the background
redis-cli BGSAVE

# 2) find where Redis keeps the file, then copy it away
redis-cli CONFIG GET dir        # e.g. /var/lib/redis
cp /var/lib/redis/dump.rdb  /backups/dump-$(date +%F).rdb

# 3) to restore: stop Redis, drop the backup in place, start again
sudo systemctl stop redis
cp /backups/dump-2026-09-20.rdb  /var/lib/redis/dump.rdb
sudo systemctl start redis
⚠️
Copy Backups Off the Server

A backup on the same disk as Redis is not safe — if the disk dies, both are gone. Copy the RDB to another machine or to cloud storage. And keep a few older copies, in case a bad write got into the newest one.


Section 04

Replication — Primary and Replica

Replication keeps one or more copies of your data on other servers. The primary takes all the writes and streams every change to its replicas. Replicas are read-only copies. This gives you a spare copy and lets you spread reads across servers.

Writes go to the primary. The primary copies every change to its replicas. Reads can be served by any replica.

🔁 One Primary, Many Replicas
writes Primary takes all writes streams changes Replica A (read-only)serves reads Replica B (read-only)serves reads replicate replicate
# on the replica server: point it at the primary
REPLICAOF 10.0.0.1 6379
# (or in redis.conf:  replicaof 10.0.0.1 6379)

# check the link and how far behind the replica is
INFO replication
# role:master / role:slave, connected_slaves, master_repl_offset ...
⏳
Replication Is Asynchronous

The primary does not wait for replicas before replying to a write. So a replica can be a tiny bit behind, and a write can be lost if the primary dies at the wrong instant. Replicas give you spare copies and read scaling — they do not by themselves make failover automatic. For that, add Sentinel.


Section 05

High Availability with Redis Sentinel

If the primary dies, someone must promote a replica to take its place — fast, and without a human at 3am. That someone is Sentinel. Sentinels are small watchdog processes that watch the primary, agree when it is truly down, and promote a replica automatically.

Several sentinels watch the primary. When enough agree it is down (a quorum), they pick a replica and promote it to the new primary. Clients are pointed at the new one.

🛡️ Automatic Failover with Sentinel
Primary ✗ down stopped responding Replica 1 → new Primary 👑 promoted Replica 2 follows new primary Sentinels (watchdogs) 👀 sentinel-1 👀 sentinel-2 👀 sentinel-3 2 of 3 agree it is down (quorum) → failover they pick a replica and promote it promote
# sentinel.conf — watch a primary called "mymaster", quorum of 2
sentinel monitor mymaster 10.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000

# start a sentinel
redis-sentinel /etc/redis/sentinel.conf

# apps ask Sentinel for the current primary address (it may change after a failover)
redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster
✅
Run at Least Three Sentinels

Use an odd number, three or more, on separate machines. A quorum stops a single confused sentinel from starting a needless failover. Your app should connect through a Sentinel-aware client so it always finds the current primary, even after it changes.


Section 06

Scaling Out with Redis Cluster

One server can only hold so much data and handle so many writes. Redis Cluster splits the data across many primaries. It divides the keyspace into 16,384 hash slots, and each node owns a range of slots. A key's slot is decided by CRC16(key) mod 16384, so every client can work out which node holds a key.

A key is hashed to a slot number, and the slot belongs to one node. Here key user:57 lands in slot 8492, which node B owns.

🧩 Hash Slots Split the Data Across Nodes
key: user:57 CRC16 mod 16384 slot 8492 16,384 slots, split across three nodes: 0 – 5460 5461 – 10922 10923 – 16383 Node Aslots 0 – 5460 Node B ← holds user:57slots 5461 – 10922 Node Cslots 10923 – 16383
# build a cluster: 3 primaries + 1 replica each (6 nodes)
redis-cli --cluster create \
  10.0.0.1:6379 10.0.0.2:6379 10.0.0.3:6379 \
  10.0.0.4:6379 10.0.0.5:6379 10.0.0.6:6379 \
  --cluster-replicas 1

# which slot does a key map to, and see the slot layout
CLUSTER KEYSLOT user:57      # -> 8492
CLUSTER SLOTS

# keep related keys on the same node with a hash tag {..}
MSET {user:57}:name "Asha" {user:57}:cart "..."
# only the part in {} is hashed, so both land in the same slot
🔑
Multi-Key Commands Need the Same Slot

In a cluster, a command that touches several keys (like MGET or a transaction) only works if those keys live in the same slot. Use a hash tag — put a shared part in braces, like {user:57}:name and {user:57}:cart — so related keys land together.


Section 07

Which Setup Do You Need?

💻
Single server
Fine for development and small apps. Turn on AOF and take RDB backups. Simple, but it is one point of failure.
dev · small
🔁
Primary + replicas
Add replicas for spare copies and to spread reads. Add Sentinel on top so failover is automatic when the primary dies.
HA with Sentinel
🧩
Cluster
When the data or write load is too big for one server, shard across many primaries with Cluster. Each shard can have its own replica.
scale + HA

Section 08

Golden Rules

💾 Persistence, HA & Scaling — Non-Negotiable Rules
1
Know your two save modes. RDB is a periodic snapshot; AOF logs every write. Use both: AOF for safety, RDB for fast backups and restarts.
2
Copy backups off the box. An RDB copy on the same disk is not a backup. Send it elsewhere, keep several, and test a restore now and then.
3
Replicas are read-only copies. The primary takes writes and streams them out. Replication is async, so a replica may lag slightly.
4
Sentinel makes failover automatic. Run three or more sentinels on separate machines, and connect apps with a Sentinel-aware client.
5
Cluster shards by hash slot. 16,384 slots split across nodes; a key's slot is CRC16(key) mod 16384. Use it when one server is not enough.
6
Use hash tags in a cluster. Put a shared part in { } so related keys share a slot and multi-key commands keep working.
7
Match the setup to the need. Single for dev, primary + replicas + Sentinel for high availability, Cluster when you must scale beyond one server.
You have completed Persistence and reliability. View all sections →