You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/develop/clients/error-handling.md
+46-40Lines changed: 46 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
---
2
2
title: Error handling
3
-
description: Learn how to handle errors when using Redis client libraries
3
+
description: Learn how to handle errors when using Redis client libraries.
4
4
linkTitle: Error handling
5
5
weight: 50
6
6
---
7
7
8
-
When working with Redis, errors can occur for various reasons—network issues, invalid commands, or resource constraints. This guide explains the types of errors you might encounter and how to handle them effectively.
8
+
When working with Redis, errors can occur for various reasons, including network issues, invalid commands, or resource constraints. This guide explains the types of errors you might encounter and how to handle them effectively.
9
9
10
10
## Categories of errors
11
11
@@ -53,17 +53,20 @@ graph TB
53
53
Command errors occur when Redis receives an invalid or malformed command. These typically indicate a bug in your code.
54
54
55
55
**Common causes:**
56
-
- Typo in command name
57
-
- Wrong number of arguments
58
-
- Invalid argument types
59
-
- Using a command that doesn't exist in your Redis version
56
+
- Typo in command name
57
+
- Wrong number of arguments
58
+
- Invalid argument types (for example, supplying a [string]({{< relref "/develop/
59
+
data-types/strings" >}}) key to a [list]({{< relref "/develop/data-types/lists"
60
+
>}}) command))
61
+
- Using a command that doesn't exist in your Redis version
60
62
61
63
**Examples:**
62
64
-`ResponseError`: Invalid command or syntax error
63
65
-`WRONGTYPE Operation against a key holding the wrong kind of value`
64
66
-`ERR unknown command`
65
67
66
-
**When to handle:** Rarely. These usually indicate a programming error and should be fixed in your code, not handled at runtime. However, some cases (like invalid user input) may warrant handling.
68
+
**When to handle:** Rarely. These usually indicate programming error and so you
69
+
should fix the errors in your code rather than attempt to handle them at runtime. However, some cases (like invalid user input) may be worth handling.
67
70
68
71
**Example:**
69
72
@@ -79,18 +82,17 @@ graph TB
79
82
80
83
### Data errors
81
84
82
-
Data errors occur when there's a problem with the data itself—serialization failures, corrupted data, or type mismatches.
85
+
Data errors occur when there are problems with the data itself, such as
86
+
serialization failures, or data corruption.
83
87
84
88
**Common causes:**
85
89
- Object cannot be serialized to JSON
86
90
- Cached data is corrupted
87
91
- Attempting to deserialize invalid data
88
-
- Type mismatch (e.g., trying to use string operations on a list)
89
92
90
93
**Examples:**
91
94
-`JSONDecodeError`: Cannot deserialize JSON data
92
95
-`SerializationError`: Cannot serialize object
93
-
-`WRONGTYPE`: Operating on wrong data type
94
96
95
97
**When to handle:** Sometimes. If the error is due to user input or external data, handle it gracefully. If it's due to your code, fix the code.
96
98
@@ -140,11 +142,13 @@ graph TB
140
142
Use this when the error is unrecoverable or indicates a bug in your code.
141
143
142
144
**When to use:**
145
+
143
146
- Command errors (invalid syntax)
144
147
- Authentication errors
145
148
- Programming errors
146
149
147
150
**Example:**
151
+
148
152
```python
149
153
try:
150
154
result = r.get(key)
@@ -155,14 +159,17 @@ except redis.ResponseError as e:
155
159
156
160
### Pattern 2: Graceful degradation
157
161
158
-
Use this when you have an alternative way to get the data.
162
+
Use this when you have an alternative way to get the data you need, so you can
163
+
fall back to using the alternative instead of the preferred code.
159
164
160
165
**When to use:**
166
+
161
167
- Cache reads (fallback to database)
162
168
- Session reads (fallback to default values)
163
169
- Optional data (skip if unavailable)
164
170
165
171
**Example:**
172
+
166
173
```python
167
174
try:
168
175
cached_value = r.get(key)
@@ -177,14 +184,17 @@ return database.get(key)
177
184
178
185
### Pattern 3: Retry with backoff
179
186
180
-
Use this when the error is temporary and the operation is idempotent.
187
+
Use this when the error could be due to network load or other temporary
188
+
conditions.
181
189
182
190
**When to use:**
191
+
183
192
- Connection timeouts
184
193
- Temporary network issues
185
194
- Redis loading data
186
195
187
196
**Example:**
197
+
188
198
```python
189
199
import time
190
200
@@ -202,16 +212,23 @@ for attempt in range(max_retries):
202
212
raise
203
213
```
204
214
215
+
Note that client libraries often implement retry logic for you, so
216
+
you may just need to provide the right configuration rather than
217
+
implementing retries yourself. See [Client-specific error handling](#client-specific-error-handling) below for links to pages that
218
+
describe retry configuration for each client library.
219
+
205
220
### Pattern 4: Log and continue
206
221
207
222
Use this when the operation is not critical to your application.
208
223
209
224
**When to use:**
225
+
210
226
- Cache writes (data loss is acceptable)
211
227
- Non-critical updates
212
228
- Metrics collection
213
229
214
230
**Example:**
231
+
215
232
```python
216
233
try:
217
234
r.setex(key, 3600, value)
@@ -247,6 +264,11 @@ graph LR
247
264
248
265
## Logging and monitoring
249
266
267
+
In production, you may find it useful to log errors when they
268
+
occur and monitor the logs for patterns. This can help you identify
269
+
which errors are most common and whether your retry and fallback
270
+
strategies are effective.
271
+
250
272
### What to log
251
273
252
274
-**Error type and message:** What went wrong?
@@ -281,19 +303,22 @@ These metrics help you identify patterns and potential issues.
281
303
282
304
### Catching all exceptions
283
305
284
-
**Problem:** You might catch unexpected errors and hide bugs.
306
+
**Problem:** If you catch all exceptions, you might catch unexpected
307
+
errors and hide bugs.
285
308
286
309
**Example (wrong):**
310
+
287
311
```python
288
312
try:
289
313
result = r.get(key)
290
-
exceptException: # Too broad!
314
+
exceptException: # Too broad - some errors indicate code problems.
291
315
pass
292
316
```
293
317
294
318
**Better approach:** Catch specific exception types.
0 commit comments