How to Manage Client Reconnections in case of Errors with redis-py

Last updated 14, Nov 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())

Details of this configuration follow.

  • Backoff Strategy: ExponentialBackoff(cap=10, base=1)
    • The first retry starts with an initial delay of 1 second.
    • The delay doubles after each retry until the maximum delay (cap) of 10 seconds.
  • Retry Count: 25
    • Allows up to 25 retry attempts before stopping.

After the 25th attempt (with a total wait time of 225 seconds), the client will give up and raise the last encountered error.

Attempt Delay Cumulative wait
1 1 1
2 2 3
3 4 7
4 8 15
5 10 (cap reached) 25
... ... ...
24 10 215
25 10 225

References

The redis-py Backoff interface