Persistence and backup
RDB snapshots and AOF on Redis, how to restore after a pod or a volume is gone, and what a second site actually needs to recover.
Redis keeps the working copy in memory. A pod that exits loses every key, unless a replica has it or the process has written it to disk. The high availability lab covers the replica. This lab covers the disk: RDB snapshots and the append-only file (AOF), a restore you can rehearse, and a copy at the DR site.
Persistence makes Redis a safer cache and a safer session store. It does not make Redis the system of record. A write acknowledged to the client can still be missing from the snapshot, and from the replica, if the process dies in that window. Data you cannot regenerate lives in PostgreSQL. The Crunchy DC/DR lab is that copy.
The commands assume the cache namespace and the OpsTree operator from the earlier labs, image quay.io/opstree/redis:v8.2.1.
RDB and AOF
| RDB | AOF | |
|---|---|---|
| What it is | A point-in-time dump of the whole dataset | A log of write commands since the last rewrite |
| File | dump.rdb |
appendonly.aof (a directory of files on Redis 7 and later) |
| Loss on a crash | Everything since the last successful snapshot | About one second with appendfsync everysec, or every write with always |
| Cost | A fork while the snapshot runs. Large datasets spike memory | A write to disk on the fsync policy you set |
Use both. RDB gives you a fast restart and a file you can ship to DR. AOF shrinks the gap the snapshot leaves. appendfsync always is the durable end of the scale and it caps your write rate at the disk. everysec is the usual choice.
The operator reads extra directives from a ConfigMap named by redisConfig.additionalRedisConfig. The key inside the ConfigMap is redis-additional.conf.
apiVersion: v1
kind: ConfigMap
metadata:
name: cache-redis-config
namespace: cache
data:
redis-additional.conf: |
save 3600 1
save 300 100
save 60 10000
appendonly yes
appendfsync everysec
aof-use-rdb-preamble yes
save 300 100 means "snapshot if at least 100 keys changed in 300 seconds". Tune the numbers to the write rate. A cache that is happy to lose five minutes can snapshot less often. A session store should not.
apiVersion: redis.redis.opstreelabs.in/v1beta2
kind: Redis
metadata:
name: cache
namespace: cache
spec:
podSecurityContext:
runAsUser: 1000
fsGroup: 1000
kubernetesConfig:
image: quay.io/opstree/redis:v8.2.1
imagePullPolicy: IfNotPresent
redisConfig:
additionalRedisConfig: cache-redis-config
storage:
volumeClaimTemplate:
spec:
accessModes: ["ReadWriteOnce"]
resources: {requests: {storage: 2Gi}}
The PVC is the whole point. Without storage, the files land on the container's writable layer and die with the pod. This is the same Redis kind as a standalone. On the RedisReplication from the HA lab, set the same ConfigMap and the same volume claim. Replicas write their own files. Restoring a replica's file onto a primary is how you get an old copy back. Know which pod you copied from.
Prove it is on
oc exec -n cache cache-0 -- redis-cli INFO persistence
oc exec -n cache cache-0 -- redis-cli CONFIG GET appendonly
oc exec -n cache cache-0 -- redis-cli CONFIG GET save
oc exec -n cache cache-0 -- redis-cli SET lab:persist yes
oc exec -n cache cache-0 -- redis-cli BGSAVE
oc exec -n cache cache-0 -- redis-cli LASTSAVE
aof_enabled:1 and an rdb_last_bgsave_status:ok are the two lines that matter. LASTSAVE returns a Unix time. Run BGSAVE, change a key, and confirm a later LASTSAVE only after the next snapshot. BGSAVE is asynchronous. BGREWRITEAOF rewrites the AOF when it has grown. The operator's disk has to have room for the rewrite, which needs free space about the size of the current file.
Delete the pod and read the key back:
oc delete pod -n cache cache-0
oc wait -n cache pod/cache-0 --for=condition=Ready
oc exec -n cache cache-0 -- redis-cli GET lab:persist
The value is yes because the volume came back with the pod. That tests persistence. It does not test a lost volume.
Restore when the volume is gone
Take a copy while the process is healthy. redis-cli --rdb writes a fresh snapshot to your machine without depending on the last save interval.
oc exec -n cache cache-0 -- redis-cli --rdb /tmp/dump.rdb
oc cp cache/cache-0:/tmp/dump.rdb ./dump.rdb
Ship dump.rdb to object storage the DR site can read. A bucket per environment, with a lifecycle that keeps the copies you would actually restore, is enough. Write the time of the snapshot next to the object. A restore without a time is a guess.
To restore onto a pod whose volume was replaced:
- Scale the
Redis(or the replication) to zero and wait until the pod is gone. Restoring into a running process does nothing. Redis reads the files at start. - Mount the new PVC, or start the pod once so the claim exists, then copy
dump.rdbto the data directory the process uses (/dataon this image). - Remove a stale AOF directory if you want the RDB to win. If both exist, current Redis loads the AOF.
- Start the pod.
GET lab:persistreturns the value from the snapshot. Keys written after that snapshot are gone. That gap is the RPO of this method.
Do this on a standby namespace before you ever do it in anger. A restore rehearsal that has never been run will be the moment you learn the filename was wrong.
The DR site
A Redis replica across a WAN is still asynchronous, and it couples the two sites: a stall on the link stalls the replication backlog, and a split can leave you with two primaries if someone promotes both. Prefer a snapshot shipped to object storage, and a Redis on the DR cluster that you restore onto when you declare the failover.
Order it behind the database. Promote PostgreSQL first (Crunchy), then restore Redis or start empty and let the application refill the cache. An empty cache is a slow afternoon. A restored cache from the wrong hour, with sessions for a database that was rolled back, is a worse incident. Sessions often should start empty at DR. Document which key prefixes are rebuilt (catalog:) and which are restored only if the snapshot is newer than the database (session: is usually rebuilt).
Replication inside one site remains what it was in the HA lab. Persistence is the copy that survives a volume. The bucket is the copy that survives a site.
What's next
Pub/sub and Streams uses the same Redis and does not change this durability story: a pub/sub message is not in the RDB. Security and monitoring puts a password and TLS on the port these commands left open, and graphs rdb_last_bgsave_status.