Learn Labs
Replication & Sentinel

Sentinel

Automatic failover, quorum, client discovery, and split brain

Replicas give you a copy. Sentinel turns a copy into a failover: a set of separate processes that watch the primary, agree when it's down, promote a replica, repoint the other replicas, and tell clients where the new primary is.

The setup in this repo

Three Sentinels watch one primary and two replicas (--profile ha):

port 26379
sentinel resolve-hostnames yes
sentinel announce-hostnames yes
sentinel monitor mymaster redis 6379 2      # quorum 2
sentinel auth-user mymaster default
sentinel auth-pass mymaster redis123
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 30000
sentinel parallel-syncs mymaster 1

Sentinels only need to be told about the primary. They discover the replicas from the primary's INFO, and each other through a Pub/Sub channel on the primary.

SENTINEL get-master-addr-by-name mymasterwhere is the primary now? -> redis 6379SENTINEL replicas mymasterthe replicas it knowsSENTINEL sentinels mymasterthe other SentinelsSENTINEL ckquorum mymasterOK 3 usable Sentinels. Quorum and failover authorization can be reachedSENTINEL failover mymasterforce a failover (for testing or maintenance)

How a failover happens

  1. Subjectively down (SDOWN). One Sentinel gets no valid reply from the primary for down-after-milliseconds (5 s here).
  2. Objectively down (ODOWN). At least quorum (2) Sentinels agree it's down.
  3. Leader election. Sentinels elect one of themselves to run the failover. This needs a majority of all Sentinels (2 of 3), regardless of the quorum setting.
  4. Pick a replica: priority (replica-priority), then the most replication offset (least data missing), then the lowest run ID.
  5. Promote it with REPLICAOF NO ONE, repoint the other replicas (parallel-syncs at a time), and publish the new address.
  6. When the old primary comes back, Sentinel makes it a replica of the new one.
3 sentinelsprimaryreplica-1replica-2clientsPING: no reply 5 s+sdown, then +odown (quorum 2)REPLICAOF NO ONEREPLICAOF replica-1get-master-addr-by-namereplica-1:6379rejoin as replicaneeds masterauth

Measured in this lab:

docker stop redis
# ...
docker exec sentinel-1 redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster
1) "redis-replica-1"      # failover took 6-16 seconds (5s detection + election + promotion)

docker start redis
docker exec redis redis-cli --user default --pass redis123 INFO replication
role:slave
master_host:redis-replica-1
master_link_status:up

Clients must ask Sentinel

After a failover, the primary is at a different address. Applications don't connect to a fixed host; they ask the Sentinels:

from redis.sentinel import Sentinel

s = Sentinel([("sentinel-1", 26379), ("sentinel-2", 26379), ("sentinel-3", 26379)],
           socket_timeout=0.5)
s.discover_master("mymaster")       # ('redis', 6379)
s.discover_slaves("mymaster")       # [('redis-replica-1', 6379), ('redis-replica-2', 6379)]

primary = s.master_for("mymaster", username="app", password="app123")
primary.set("app:k", "v")           # reconnects to the new primary after a failover
replica = s.slave_for("mymaster", username="app", password="app123")   # for stale-tolerant reads

During the failover itself (seconds), writes fail. Clients should retry with backoff, and the application has to tolerate a short window of errors.

Lost writes and split brain

Sentinel doesn't change the fact that replication is asynchronous. Writes the old primary acknowledged but hadn't yet sent are lost when a replica is promoted. Worse is a network partition:

[ primary + client A ]  ||  partition  ||  [ replica-1, replica-2, sentinels, client B ]

Sentinels promote replica-1. Now there are two primaries:
client A keeps writing to the old primary (it's still up, just isolated)
client B writes to replica-1
When the partition heals, the old primary becomes a replica and its writes are discarded.

The mitigation is to make an isolated primary stop accepting writes:

min-replicas-to-write 1
min-replicas-max-lag 10
# an isolated primary that hasn't heard from any replica for 10s refuses writes

This bounds the loss to roughly min-replicas-max-lag seconds. It doesn't eliminate it. Redis with Sentinel is a highly available, eventually consistent system, not a strongly consistent one.

Deployment rules

  • At least three Sentinels, on separate machines or zones, so one failure doesn't take out the majority.
  • Not on the same machine as the primary alone: if that machine dies, you lose both the primary and the votes.
  • down-after-milliseconds trades failover speed for false positives: too low, and a GC pause or network blip causes an unnecessary failover.
  • Test failovers regularly. SENTINEL failover mymaster does a controlled one.

Sentinel vs Cluster

Sentinel gives you HA for one dataset that fits on one machine. When the data or write load outgrows a single primary, you need sharding: Redis Cluster, which also has failover built in, so you don't run Sentinel with it.

Lab

  1. docker compose -f compose_prod.yaml --profile ha up -d, then SENTINEL ckquorum mymaster.
  2. In one terminal, follow the Sentinel log: docker logs -f sentinel-1.
  3. docker stop redis. Read the +sdown, +odown, +vote-for-leader, +selected-slave, +promoted-slave, +switch-master events as they happen. Time the failover.
  4. docker start redis and confirm it rejoins as a replica with master_link_status:up.
  5. Run the Python snippet before and after a failover.
  6. Reset the lab: docker compose -f compose_prod.yaml --profile ha down && docker compose -f compose_prod.yaml --profile ha up -d.

On this page