Skip to content

Commit 8ebfc68

Browse files
DOC-5880 improved main error handling page
1 parent 7352dc5 commit 8ebfc68

File tree

1 file changed

+46
-40
lines changed

1 file changed

+46
-40
lines changed

content/develop/clients/error-handling.md

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
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.
44
linkTitle: Error handling
55
weight: 50
66
---
77

8-
When working with Redis, errors can occur for various reasonsnetwork 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.
99

1010
## Categories of errors
1111

@@ -53,17 +53,20 @@ graph TB
5353
Command errors occur when Redis receives an invalid or malformed command. These typically indicate a bug in your code.
5454

5555
**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
6062

6163
**Examples:**
6264
- `ResponseError`: Invalid command or syntax error
6365
- `WRONGTYPE Operation against a key holding the wrong kind of value`
6466
- `ERR unknown command`
6567

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.
6770

6871
**Example:**
6972

@@ -79,18 +82,17 @@ graph TB
7982

8083
### Data errors
8184

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.
8387

8488
**Common causes:**
8589
- Object cannot be serialized to JSON
8690
- Cached data is corrupted
8791
- Attempting to deserialize invalid data
88-
- Type mismatch (e.g., trying to use string operations on a list)
8992

9093
**Examples:**
9194
- `JSONDecodeError`: Cannot deserialize JSON data
9295
- `SerializationError`: Cannot serialize object
93-
- `WRONGTYPE`: Operating on wrong data type
9496

9597
**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.
9698

@@ -140,11 +142,13 @@ graph TB
140142
Use this when the error is unrecoverable or indicates a bug in your code.
141143

142144
**When to use:**
145+
143146
- Command errors (invalid syntax)
144147
- Authentication errors
145148
- Programming errors
146149

147150
**Example:**
151+
148152
```python
149153
try:
150154
result = r.get(key)
@@ -155,14 +159,17 @@ except redis.ResponseError as e:
155159

156160
### Pattern 2: Graceful degradation
157161

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.
159164

160165
**When to use:**
166+
161167
- Cache reads (fallback to database)
162168
- Session reads (fallback to default values)
163169
- Optional data (skip if unavailable)
164170

165171
**Example:**
172+
166173
```python
167174
try:
168175
cached_value = r.get(key)
@@ -177,14 +184,17 @@ return database.get(key)
177184

178185
### Pattern 3: Retry with backoff
179186

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.
181189

182190
**When to use:**
191+
183192
- Connection timeouts
184193
- Temporary network issues
185194
- Redis loading data
186195

187196
**Example:**
197+
188198
```python
189199
import time
190200

@@ -202,16 +212,23 @@ for attempt in range(max_retries):
202212
raise
203213
```
204214

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+
205220
### Pattern 4: Log and continue
206221

207222
Use this when the operation is not critical to your application.
208223

209224
**When to use:**
225+
210226
- Cache writes (data loss is acceptable)
211227
- Non-critical updates
212228
- Metrics collection
213229

214230
**Example:**
231+
215232
```python
216233
try:
217234
r.setex(key, 3600, value)
@@ -247,6 +264,11 @@ graph LR
247264

248265
## Logging and monitoring
249266

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+
250272
### What to log
251273

252274
- **Error type and message:** What went wrong?
@@ -281,19 +303,22 @@ These metrics help you identify patterns and potential issues.
281303

282304
### Catching all exceptions
283305

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.
285308

286309
**Example (wrong):**
310+
287311
```python
288312
try:
289313
result = r.get(key)
290-
except Exception: # Too broad!
314+
except Exception: # Too broad - some errors indicate code problems.
291315
pass
292316
```
293317

294318
**Better approach:** Catch specific exception types.
295319

296320
**Example (correct):**
321+
297322
```python
298323
try:
299324
result = r.get(key)
@@ -304,20 +329,23 @@ except redis.ConnectionError:
304329

305330
### Not distinguishing error types
306331

307-
**Problem:** Different errors need different handling. Retrying a syntax error won't help.
332+
**Problem:** Different errors need different handling. For example, retrying a syntax error won't help.
308333

309334
**Example (wrong):**
335+
310336
```python
311337
try:
312338
result = r.get(key)
313339
except redis.ResponseError:
314-
# Retry? This won't help if it's a syntax error!
340+
# Retry? This won't help if it's a syntax error.
315341
retry()
316342
```
317343

318-
**Better approach:** Handle each error type differently based on whether it's recoverable.
344+
**Better approach:** Handle each error type differently based on whether or not
345+
it is recoverable.
319346

320347
**Example (correct):**
348+
321349
```python
322350
try:
323351
result = r.get(key)
@@ -327,28 +355,12 @@ except redis.ResponseError:
327355
raise # Fail on syntax error
328356
```
329357

330-
### Retrying non-idempotent operations
331-
332-
**Problem:** Retrying non-idempotent operations can cause data corruption. Each retry increments the counter again.
333-
334-
**Example (wrong):**
335-
```python
336-
# This increments the counter each retry!
337-
for attempt in range(3):
338-
try:
339-
r.incr(counter)
340-
break
341-
except redis.TimeoutError:
342-
pass # Retry
343-
```
344-
345-
**Better approach:** Only retry idempotent operations (GET, SET with same value) or use transactions.
346-
347358
### Ignoring connection pool errors
348359

349360
**Problem:** Connection pool errors indicate a configuration or concurrency issue that needs to be addressed.
350361

351362
**Example (wrong):**
363+
352364
```python
353365
# Pool is exhausted, but we don't handle it
354366
result = r.get(key) # Might timeout waiting for connection
@@ -365,9 +377,3 @@ For detailed information about exceptions in your client library, see:
365377
- [Java (Jedis) error handling]({{< relref "/develop/clients/jedis/error-handling" >}})
366378
- [Go (go-redis) error handling]({{< relref "/develop/clients/go/error-handling" >}})
367379
- [.NET (NRedisStack) error handling]({{< relref "/develop/clients/dotnet/error-handling" >}})
368-
369-
## Next steps
370-
371-
- Learn about [connection pooling]({{< relref "/develop/clients/pools-and-muxing" >}}) to avoid resource errors
372-
- Explore [client-side caching]({{< relref "/develop/clients/client-side-caching" >}}) for resilient applications
373-
- See [use-case guides]({{< relref "/develop/use-cases" >}}) for pattern-specific error handling examples

0 commit comments

Comments
 (0)