Redis Sentinel

Redis Sentinel is a high-availability monitoring system that watches a master-replica setup. When the master goes down, it automatically promotes a replica to take its place, and clients automatically learn the new master’s address.

It does three things:

  1. Monitoring: Sends a PING to every Redis node each second to see which ones aren’t responding.
  2. Failover: When the master fails, it picks one of the replicas and promotes it to be the new master.
  3. Notification: Pushes the new master’s address to clients and to the other replicas.

A typical Sentinel architecture looks like this:

  • 3 Sentinel nodes form a cluster and communicate with each other
  • 1 Redis master handles write requests
  • 2 Redis replicas handle read requests and keep data backups
  • The Sentinel nodes monitor the status of all Redis nodes
  • When the master fails, Sentinel elects a new master and notifies clients

In production, deploy at least 3 Sentinel nodes, and always an odd number, so votes are unlikely to end in a tie.

Sentinel only handles failover. Cluster handles both failover and data sharding.

Split-brain happen in a Redis cluster

During a network partition, a Redis cluster can end up with more than one master accepting writes at once, which makes the data inconsistent.

Here’s a typical scenario. The network between a master and its replica goes down. Sentinel sees that it can’t reach the master and promotes a replica to be the new master. But the original master is still running, and only the network link is broken. Now there are two masters, and whichever one a client writes to, the data becomes inconsistent.

It gets worse when the network recovers. The old master is demoted to a replica and syncs its data from the new master. Any data that clients wrote to the old master in the meantime is simply lost.

Redis provides two parameters to reduce the split-brain problem:

  • min-slaves-to-write: the master must have at least N replicas confirm before it will accept a write.
  • min-slaves-max-lag: the maximum lag, in seconds, a replica is allowed. A replica that lags more than this no longer counts as a valid replica.

For example, with min-slaves-to-write=2 and min-slaves-max-lag=10, the master accepts writes only when at least 2 replicas are lagging by no more than 10 seconds. If split-brain happens, the isolated master no longer has enough replicas, so it refuses writes.