How Redis stays up on Kubernetes
Replication, Sentinel and Cluster, each animated failing over, and what happens to Redis's data on Longhorn, cloud disks, local disks, NFS and emptyDir when a pod moves.
Redis keeps everything in memory, so it is fast. It also means one Redis server is one place to lose everything: a crashed pod, a drained node or a full disk, and every app that uses it stops. High availability means keeping more than one copy and having something ready to switch to a healthy one by itself.
Redis has three ways to do that, and on Kubernetes each one also depends on where its data is stored. This post shows all of it working. Every figure plays by itself when you scroll to it; you can pause, step, or pick another scenario. When you want to set it up, the guide has every command, with Helm and with plain YAML.
#Replication
The simplest form: one primary takes every write and streams a copy of each one to its replicas. This setup is often called master–slave: the primary is the master and the replicas are slaves.
#Master and slave, primary and replica
They are the same two roles. Redis used master and slave until version 5.0 renamed slaves to replicas (SLAVEOF became REPLICAOF). This post says primary and replica, but the old words are still everywhere you'll look, so it helps to know where:
| Where you see it | What it says | What it means |
|---|---|---|
INFO replication | role:master, connected_slaves:2, slave0:… | primary, its replica count, each replica |
| Commands | SLAVEOF and REPLICAOF, SENTINEL slaves and SENTINEL replicas | old and new names for the same command |
| Sentinel | mymaster, get-master-addr-by-name, +switch-master | the group it watches and its primary |
CLUSTER NODES | master, slave | primary, replica |
| Bitnami Helm chart | redis-master, redis-replicas | the primary's and the replicas' StatefulSets |
| OT-Container-Kit operator | leader, follower | the roles a Cluster pod starts with |
Two things to take from it:
- Replication is asynchronous. The primary answers your app before the replicas have the write. If the primary dies in that moment, the write is gone. The
WAITcommand can make one write wait for replicas when it really matters. - Replication alone never fails over. The replicas have the data, but nothing promotes one. You do it by hand with
REPLICAOF NO ONE, then repoint everything else. On its own this is a way to keep copies and spread reads, not high availability.
The last scenario is the trap the Redis docs warn about: a primary with persistence off that restarts automatically comes back empty, and its replicas faithfully copy the empty dataset. On Kubernetes, where pods restart automatically, keep persistence on.
#Sentinel
Sentinel adds the missing decision-maker. Sentinels are small Redis processes that watch the primary and its replicas, agree when the primary is down, and promote a replica. Run at least three, on different nodes, so they can outvote one that is cut off.
The words in the figure are the ones you'll see in Sentinel's logs:
| Term | What it means |
|---|---|
sdown | Subjectively down: one Sentinel got no valid reply for down-after-milliseconds. |
odown | Objectively down: at least quorum Sentinels agree. |
| leader | The Sentinel a majority of all Sentinels voted for. Only it runs the failover. |
mymaster | The name of the group being watched. Clients ask for the primary by this name. |
Your app has to cooperate. A client library that supports Sentinel asks for the primary's address by name and asks again when its connection breaks. Point a plain client at one fixed address and it will keep talking to the old primary.
#One Sentinel group for many services
With microservices, each service usually gets its own small Redis: its own primary and replicas, its own data. They don't each need their own Sentinels. One group of three Sentinels can watch every primary, each under its own name (orders, payments, …), and every service asks for its own name. This is the most common way to run Sentinel outside Kubernetes, and it works inside it too.
What you gain
- Failovers stay separate: each name has its own record, quorum and failover, so when orders fails over, payments doesn't notice.
- Fewer pods: ten services need 3 Sentinels in total instead of 30.
- Adding a service is a command: SENTINEL MONITOR on each Sentinel adds a name while they run.
What to watch
- The Sentinels are shared: with two of the three down, no service can fail over.
- Spread the three across nodes, and watch them as closely as the primaries.
The Helm charts in the guide can't share Sentinels between releases: each install brings its own three. A shared group is set up with plain YAML.
#Cluster
Replication and Sentinel copy all the data to every server, so the dataset has to fit in one server's memory. Redis Cluster splits it instead. The keyspace is divided into 16,384 hash slots. Each key belongs to one slot, each primary owns a range of slots, and each primary has its own replica.
- No Sentinels. The nodes watch each other over the cluster bus (port 16379) and vote among themselves.
- The client does the routing. It hashes the key,
CRC16(key) mod 16384, and goes straight to the owner. A wrong guess gets aMOVEDreply, never a forwarded request. - Multi-key commands need the same slot.
MGET user:7 cart:7fails if the two keys live on different nodes. Hash tags pin keys together:{user:7}:profileand{user:7}:carthash only the part in braces, so they share a slot. - Three primaries is the minimum for the vote to work, and each primary's replica should run on a different node.
#Which one to choose
| Replication | Sentinel | Shared Sentinel | Cluster | |
|---|---|---|---|---|
| Fails over by itself | No | Yes | Yes, each service on its own | Yes |
| Dataset size | Fits in one server | Fits in one server | Each service's fits in one server | Split across primaries |
| Write capacity | One primary | One primary | One primary per service | One per primary |
| Pods (smallest HA setup) | 2 | 3 Redis + 3 Sentinel (often in the same pods) | 3 Sentinel, plus 2+ Redis per service | 6 |
| Client support needed | None | Sentinel-aware | Sentinel-aware, asking for its own name | Cluster-aware |
| Multi-key commands | All | All | All, within a service | Same slot only |
If your data fits in one server's memory, Sentinel is usually the right answer: it keeps every command working, and most client libraries support it. With many services, each with its own Redis, share one Sentinel group between them. Choose Cluster when the data or the write load is more than one server can take.
#Where Redis keeps its data
Redis writes two kinds of files so it can rebuild its memory after a restart: an RDB snapshot every so often, and with appendonly yes, an append-only file (AOF) that logs every write and is flushed to disk every second. On Kubernetes those files live on a volume, and the volume decides what survives when a pod is deleted or a node dies.
#Claims, classes and volumes
A StatefulSet asks for storage through a PersistentVolumeClaim (PVC), one per pod. The claim is matched to a PersistentVolume (PV): the actual disk. Usually a StorageClass makes that disk on demand.
Redis needs ReadWriteOnce volumes: each pod has its own disk, mounted by one node at a time. Replicas never share a disk with their primary; they keep their own copy over the network.
#What happens when a pod moves
The behaviour that matters is what happens to the data when a pod has to run somewhere else. Storage types fall into four groups:
| Storage | Kind | Pod can move to another node | Notes |
|---|---|---|---|
| Longhorn | Network block disk | Yes, the disk follows | Keeps its own copies on other nodes |
| AWS EBS, Google Persistent Disk, Azure Disk | Network block disk | Yes, within one zone | A disk can't cross availability zones |
| Ceph RBD (Rook) | Network block disk | Yes | Same behaviour as Longhorn here |
| Local PV, local-path-provisioner, hostPath | Local disk | No, tied to one node | Fastest; hostPath is for testing only |
| NFS (csi-driver-nfs) | Shared file server | Yes | Slow fsync hurts the append-only file |
| emptyDir | None | Data is lost | Fine for a pure cache |
Longhorn and the cloud disks behave the same in the figure below, so they share one scenario. Local disks and emptyDir behave differently, so they get their own.
With Longhorn there are two layers of copies: Redis keeps a copy on each replica, and Longhorn keeps copies of each disk on other nodes. Three Redis pods on Longhorn's default of three copies per disk store the same data nine times. A StorageClass for Redis can ask Longhorn for fewer copies (numberOfReplicas: "2"), and dataLocality: best-effort keeps one copy on the node the pod runs on, so reads don't cross the network.
#NFS is different
NFS is a file server, not a disk. Every claim becomes a folder on one shared server, which makes it easy to set up and easy to move pods around. The cost is the append-only file: every second Redis asks for its log to be flushed, and on NFS that waits on the network.
#What happens when you delete a pod
Here is everything working together: Sentinel mode, as the Bitnami Helm chart sets it up, with each pod's data on Longhorn. You run one command: kubectl delete pod redis-node-0, and redis-node-0 is the primary.
What makes this work:
- The StatefulSet brings back a pod with the same name.
- The PersistentVolumeClaim outlives the pod, so the new pod gets its old disk.
- Sentinel moved the primary role somewhere else before the pod stopped (planned delete), or within
down-after-milliseconds(crash). - The start script asks Sentinel who the primary is, so the returning pod joins as a replica instead of claiming to be the primary.
To try it on your own cluster, follow the setup guide. Each mode ends with a "break it on purpose" step and this figure, so you can compare what you see with what should happen.
Referenced by
Comments
Sign in with GitHub to comment. Comments are kept as GitHub Discussions.