How to Manage Client Reconnections in case of Errors with redis-py
Last updated 24, Apr 2024
Question
When a connection to a Redis database cannot be established, e.g., when the max connection is reached, does Redis provide retry mechanisms, or does the client/application need to implement a retrying mechanism?
Answer
Retry mechanisms are usually available in the Redis client library of choice and can be configured using a variety of parameters to achieve the desired behavior. An example is proposed to illustrate the behavior of the redis-py
client library. Connect to the desired cluster node and limit the database's maximum number of connections as follows:
rladmin tune db db:1 max_connections 1
Now, open a connection in another terminal, as an example using redis-cli
, to keep the only connection slot busy. Finally, test the following script. The script will attempt to establish a new connection and fail because no free slot is available. Observe how it will block until the redis-cli
session is released.
import redis
from redis.retry import Retry
from redis.exceptions import (TimeoutError, ConnectionError)
from redis.backoff import ExponentialBackoff
r = redis.Redis(host='127.0.0.1', port=16071, retry=Retry(ExponentialBackoff(cap=10, base=1), 25), retry_on_error=[ConnectionError, TimeoutError, ConnectionResetError], health_check_interval=1)
print(r.ping())
References
The redis-py
Backoff interface