IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| Integration with MassTransit or MediatR | Dead Letter Queues | |
π Retry Policies in .NET Core |
A Retry Policy is a resilience strategy used in distributed systems to handle transient failures (temporary issues such as network hiccups, timeouts, or overloaded services). Instead of failing immediately, the system retries the operation after a delay, often with strategies like exponential backoff and jitter.
Hereβs how to configure an HTTP client with retries and exponential backoff:
// Install-Package Polly.Extensions.Http
using Microsoft.Extensions.DependencyInjection;
using Polly;
using Polly.Extensions.Http;
using System;
using System.Net.Http;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient("RetryClient")
.AddPolicyHandler(GetRetryPolicy());
}
private IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
{
return HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(
retryCount: 3,
sleepDurationProvider: attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt)) +
TimeSpan.FromMilliseconds(new Random().Next(0, 100)),
onRetry: (outcome, timespan, retryAttempt, context) =>
{
Console.WriteLine($"Retry {retryAttempt} after {timespan.TotalSeconds} seconds");
});
}
}
Retry Policies are essential for building resilient microservices. In .NET Core, libraries like Polly make it easy to implement retries with exponential backoff and jitter. When combined with circuit breakers and proper monitoring, they significantly improve system reliability.
| Integration with MassTransit or MediatR | Dead Letter Queues | |