Redis Sentinel Configuration¶
Redis Sentinel provides high availability (HA) for Redis. In a Sentinel-managed environment, the system consists of a configurable number of Sentinel nodes and a group of Redis servers (the master and its replicas or slaves).^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]
The core responsibilities of Sentinel include monitoring, notification, automatic failover, and configuration provider services. It allows clients to discover the current master node dynamically, which is crucial for maintaining service continuity during a master failure.^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]
Sentinel Setup and Connection¶
To connect to a high-availability Redis cluster, a client must be configured with the addresses of the Sentinel nodes, not the Redis server directly.^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md] In a Python environment using the redis-py library, this is handled by the Sentinel class.^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]
The client typically requires three pieces of information:
1. Sentinels: A list of host and port pairs for the Sentinel nodes (e.g., [('sentinel-0', 5000), ('sentinel-1', 5000)]).
2. Master Name: The specific name of the master group being monitored (e.g., mymaster).
3. Password: The authentication password for the Redis instances.^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]
from redis.sentinel import Sentinel
# Configure the list of Sentinel nodes
sentinel = Sentinel([
('sentinel-0', 5000),
('sentinel-1', 5000),
('sentinel-2', 5000)
], socket_timeout=0.1)
# Retrieve the master connection
master = sentinel.master_for(
'mymaster',
password='a-very-complex-password-here',
socket_timeout=0.1
)
Master and Slave Discovery¶
Once connected to the Sentinel group, the client can dynamically query the network topology to find which node is currently acting as the master and which are slaves.^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]
This discovery process is automatic, allowing the application to adapt to changes in the infrastructure (such as a promoted slave) without manual code updates.
sentinel.discover_master('mymaster'): Returns the address of the current master.^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]sentinel.discover_slaves('mymaster'): Returns a list of the current slave replicas.^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]
Handling Failover and Connection Errors¶
When a master node fails, Sentinels orchestrate a failover, promoting a slave to become the new master. During this brief transition, attempts to write to the old master will result in a redis.exceptions.ConnectionError.^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]
To ensure application stability, clients must implement retry logic. Because the Sentinel cluster needs time to agree on the failure and promote a new node, immediate retries may fail. A robust retry mechanism involves catching connection exceptions, waiting for a short backoff period, and retrying the operation.^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]
The following example demonstrates a retry wrapper:
import redis.exceptions
import time
def redis_command(command, *args):
max_retries = 3
count = 0
backoffSeconds = 5
while True:
try:
return command(*args)
except (redis.exceptions.ConnectionError,
redis.exceptions.TimeoutError):
count += 1
if count > max_retries:
raise
print('Retrying in {} seconds'.format(backoffSeconds))
time.sleep(backoffSeconds)
Related Concepts¶
- [[Redis]]
- [[High Availability]]
- [[Data Persistence]]
Sources¶
400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md