|
| 1 | +--- |
| 2 | +title: Error handling |
| 3 | +description: Learn how to handle errors when using NRedisStack. |
| 4 | +linkTitle: Error handling |
| 5 | +weight: 60 |
| 6 | +--- |
| 7 | + |
| 8 | +NRedisStack uses **exceptions** to signal errors. Code examples in the documentation often omit error handling for brevity, but it is essential in production code. This page explains how NRedisStack's error handling works and how to apply common error handling patterns. |
| 9 | + |
| 10 | +For an overview of error types and handling strategies, see [Error handling]({{< relref "/develop/clients/error-handling" >}}). |
| 11 | +See also [Production usage]({{< relref "/develop/clients/dotnet/produsage" >}}) |
| 12 | +for more information on connection management, timeouts, and other aspects of |
| 13 | +app reliability. |
| 14 | + |
| 15 | +## Exception types |
| 16 | + |
| 17 | +NRedisStack throws exceptions to signal errors. Common exception types include: |
| 18 | + |
| 19 | +| Exception | When it occurs | Recoverable | Recommended action | |
| 20 | +|---|---|---|---| |
| 21 | +| `RedisConnectionException` | Connection refused or lost | ✅ | Retry with backoff or fall back | |
| 22 | +| `RedisTimeoutException` | Operation exceeded timeout | ✅ | Retry with backoff | |
| 23 | +| `RedisCommandException` | Invalid command or arguments | ❌ | Fix the command or arguments | |
| 24 | +| `RedisServerException` | Invalid operation on server | ❌ | Fix the operation or data | |
| 25 | + |
| 26 | +See [Categories of errors]({{< relref "/develop/clients/error-handling#categories-of-errors" >}}) |
| 27 | +for a more detailed discussion of these errors and their causes. |
| 28 | + |
| 29 | +## Applying error handling patterns |
| 30 | + |
| 31 | +The [Error handling]({{< relref "/develop/clients/error-handling" >}}) overview |
| 32 | +describes four main patterns. The sections below show how to implement them in |
| 33 | +NRedisStack: |
| 34 | + |
| 35 | +### Pattern 1: Fail fast |
| 36 | + |
| 37 | +Catch specific exceptions that represent unrecoverable errors and re-throw them (see |
| 38 | +[Pattern 1: Fail fast]({{< relref "/develop/clients/error-handling#pattern-1-fail-fast" >}}) |
| 39 | +for a full description): |
| 40 | + |
| 41 | +```csharp |
| 42 | +using NRedisStack; |
| 43 | +using StackExchange.Redis; |
| 44 | + |
| 45 | +var muxer = ConnectionMultiplexer.Connect("localhost:6379"); |
| 46 | +var db = muxer.GetDatabase(); |
| 47 | + |
| 48 | +try { |
| 49 | + var result = db.StringGet("key"); |
| 50 | +} catch (RedisCommandException) { |
| 51 | + // This indicates a bug in the code, such as using |
| 52 | + // StringGet on a list key. |
| 53 | + throw; |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +### Pattern 2: Graceful degradation |
| 58 | + |
| 59 | +Catch specific errors and fall back to an alternative, where possible (see |
| 60 | +[Pattern 2: Graceful degradation]({{< relref "/develop/clients/error-handling#pattern-2-graceful-degradation" >}}) |
| 61 | +for a full description): |
| 62 | + |
| 63 | +```csharp |
| 64 | +try { |
| 65 | + var cachedValue = db.StringGet(key); |
| 66 | + return cachedValue.ToString(); |
| 67 | +} catch (RedisConnectionException) { |
| 68 | + logger.LogWarning("Cache unavailable, using database"); |
| 69 | + |
| 70 | + // Fallback to database |
| 71 | + return database.Get(key); |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### Pattern 3: Retry with backoff |
| 76 | + |
| 77 | +Retry on temporary errors such as timeouts (see |
| 78 | +[Pattern 3: Retry with backoff]({{< relref "/develop/clients/error-handling#pattern-3-retry-with-backoff" >}}) |
| 79 | +for a full description): |
| 80 | + |
| 81 | +```csharp |
| 82 | +const int maxRetries = 3; |
| 83 | +int retryDelay = 100; |
| 84 | + |
| 85 | +for (int attempt = 0; attempt < maxRetries; attempt++) { |
| 86 | + try { |
| 87 | + return db.StringGet(key).ToString(); |
| 88 | + } catch (RedisTimeoutException) { |
| 89 | + if (attempt < maxRetries - 1) { |
| 90 | + Thread.Sleep(retryDelay); |
| 91 | + retryDelay *= 2; // Exponential backoff |
| 92 | + } else { |
| 93 | + throw; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +See also [Timeouts]({{< relref "/develop/clients/dotnet/produsage#timeouts" >}}) |
| 100 | +for more information on configuring timeouts in NRedisStack. |
| 101 | + |
| 102 | +### Pattern 4: Log and continue |
| 103 | + |
| 104 | +Log non-critical errors and continue (see |
| 105 | +[Pattern 4: Log and continue]({{< relref "/develop/clients/error-handling#pattern-4-log-and-continue" >}}) |
| 106 | +for a full description): |
| 107 | + |
| 108 | +```csharp |
| 109 | +try { |
| 110 | + db.StringSet(key, value, TimeSpan.FromSeconds(3600)); |
| 111 | +} catch (RedisConnectionException) { |
| 112 | + logger.LogWarning($"Failed to cache {key}, continuing without cache"); |
| 113 | + // Application continues normally |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## Async error handling |
| 118 | + |
| 119 | +Error handling works the usual way with `async`/`await`, as shown in the |
| 120 | +example below: |
| 121 | + |
| 122 | +```csharp |
| 123 | +using NRedisStack; |
| 124 | +using StackExchange.Redis; |
| 125 | + |
| 126 | +var muxer = ConnectionMultiplexer.Connect("localhost:6379"); |
| 127 | +var db = muxer.GetDatabase(); |
| 128 | + |
| 129 | +async Task<string> GetWithFallbackAsync(string key) { |
| 130 | + try { |
| 131 | + var result = await db.StringGetAsync(key); |
| 132 | + if (result.HasValue) { |
| 133 | + return result.ToString(); |
| 134 | + } |
| 135 | + } catch (RedisConnectionException) { |
| 136 | + logger.LogWarning("Cache unavailable"); |
| 137 | + return await database.GetAsync(key); |
| 138 | + } |
| 139 | + |
| 140 | + return await database.GetAsync(key); |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +## See also |
| 145 | + |
| 146 | +- [Error handling]({{< relref "/develop/clients/error-handling" >}}) |
| 147 | +- [Production usage]({{< relref "/develop/clients/dotnet/produsage" >}}) |
0 commit comments