Skip to content

Commit 2005c49

Browse files
Sumit Ghoshnishanil
andauthored
Fix/23225 - Updates code snippet to include new jitter strategy (#23545)
* Included Jitter property related changes. * Updated jitter strategy code sample * Fixed lint error * Updated sentense * Update docs/architecture/microservices/implement-resilient-applications/implement-http-call-retries-exponential-backoff-polly.md Co-authored-by: Nish Anil <[email protected]>
1 parent cd101ae commit 2005c49

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

docs/architecture/microservices/implement-resilient-applications/implement-http-call-retries-exponential-backoff-polly.md

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,16 @@ With Polly, you can define a Retry policy with the number of retries, the expone
4646

4747
## Add a jitter strategy to the retry policy
4848

49-
A regular Retry policy can impact your system in cases of high concurrency and scalability and under high contention. To overcome peaks of similar retries coming from many clients in case of partial outages, a good workaround is to add a jitter strategy to the retry algorithm/policy. This can improve the overall performance of the end-to-end system by adding randomness to the exponential backoff. This spreads out the spikes when issues arise. The principle is illustrated by the following example:
49+
A regular Retry policy can affect your system in cases of high concurrency and scalability and under high contention. To overcome peaks of similar retries coming from many clients in partial outages, a good workaround is to add a jitter strategy to the retry algorithm/policy. This strategy can improve the overall performance of the end-to-end system. As recommended in [Polly: Retry with Jitter](https://github.com/App-vNext/Polly/wiki/Retry-with-jitter), a good jitter strategy can be implemented by smooth and evenly distributed retry intervals applied with a well-controlled median initial retry delay on an exponential backoff. This approach helps to spread out the spikes when the issue arises. The principle is illustrated by the following example:
5050

5151
```csharp
52-
Random jitterer = new Random();
53-
var retryWithJitterPolicy = HttpPolicyExtensions
54-
.HandleTransientHttpError()
55-
.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
56-
.WaitAndRetryAsync(6, // exponential back-off plus some jitter
57-
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))
58-
+ TimeSpan.FromMilliseconds(jitterer.Next(0, 100))
59-
);
60-
```
6152

62-
Polly provides production-ready jitter algorithms via the project website.
53+
var delay = Backoff.DecorrelatedJitterBackoffV2(medianFirstRetryDelay: TimeSpan.FromSeconds(1), retryCount: 5);
54+
55+
var retryPolicy = Policy
56+
.Handle<FooException>()
57+
.WaitAndRetryAsync(delay);
58+
```
6359

6460
## Additional resources
6561

@@ -72,9 +68,6 @@ Polly provides production-ready jitter algorithms via the project website.
7268
- **Polly (.NET resilience and transient-fault-handling library)**
7369
<https://github.com/App-vNext/Polly>
7470

75-
- **Polly: Retry with Jitter**
76-
<https://github.com/App-vNext/Polly/wiki/Retry-with-jitter>
77-
7871
- **Marc Brooker. Jitter: Making Things Better With Randomness**
7972
<https://brooker.co.za/blog/2015/03/21/backoff.html>
8073

0 commit comments

Comments
 (0)