Video in progress

Strong Middle ⚡ Redis and caching

What does Redis Sentinel do, and how does a Spring application find out the master has changed?

Sentinels watch the master, agree by quorum that it is "dead", promote one of the replicas to master and point the others at it. The application doesn't know the Redis address, only the list of Sentinels, and asks them for the current master.

📖 Answer

The usual setup: 1 master, 2 replicas, 3 Sentinels (on separate machines). The quorum is 2: if two Sentinels don't agree, no failover starts. This prevents a wrong failover caused by a network problem on a single Sentinel.

Spring Boot:

spring:
  data:
    redis:
      sentinel:
        master: mymaster
        nodes: 10.0.0.11:26379,10.0.0.12:26379,10.0.0.13:26379
      password: ${REDIS_PASSWORD}

Lettuce gets the master address from Sentinel and subscribes to the +switch-master event. When a failover happens, it reconnects to the new master.

What to watch for in prod:

  • failover isn't instant: down-after-milliseconds (say, 5 s) plus the election means 5-15 seconds of write errors. The application must be ready for that with timeouts and retries;
  • because replication is asynchronous, the latest writes can be lost;
  • in Docker/Kubernetes, Sentinel may announce an internal IP and the application can't connect; set sentinel announce-ip;
  • Sentinel doesn't shard: all the data has to fit on one master. If you need to split it, use Redis Cluster.

Before going to prod, try a failover once with SENTINEL FAILOVER mymaster and watch the application logs.