22title : Error handling
33description : Learn how to handle errors when using redis-py
44linkTitle : Error handling
5- weight : 50
5+ weight : 65
66---
77
88redis-py uses ** exceptions** to signal errors. The redis-py documentation mainly
@@ -53,7 +53,7 @@ redis-py:
5353
5454### Pattern 1: Fail fast
5555
56- Catch specific exceptions and re-raise them (see
56+ Catch specific exceptions that represent unrecoverable errors and re-raise them (see
5757[ Pattern 1: Fail fast] ({{< relref "/develop/clients/error-handling#pattern-1-fail-fast" >}})
5858for a full description):
5959
@@ -65,7 +65,7 @@ r = redis.Redis()
6565try :
6666 result = r.get(key)
6767except redis.ResponseError:
68- # This indicates a bug in our code
68+ # This indicates a bug in the code
6969 raise
7070```
7171
@@ -91,24 +91,12 @@ return database.get(key)
9191
9292Retry on temporary errors like timeouts (see
9393[ Pattern 3: Retry with backoff] ({{< relref "/develop/clients/error-handling#pattern-3-retry-with-backoff" >}})
94- for a full description):
95-
96- ``` python
97- import time
98-
99- max_retries = 3
100- retry_delay = 0.1
101-
102- for attempt in range (max_retries):
103- try :
104- return r.get(key)
105- except redis.TimeoutError:
106- if attempt < max_retries - 1 :
107- time.sleep(retry_delay)
108- retry_delay *= 2 # Exponential backoff
109- else :
110- raise
111- ```
94+ for a full description). redis-py has built-in retry logic
95+ which is highly configurable. You can customize the retry strategy
96+ (or supply your own custom strategy) and you can also specify which errors
97+ should be retried. See
98+ [ Production usage] ({{< relref "/develop/clients/redis-py/produsage#retries" >}})
99+ for more information.
112100
113101### Pattern 4: Log and continue
114102
@@ -126,7 +114,8 @@ except redis.ConnectionError:
126114
127115## Async error handling
128116
129- If you're using ` redis.asyncio ` , error handling works the same way, but with ` async ` /` await ` :
117+ Error handling works the usual way when you use ` async ` /` await ` ,
118+ as shown in the example below:
130119
131120``` python
132121import redis.asyncio as redis
@@ -142,22 +131,7 @@ async def get_with_fallback(key):
142131 await r.close()
143132```
144133
145- ## Connection pool errors
146-
147- Connection pool exhaustion raises a ` redis.ConnectionError ` . Monitor pool usage
148- and adjust the pool size if necessary:
149-
150- ``` python
151- pool = redis.ConnectionPool(
152- host = " localhost" ,
153- port = 6379 ,
154- max_connections = 50 # Adjust based on your needs
155- )
156- r = redis.Redis(connection_pool = pool)
157- ```
158-
159134## See also
160135
161136- [ Error handling] ({{< relref "/develop/clients/error-handling" >}})
162137- [ Production usage] ({{< relref "/develop/clients/redis-py/produsage" >}})
163- - [ Connection pooling] ({{< relref "/develop/clients/pools-and-muxing" >}})
0 commit comments