Skip to content

Commit c7cf7ab

Browse files
DOC-5288 added retries to NRedisStack prod usage advice
1 parent 2b8da43 commit c7cf7ab

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

content/develop/clients/dotnet/produsage.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ progress in implementing the recommendations.
2828
{{< checklist-item "#event-handling" >}}Event handling{{< /checklist-item >}}
2929
{{< checklist-item "#timeouts" >}}Timeouts{{< /checklist-item >}}
3030
{{< checklist-item "#exception-handling" >}}Exception handling{{< /checklist-item >}}
31+
{{< checklist-item "#retries" >}}Retries{{< /checklist-item >}}
3132
{{< /checklist >}}
3233

3334
## Recommendations
@@ -110,3 +111,68 @@ the most common Redis exceptions:
110111
(for example, trying to access a
111112
[stream entry]({{< relref "/develop/data-types/streams#entry-ids" >}})
112113
using an invalid ID).
114+
115+
### Retries
116+
117+
During the initial `ConnectionMultiplexer.Connect()` call, `NRedisStack` will
118+
keep trying to connect if the first attempt fails. By default, it will make
119+
three attempts, but you can configure the number of retries using the
120+
`ConnectRetry` configuration option:
121+
122+
```cs
123+
var muxer = ConnectionMultiplexer.Connect(new ConfigurationOptions {
124+
ConnectRetry = 5, // Retry up to five times.
125+
.
126+
.
127+
});
128+
```
129+
130+
After the initial `Connect()` call is successful, `NRedisStack` will
131+
automatically attempt to reconnect if the connection is lost. You can
132+
specify a reconnection strategy with the `ReconnectRetryPolicy` configuration
133+
option. `NRedisStack` provides two built-in classes that implement
134+
reconnection strategies:
135+
136+
- `ExponentialRetry`: (Default) Uses an
137+
[exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff)
138+
strategy, where you specify an increment to the delay between successive
139+
attempts and, optionally, a maximum delay, both in milliseconds.
140+
- `LinearRetry`: Uses a linear backoff strategy with a fixed delay between
141+
attempts, in milliseconds.
142+
143+
The example below shows how to use the `ExponentialRetry` class:
144+
145+
```cs
146+
var muxer = ConnectionMultiplexer.Connect(new ConfigurationOptions {
147+
// 500ms increment per attempt, max 2000ms.
148+
ReconnectRetryPolicy = new ExponentialRetry(500, 2000),
149+
.
150+
.
151+
});
152+
```
153+
154+
You can also implement your own custom retry policy by creating a class
155+
that implements the `IReconnectRetryPolicy` interface.
156+
157+
`NRedisStack` doesn't provide an automated retry mechanism for commands, but
158+
you can implement your own retry logic in your application code. Use
159+
a loop with a `try`/`catch` block to catch `RedisConnectionException` and
160+
`RedisTimeoutException` exceptions and then retry the command after a
161+
suitable delay, as shown in the example below:
162+
163+
```cs
164+
const int MAX_RETRIES = 3;
165+
166+
for (int i = 0; i < MAX_RETRIES; i++) {
167+
try {
168+
string value = db.StringGet("foo");
169+
break;
170+
} catch (RedisConnectionException) {
171+
// Wait before retrying.
172+
Thread.Sleep(500 * (i + 1));
173+
} catch (RedisTimeoutException) {
174+
// Wait before retrying.
175+
Thread.Sleep(500 * (i + 1));
176+
}
177+
}
178+
```

0 commit comments

Comments
 (0)