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 1Sentinels 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.
How a failover happens
- Subjectively down (SDOWN). One Sentinel gets no valid reply from
the primary for
down-after-milliseconds(5 s here). - Objectively down (ODOWN). At least quorum (2) Sentinels agree it's down.
- 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.
- Pick a replica: priority (
replica-priority), then the most replication offset (least data missing), then the lowest run ID. - Promote it with
REPLICAOF NO ONE, repoint the other replicas (parallel-syncsat a time), and publish the new address. - When the old primary comes back, Sentinel makes it a replica of the new one.
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:upThe first time this lab ran, the old primary came back as a replica
with master_link_status:down, permanently. It had no masterauth,
because only the replicas' command lines carried the replication
password. Once it became a replica after the failover, it couldn't
authenticate to the new primary. Every node must carry
masteruser/masterauth, including the current primary, because any
node may become a replica. This repo's redis.conf sets them for that
reason.
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 readsDuring 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 writesThis 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-millisecondstrades failover speed for false positives: too low, and a GC pause or network blip causes an unnecessary failover.- Test failovers regularly.
SENTINEL failover mymasterdoes 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
docker compose -f compose_prod.yaml --profile ha up -d, thenSENTINEL ckquorum mymaster.- In one terminal, follow the Sentinel log:
docker logs -f sentinel-1. docker stop redis. Read the+sdown,+odown,+vote-for-leader,+selected-slave,+promoted-slave,+switch-masterevents as they happen. Time the failover.docker start redisand confirm it rejoins as a replica withmaster_link_status:up.- Run the Python snippet before and after a failover.
- Reset the lab:
docker compose -f compose_prod.yaml --profile ha down && docker compose -f compose_prod.yaml --profile ha up -d.