Redis high availability

Replication, Sentinel and Redis Cluster on OpenShift, and failover tests you can run by deleting a pod or asking Sentinel to promote.

Updated CompTech Team

One Redis pod is a cache with a single point of failure. The deploy lab stopped there. High availability adds copies, and a rule for which copy becomes the primary when the current one disappears. Redis has three mechanisms, and they solve different problems. Mixing them up is how a failover test passes while the application still writes to a dead pod.

Mechanism What it adds What it does not add
Replication A primary and replicas. Replicas follow the primary asynchronously A new primary. Somebody has to promote a replica
Sentinel A quorum that promotes a replica and publishes the new address A way to spread keys across machines. One primary still holds every key
Cluster Up to 16,384 hash slots, split across primaries, each with its own replicas A single keyspace you can scan as one server. A client must be cluster-aware

Replication is the base of the other two. Sentinel watches one replication group. Cluster shards the keyspace and fails over inside each shard.

Apply this in the cache namespace only after you remove the standalone Redis named cache from the deploy lab, or pick another namespace. Two operators managing the same pods will fight. The operator is already installed. The anyuid binding from that lab still applies if you reuse the namespace.

Replication

A replica holds a copy of the primary's dataset and accepts reads if you ask it to (READONLY from a client that is allowed to read replicas). Writes go to the primary. The copy is asynchronous: a write acknowledged to the client can still be missing on the replica if the primary dies in that moment. WAIT 1 1000 blocks the client until one replica has acknowledged, or until a second has passed. It is a per-command choice, not the default.

The OpsTree RedisReplication resource runs that topology. clusterSize: 3 is one primary and two replicas.

apiVersion: redis.redis.opstreelabs.in/v1beta2
kind: RedisReplication
metadata:
  name: cache
  namespace: cache
spec:
  clusterSize: 3
  podSecurityContext:
    runAsUser: 1000
    fsGroup: 1000
  kubernetesConfig:
    image: quay.io/opstree/redis:v8.2.1
    imagePullPolicy: IfNotPresent
    resources:
      requests: {cpu: 100m, memory: 128Mi}
      limits: {cpu: "1", memory: 1Gi}
  storage:
    volumeClaimTemplate:
      spec:
        accessModes: ["ReadWriteOnce"]
        resources: {requests: {storage: 2Gi}}
oc apply -f replication.yaml
oc rollout status statefulset/cache -n cache
for p in cache-0 cache-1 cache-2; do
  echo -n "$p "
  oc exec -n cache "$p" -- redis-cli INFO replication | awk -F: '/^role/{gsub(/\r/,"",$2); print $2}'
done

One pod prints master. The others print slave: that is still the role string INFO replication uses for a replica. Write a key through the primary pod, then read it on a replica. The replica will refuse the write.

Deleting the primary pod without Sentinel or Cluster loses writes until that pod returns. The StatefulSet starts it again with the same name and the same volume, and it becomes the primary again only if nothing else was promoted. Do not rehearse failover on this resource alone.

Sentinel

Sentinel is a separate process. It does not store your keys. Three Sentinels agree, by a quorum, that the primary is unreachable, then one of them promotes a replica and rewrites the other replicas to follow it. Clients learn the new address from Sentinel, not from a Kubernetes Service that always pointed at one pod.

A Sentinel calls a primary subjectively down when it cannot reach it. The primary is objectively down when enough Sentinels agree. The quorum for three Sentinels is 2. Spread the Sentinel pods across nodes; three Sentinels on one node notice nothing when that node dies.

apiVersion: redis.redis.opstreelabs.in/v1beta2
kind: RedisSentinel
metadata:
  name: cache
  namespace: cache
spec:
  clusterSize: 3
  podSecurityContext:
    runAsUser: 1000
    fsGroup: 1000
  redisSentinelConfig:
    redisReplicationName: cache
    masterGroupName: myMaster
  kubernetesConfig:
    image: quay.io/opstree/redis-sentinel:v8.2.1
    imagePullPolicy: IfNotPresent
    resources:
      requests: {cpu: 50m, memory: 64Mi}
      limits: {cpu: 500m, memory: 256Mi}

The sample names the Sentinel cache, and the operator appends -sentinel to the StatefulSet, so the first pod is cache-sentinel-0. Sentinel listens on 26379.

oc apply -f sentinel.yaml
oc get pods -n cache
oc exec -n cache cache-sentinel-0 -- redis-cli -p 26379 SENTINEL masters

The name field should be myMaster, which the manifest set. If it differs, use the printed name in the commands below.

oc exec -n cache cache-0 -- redis-cli SET lab:failover before
oc exec -n cache cache-sentinel-0 -- redis-cli -p 26379 SENTINEL failover myMaster
oc exec -n cache cache-sentinel-0 -- redis-cli -p 26379 SENTINEL get-master-addr-by-name myMaster

SENTINEL failover is the planned test: Sentinel promotes a replica even though the old primary is healthy. Read lab:failover through the new primary. It should still be before.

The unplanned test is to delete the pod that currently has the primary role, then wait:

oc delete pod -n cache cache-0
oc exec -n cache cache-sentinel-0 -- redis-cli -p 26379 SENTINEL get-master-addr-by-name myMaster

Until Sentinel publishes the new address, a client that has cached the old one will time out or hit a replica and get READONLY. Applications must ask Sentinel (or a client that does, such as Lettuce's sentinel mode) rather than a single pod DNS name. Give the test half a minute before calling it a failure; down-after-milliseconds and the failover timeout are on the order of seconds, and a pod delete is faster than that.

Cluster

Redis Cluster splits the keyspace into 16,384 hash slots. A key's slot is CRC16(key) mod 16384. A primary owns a range of slots. A replica of that primary can take those slots over. Keys that must stay together, because you touch them in one transaction, share a hash tag: session:{7f3a}:cart and session:{7f3a}:user land on the same slot. The tag is the substring between { and }.

You want at least three primaries, and at least one replica for each primary you expect to survive a pod death. A primary with no replica takes its slots with it when its pod is deleted, and the cluster reports those slots as failed.

apiVersion: redis.redis.opstreelabs.in/v1beta2
kind: RedisCluster
metadata:
  name: cache-sharded
  namespace: cache
spec:
  clusterSize: 3
  clusterVersion: v7
  persistenceEnabled: true
  podSecurityContext:
    runAsUser: 1000
    fsGroup: 1000
  kubernetesConfig:
    image: quay.io/opstree/redis:v8.2.1
    imagePullPolicy: IfNotPresent
  storage:
    volumeClaimTemplate:
      spec:
        accessModes: ["ReadWriteOnce"]
        resources: {requests: {storage: 2Gi}}

clusterVersion: v7 is the operator's cluster-bus setting from its own sample, including when the image is Redis 8.2. After the pods are up, read the topology before you delete anything:

The operator's StatefulSets end in -leader and -follower. CLUSTER FAILOVER has to run on a follower. On a primary it is a different operation. If either variable is empty, list the pods with oc get pods -n cache and substitute those names.

LEADER=$(oc get pods -n cache -o name | grep cache-sharded-leader | head -1)
FOLLOWER=$(oc get pods -n cache -o name | grep cache-sharded-follower | head -1)
oc exec -n cache "$LEADER" -- redis-cli CLUSTER INFO
oc exec -n cache "$LEADER" -- redis-cli CLUSTER NODES
oc exec -n cache "$LEADER" -- redis-cli -c SET lab:slot ok
oc exec -n cache "$FOLLOWER" -- redis-cli CLUSTER FAILOVER
oc exec -n cache "$LEADER" -- redis-cli -c GET lab:slot

cluster_state:ok is the healthy line. CLUSTER NODES prints master or slave, the slots each master owns, and which master a slave follows. If you see masters and no slaves, this sample has given you no failover target. Stop there and set the operator's follower fields from its current RedisCluster example before you continue. Deleting a master that owns slots alone drops those slots. After CLUSTER FAILOVER, lab:slot is still ok, and the old follower's line now says master for that slot range. For the unplanned case, delete one primary pod and watch CLUSTER INFO return to cluster_state:ok once its replica has the slots. A client that is not using -c (or a cluster client in the application) will stop at a MOVED error. That error means the client is talking to Cluster as if it were a single instance.

What you proved

Replication copied the dataset. Sentinel changed which pod is the primary and will tell a client that asks. Cluster did the same inside one slot range and left the other ranges where they were. None of the three replace PostgreSQL, and none of them keep a write that the primary acknowledged and then lost before any replica received it. Durability of that last write is the persistence lab. A second site for Postgres, which is a different design, is the Crunchy DC/DR lab.

What's next

Persistence and backup covers RDB, AOF and restoring after the volume is gone. Security and monitoring puts a password and TLS on the ports these tests left open, and graphs failover in Grafana.