Retry with Backoff Implementation in C#
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Retry with Backoff runs in O(attempts) time and O(1) space, in C# as in every other language.
- ✓Polly is the standard and is now integrated into the framework: Microsoft.Extensions.Http.Resilience adds resilience handlers to HttpClient with AddStandardResilienceHandler, which bundles retry with jitter, a circuit breaker, a timeout and a concurrency limiter in one opinionated pipeline.
- ✓Prefer AddStandardResilienceHandler on the typed HttpClient, it applies the defaults most teams would arrive at anyway, in the right order, with metrics.
- ✓The C# trap: The v7 to v8 rewrite is the practical trap: Policy.Handle<T>().WaitAndRetryAsync from almost every existing tutorial is the old API, and mixing it with v8 code produces confusing build errors.
- ✓Pipeline overhead is negligible against a network call and Polly v8 was specifically optimised to reduce allocation compared with v7.
What Retry with Backoff actually is
Retry with Backoff in C#: what you actually reach for
C# implementation
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Polly;
using Polly.Retry;
public static class Resilience
{
// Preferred: retry + breaker + timeout + concurrency limiter, in order.
public static void AddResilientClient(IServiceCollection services) =>
services.AddHttpClient("upstream", c => c.BaseAddress = new Uri("https://api.example.com"))
.AddStandardResilienceHandler();
// Hand-built (Polly v8 API, v7's Policy.Handle<T>() does NOT compile here).
public static ResiliencePipeline<HttpResponseMessage> Pipeline() =>
new ResiliencePipelineBuilder<HttpResponseMessage>()
.AddRetry(new RetryStrategyOptions<HttpResponseMessage>
{
MaxRetryAttempts = 4,
BackoffType = DelayBackoffType.Exponential,
Delay = TimeSpan.FromMilliseconds(200), // BASE, not a fixed wait
UseJitter = true, // DEFAULT IS FALSE
ShouldHandle = new PredicateBuilder<HttpResponseMessage>()
.Handle<HttpRequestException>()
.HandleResult(r => (int)r.StatusCode is 429 or >= 500),
})
.AddTimeout(TimeSpan.FromSeconds(3)) // PER ATTEMPT
.Build();
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe C#-specific trap
Performance in C#, beyond the O(attempts)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which C# type or library should I use for Retry with Backoff?
Polly is the standard and is now integrated into the framework: Microsoft.Extensions.Http.Resilience adds resilience handlers to HttpClient with AddStandardResilienceHandler, which bundles retry with jitter, a circuit breaker, a timeout and a concurrency limiter in one opinionated pipeline. Polly v8 replaced the v7 Policy API with ResiliencePipelineBuilder, so v7 examples do not compile against v8.
What is the most common C# mistake when implementing Retry with Backoff?
The v7 to v8 rewrite is the practical trap: Policy.Handle<T>().WaitAndRetryAsync from almost every existing tutorial is the old API, and mixing it with v8 code produces confusing build errors. The behavioural trap is that Delay in a v8 retry strategy is the BASE delay for exponential backoff, not a fixed wait, and leaving UseJitter at its default of false gives you the synchronised herd. Third, retrying a POST through a shared HttpClient without an idempotency key duplicates side effects exactly as elsewhere.
How does Retry with Backoff actually perform in C#?
Pipeline overhead is negligible against a network call and Polly v8 was specifically optimised to reduce allocation compared with v7. The standard resilience handler also adds a concurrency limiter, which is what prevents retries from amplifying load, the piece hand-written retry loops almost always omit. Total worst-case latency is bounded by the total request timeout in the standard pipeline, which is why that pipeline is safer than a bare retry. Time complexity is O(attempts) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what Devin offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.