How Redis Sentinel Works: Overview and Example
Redis Sentinel is a system that monitors Redis servers to detect failures and automatically promotes a replica to master if the current master fails. It uses
sentinel monitor to watch servers and sentinel failover to switch roles, ensuring high availability without manual intervention.Syntax
Redis Sentinel uses configuration commands to monitor and manage Redis instances. The main commands are:
sentinel monitor <master-name> <ip> <port> <quorum>: Sets up monitoring of a master Redis server.sentinel failover <master-name>: Triggers a manual failover to promote a replica.sentinel get-master-addr-by-name <master-name>: Gets the current master address.
redis
sentinel monitor mymaster 127.0.0.1 6379 2 sentinel failover mymaster sentinel get-master-addr-by-name mymaster
Example
This example shows how to configure Redis Sentinel to monitor a master server and perform automatic failover.
bash
# sentinel.conf sentinel monitor mymaster 127.0.0.1 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 60000 sentinel parallel-syncs mymaster 1 # Start sentinel with: redis-sentinel sentinel.conf # Commands to check master address: redis-cli -p 26379 sentinel get-master-addr-by-name mymaster
Output
127.0.0.1
6379
Common Pitfalls
Common mistakes when using Redis Sentinel include:
- Setting quorum too low, causing false failovers.
- Not configuring enough Sentinel instances for reliable consensus.
- Failing to update client applications to use Sentinel for master discovery.
- Ignoring network partitions that can cause split-brain scenarios.
Always run at least three Sentinel instances and configure clients to query Sentinel for the current master.
redis
## Wrong: Single sentinel with quorum 1 sentinel monitor mymaster 127.0.0.1 6379 1 ## Right: Three sentinels with quorum 2 sentinel monitor mymaster 127.0.0.1 6379 2
Quick Reference
| Command | Description |
|---|---|
| sentinel monitor | Start monitoring a master server |
| sentinel failover | Force a failover to a replica |
| sentinel get-master-addr-by-name | Get current master address |
| sentinel reset | Reset Sentinel state for a master |
| sentinel masters | List all monitored masters |
| sentinel slaves | List replicas of a master |
Key Takeaways
Redis Sentinel monitors Redis masters and replicas to detect failures automatically.
It performs automatic failover by promoting a replica to master when needed.
Configure multiple Sentinel instances with a proper quorum for reliability.
Clients should query Sentinel to discover the current master address.
Avoid network partitions and ensure Sentinel instances can communicate.