v1.31.0
guíaPatrón · 07_Resilienciaenglishversión en español

Resilience Guide

5 minlectura
1047palabras
10secciones
2026-09-21última edición
3cambios

Version: 1.0.0 Date: 2026-02-10 Author: la organización Purpose: Complete resilience guide for .NET 10 projects


Table of Contents

  1. Introduction
  2. Polly v8+
  3. HttpClientFactory + Resilience
  4. Health Checks
  5. Graceful Shutdown
  6. Retry Strategies
  7. Resilience Checklist
  8. NuGet Packages
  9. References

1. Introduction

1.1 What is resilience

Resilience is the ability of a system to recover from failures and continue functioning acceptably. In distributed systems (APIs, databases, external services), failures are inevitable. A resilient system assumes things will fail and is prepared to handle it.

1.2 Why it matters

  • Applications depend on SQL Server, Azure AD, external services (Banner, Oracle HCM, Sigma)
  • Load-balanced infrastructure requires each instance to handle failures independently
  • Load spikes (enrollments, start of term) can saturate services

1.3 Key resilience patterns

PatternDescriptionAnalogy
RetryRetry a failed operation with wait between attemptsCalling back a busy phone number
Circuit BreakerStop calling a failing service to give it time to recoverCircuit breaker cutting power during electrical overload
TimeoutLimit maximum wait time for an operationNot waiting indefinitely in a queue
BulkheadIsolate failures by limiting resources per service, preventing propagationWatertight compartments in a ship
FallbackProvide an alternative response when the primary operation failsPrepared plan B

2. Polly v8+

2.1 Resilience Pipeline API (v8+)

Polly v8 introduces a new API based on ResiliencePipeline that replaces the old Policy API. The new API is more composable, more efficient, and supports generics natively.

IMPORTANT: Do not use the legacy Policy API (v7). Examples in this document use exclusively the v8+ API.

2.2 Retry: retries with exponential backoff and jitter

using Polly;
using Polly.Retry;

// Configure retry with exponential backoff + jitter
var retryPipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
    .AddRetry(new RetryStrategyOptions<HttpResponseMessage>
    {
        // How many times to retry
        MaxRetryAttempts = 3,

        // Exponential backoff with jitter (recommended)
        BackoffType = DelayBackoffType.Exponential,
        Delay = TimeSpan.FromSeconds(1),        // 1s, 2s, 4s + jitter
        UseJitter = true,                        // Avoid "thundering herd"

        // Which HTTP responses to retry
        ShouldHandle = new PredicateBuilder<HttpResponseMessage>()
            .Handle<HttpRequestException>()
            .Handle<TimeoutRejectedException>()
            .HandleResult(response =>
                response.StatusCode == System.Net.HttpStatusCode.RequestTimeout ||       // 408
                response.StatusCode == System.Net.HttpStatusCode.TooManyRequests ||       // 429
                response.StatusCode >= System.Net.HttpStatusCode.InternalServerError),    // 500+

        // Logging each retry
        OnRetry = args =>
        {
            Log.Warning(
                "Retry {RetryAttempt} of {MaxRetries} after {Delay}ms. Reason: {Outcome}",
                args.AttemptNumber + 1,
                3,
                args.RetryDelay.TotalMilliseconds,
                args.Outcome.Exception?.Message ?? args.Outcome.Result?.StatusCode.ToString());
            return default;
        }
    })
    .Build();

2.3 Circuit Breaker: cut calls to failing services

using Polly.CircuitBreaker;

var circuitBreakerPipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
    .AddCircuitBreaker(new CircuitBreakerStrategyOptions<HttpResponseMessage>
    {
        // Open circuit if 50% of requests fail
        FailureRatio = 0.5,

        // In a 30-second sampling window
        SamplingDuration = TimeSpan.FromSeconds(30),

        // With a minimum of 10 requests to evaluate
        MinimumThroughput = 10,

        // Keep circuit open for 30 seconds
        BreakDuration = TimeSpan.FromSeconds(30),

        // What is considered a failure
        ShouldHandle = new PredicateBuilder<HttpResponseMessage>()
            .Handle<HttpRequestException>()
            .Handle<TimeoutRejectedException>()
            .HandleResult(r => r.StatusCode >= System.Net.HttpStatusCode.InternalServerError),

        // Circuit breaker events
        OnOpened = args =>
        {
            Log.Warning("Circuit Breaker OPENED. Duration: {BreakDuration}s. Reason: {Outcome}",
                args.BreakDuration.TotalSeconds,
                args.Outcome.Exception?.Message ?? "error responses");
            return default;
        },
        OnClosed = args =>
        {
            Log.Information("Circuit Breaker CLOSED. Service recovered");
            return default;
        },
        OnHalfOpened = args =>
        {
            Log.Information("Circuit Breaker HALF-OPEN. Testing service...");
            return default;
        }
    })
    .Build();

2.4 Timeout: limit wait time

using Polly.Timeout;

var timeoutPipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
    .AddTimeout(new TimeoutStrategyOptions
    {
        // Timeout per individual request
        Timeout = TimeSpan.FromSeconds(10),

        OnTimeout = args =>
        {
            Log.Warning("Timeout reached after {Timeout}s", args.Timeout.TotalSeconds);
            return default;
        }
    })
    .Build();

2.5 Bulkhead: limit concurrent requests

using Polly.RateLimiting;

var bulkheadPipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
    .AddConcurrencyLimiter(new ConcurrencyLimiterOptions
    {
        // Maximum 25 concurrent requests to this service
        PermitLimit = 25,

        // Queue of 50 waiting requests
        QueueLimit = 50
    })
    .Build();

Order matters. They execute from outside in: Total timeout > Retry > Circuit Breaker > Per-attempt timeout.

// Combined pipeline with correct order
var resiliencePipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
    // 1. Total timeout (for entire operation including retries)
    .AddTimeout(new TimeoutStrategyOptions
    {
        Timeout = TimeSpan.FromSeconds(60),
        Name = "TotalTimeout"
    })
    // 2. Retry (retries the operation)
    .AddRetry(new RetryStrategyOptions<HttpResponseMessage>
    {
        MaxRetryAttempts = 3,
        BackoffType = DelayBackoffType.Exponential,
        Delay = TimeSpan.FromSeconds(1),
        UseJitter = true,
        ShouldHandle = new PredicateBuilder<HttpResponseMessage>()
            .Handle<HttpRequestException>()
            .Handle<TimeoutRejectedException>()
            .HandleResult(r => (int)r.StatusCode >= 500),
        Name = "Retry"
    })
    // 3. Circuit Breaker (protects target service)
    .AddCircuitBreaker(new CircuitBreakerStrategyOptions<HttpResponseMessage>
    {
        FailureRatio = 0.5,
        SamplingDuration = TimeSpan.FromSeconds(30),
        MinimumThroughput = 10,
        BreakDuration = TimeSpan.FromSeconds(30),
        ShouldHandle = new PredicateBuilder<HttpResponseMessage>()
            .Handle<HttpRequestException>()
            .HandleResult(r => (int)r.StatusCode >= 500),
        Name = "CircuitBreaker"
    })
    // 4. Timeout per individual attempt
    .AddTimeout(new TimeoutStrategyOptions
    {
        Timeout = TimeSpan.FromSeconds(10),
        Name = "AttemptTimeout"
    })
    .Build();

2.7 Note about Polly v8 vs v7

AspectPolly v7 (legacy)Polly v8+ (current)
APIPolicy.Handle<T>().WaitAndRetryAsync()ResiliencePipelineBuilder.AddRetry()
CompositionPolicy.WrapAsync()Fluent builder with .Add*()
PerformanceReflectionOptimized, fewer allocations
GenericsLimitedNative
Microsoft.ExtensionsSeparateIntegrated

3. HttpClientFactory + Resilience

3.1 Standard Resilience Handler

.NET 10 includes Microsoft.Extensions.Http.Resilience which integrates Polly directly with HttpClientFactory.

// Program.cs - Standard resilience (recommended for most cases)
builder.Services.AddHttpClient("BannerApi", client =>
{
    client.BaseAddress = new Uri(builder.Configuration["BannerApi:BaseUrl"]!);
    client.DefaultRequestHeaders.Add("Accept", "application/json");
})
.AddStandardResilienceHandler();
// ^ Includes: Retry (3), Circuit Breaker, Timeout (30s), Bulkhead, Total Timeout

3.2 Custom resilience per service

// Custom configuration for a critical service
builder.Services.AddHttpClient("OracleHCM", client =>
{
    client.BaseAddress = new Uri(builder.Configuration["OracleHCM:BaseUrl"]!);
})
.AddResilienceHandler("oracle-resilience", builder =>
{
    // Longer total timeout for Oracle
    builder.AddTimeout(new TimeoutStrategyOptions
    {
        Timeout = TimeSpan.FromSeconds(90)
    });

    builder.AddRetry(new HttpRetryStrategyOptions
    {
        MaxRetryAttempts = 2,
        BackoffType = DelayBackoffType.Exponential,
        Delay = TimeSpan.FromSeconds(2),
        UseJitter = true
    });

    builder.AddCircuitBreaker(new HttpCircuitBreakerStrategyOptions
    {
        FailureRatio = 0.3,           // Oracle is more sensitive
        SamplingDuration = TimeSpan.FromSeconds(60),
        MinimumThroughput = 5,
        BreakDuration = TimeSpan.FromSeconds(60)
    });

    builder.AddTimeout(new TimeoutStrategyOptions
    {
        Timeout = TimeSpan.FromSeconds(30)
    });
});

3.3 Configuration via appsettings.json

{
  "Resilience": {
    "BannerApi": {
      "Retry": {
        "MaxRetryAttempts": 3,
        "BackoffType": "Exponential",
        "DelaySeconds": 1,
        "UseJitter": true
      },
      "CircuitBreaker": {
        "FailureRatio": 0.5,
        "SamplingDurationSeconds": 30,
        "MinimumThroughput": 10,
        "BreakDurationSeconds": 30
      },
      "Timeout": {
        "TimeoutSeconds": 10
      },
      "TotalTimeout": {
        "TimeoutSeconds": 60
      }
    }
  }
}

4. Health Checks

4.1 Three mandatory endpoints

EndpointPurposeWhat it validatesUsed by
/healthGeneral statusEverythingDashboards, monitoring
/health/readyReady to receive trafficDB, external dependenciesLoad balancer, Kubernetes readiness
/health/liveApplication is aliveBasic self-checkKubernetes liveness, supervisor

4.2 Difference between liveness and readiness

  • Liveness: "Is the process alive?". If it fails, restart the instance.
  • Readiness: "Can it receive traffic?". If it fails, remove from load balancer (don't restart).

Example: application is alive (liveness=OK) but SQL Server is down (readiness=FAIL). Load balancer stops sending traffic but doesn't restart the instance.

4.3 Complete configuration

// Program.cs

// =============================================
// Register health checks
// =============================================
builder.Services.AddHealthChecks()
    // MANDATORY: SQL Server (readiness)
    .AddSqlServer(
        connectionString: builder.Configuration.GetConnectionString("DefaultConnection")!,
        healthQuery: "SELECT 1",
        name: "sqlserver",
        failureStatus: HealthStatus.Unhealthy,
        tags: ["ready", "db"])

    // MANDATORY: Self check (liveness)
    .AddCheck("self", () => HealthCheckResult.Healthy("Application active"),
        tags: ["live"])

    // OPTIONAL: Redis (if used)
    .AddRedis(
        redisConnectionString: builder.Configuration.GetConnectionString("Redis")!,
        name: "redis",
        failureStatus: HealthStatus.Degraded,
        tags: ["ready", "cache"])

    // OPTIONAL: Azure Blob Storage (if used)
    .AddAzureBlobStorage(
        connectionString: builder.Configuration.GetConnectionString("AzureStorage")!,
        name: "azure-blob",
        failureStatus: HealthStatus.Degraded,
        tags: ["ready", "storage"])

    // OPTIONAL: External API
    .AddUrlGroup(
        new Uri(builder.Configuration["BannerApi:HealthUrl"]!),
        name: "banner-api",
        failureStatus: HealthStatus.Degraded,
        tags: ["ready", "external"]);

// =============================================
// Map endpoints
// =============================================
var app = builder.Build();

// General endpoint
app.MapHealthChecks("/health", new HealthCheckOptions
{
    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});

// Readiness: only checks with "ready" tag
app.MapHealthChecks("/health/ready", new HealthCheckOptions
{
    Predicate = check => check.Tags.Contains("ready"),
    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});

// Liveness: only checks with "live" tag
app.MapHealthChecks("/health/live", new HealthCheckOptions
{
    Predicate = check => check.Tags.Contains("live"),
    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});

4.4 Custom health check

public class BannerApiHealthCheck : IHealthCheck
{
    private readonly IHttpClientFactory _httpClientFactory;

    public BannerApiHealthCheck(IHttpClientFactory httpClientFactory)
    {
        _httpClientFactory = httpClientFactory;
    }

    public async Task<HealthCheckResult> CheckHealthAsync(
        HealthCheckContext context,
        CancellationToken cancellationToken = default)
    {
        try
        {
            var client = _httpClientFactory.CreateClient("BannerApi");
            var response = await client.GetAsync("/health",
                cancellationToken);

            if (response.IsSuccessStatusCode)
            {
                return HealthCheckResult.Healthy("Banner API available");
            }

            return HealthCheckResult.Degraded(
                $"Banner API responds with {response.StatusCode}");
        }
        catch (Exception ex)
        {
            return HealthCheckResult.Unhealthy(
                "Banner API unavailable",
                exception: ex);
        }
    }
}

// Register
builder.Services.AddHealthChecks()
    .AddCheck<BannerApiHealthCheck>("banner-api", tags: ["ready", "external"]);

4.5 Health check security

Health checks should NOT expose sensitive information:

// In production, limit information
app.MapHealthChecks("/health", new HealthCheckOptions
{
    // Don't show details in production
    ResponseWriter = async (context, report) =>
    {
        context.Response.ContentType = "application/json";

        var result = new
        {
            status = report.Status.ToString(),
            // Only show check names, not details
            checks = report.Entries.Select(e => new
            {
                name = e.Key,
                status = e.Value.Status.ToString()
                // DO NOT include: e.Value.Description, e.Value.Exception
            })
        };

        await context.Response.WriteAsJsonAsync(result);
    }
})
.RequireAuthorization(); // Protect with authentication in production

5. Graceful Shutdown

5.1 IHostApplicationLifetime

// Program.cs - Configure graceful shutdown
var app = builder.Build();

var lifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();

lifetime.ApplicationStarted.Register(() =>
{
    Log.Information("Application started");
});

lifetime.ApplicationStopping.Register(() =>
{
    Log.Warning("Application stopping... Draining requests");
});

lifetime.ApplicationStopped.Register(() =>
{
    Log.Information("Application stopped correctly");
});

// Configure shutdown timeout
builder.WebHost.ConfigureKestrel(options =>
{
    // Timeout to close existing connections
    options.Limits.KeepAliveTimeout = TimeSpan.FromSeconds(30);
});

// Global shutdown timeout
builder.Host.ConfigureHostOptions(options =>
{
    options.ShutdownTimeout = TimeSpan.FromSeconds(30);
});

5.2 Drain requests in BackgroundService

public class OrderProcessorWorker : BackgroundService
{
    private readonly ILogger<OrderProcessorWorker> _logger;
    private readonly IServiceScopeFactory _scopeFactory;
    private readonly ConcurrentDictionary<Guid, Task> _activeTasks = new();

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            try
            {
                var taskId = Guid.NewGuid();
                var task = ProcessNextItemAsync(stoppingToken);
                _activeTasks.TryAdd(taskId, task);

                _ = task.ContinueWith(_ => _activeTasks.TryRemove(taskId, out _));

                await Task.Delay(1000, stoppingToken);
            }
            catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
            {
                break;
            }
        }
    }

    public override async Task StopAsync(CancellationToken cancellationToken)
    {
        _logger.LogWarning("Shutdown requested. Waiting for {Count} active tasks...",
            _activeTasks.Count);

        // Wait for active tasks to complete (with timeout)
        if (_activeTasks.Any())
        {
            var pendingTasks = _activeTasks.Values.ToArray();
            var timeoutTask = Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);

            await Task.WhenAny(Task.WhenAll(pendingTasks), timeoutTask);

            var remaining = _activeTasks.Count;
            if (remaining > 0)
            {
                _logger.LogError("Shutdown timeout. {Count} tasks not completed", remaining);
            }
        }

        _logger.LogInformation("Worker stopped correctly");
        await base.StopAsync(cancellationToken);
    }

    private async Task ProcessNextItemAsync(CancellationToken ct)
    {
        // Process item...
    }
}

5.3 Signal handling

.NET automatically handles system signals when using the generic host:

SignalPlatform.NET Action
SIGTERMLinux / Docker / KubernetesTriggers ApplicationStopping, starts shutdown
SIGINT (Ctrl+C)AllTriggers ApplicationStopping, starts shutdown
Windows Service StopWindowsTriggers ApplicationStopping, starts shutdown

5.4 Load balancer deregistration

Recommended shutdown process:

1. Load balancer sends SIGTERM
2. Application marks /health/ready as Unhealthy
3. Load balancer stops sending new requests
4. Application drains existing requests (max 30s)
5. Application closes connections and frees resources
6. Process terminates
// Mark as not-ready during shutdown
public class ReadinessHealthCheck : IHealthCheck
{
    private static volatile bool _isReady = true;

    public static void SetNotReady() => _isReady = false;

    public Task<HealthCheckResult> CheckHealthAsync(
        HealthCheckContext context,
        CancellationToken cancellationToken = default)
    {
        return Task.FromResult(_isReady
            ? HealthCheckResult.Healthy("Ready")
            : HealthCheckResult.Unhealthy("Shutting down"));
    }
}

// In Program.cs
lifetime.ApplicationStopping.Register(() =>
{
    ReadinessHealthCheck.SetNotReady();
    // Give load balancer time to detect the change
    Thread.Sleep(5000);
});

6. Retry Strategies

6.1 Strategy comparison

StrategyDelay between retriesWhen to useRisk
Immediate0, 0, 0Never in productionOverloads the service
Fixed interval2s, 2s, 2sRarelyThundering herd
Exponential1s, 2s, 4sAcceptableSynchronized retries
Exponential + jitter1.2s, 2.7s, 3.9sRECOMMENDEDLow risk

Jitter adds random variation to delay to prevent multiple instances from retrying at the same time (thundering herd problem).

6.2 When to retry

Retry (transient failures):

HTTP CodeDescriptionRetriable
408Request TimeoutYes
429Too Many RequestsYes (respect Retry-After header)
500Internal Server ErrorYes
502Bad GatewayYes
503Service UnavailableYes
504Gateway TimeoutYes

DO NOT retry (permanent failures):

HTTP CodeDescriptionWhy not retry
400Bad RequestSent data is incorrect
401UnauthorizedInvalid credentials
403ForbiddenNo permissions
404Not FoundResource doesn't exist
409ConflictState conflict
422Unprocessable EntityValidation failed

6.3 Idempotency

Before retrying a write operation, ensure it's idempotent (executing it multiple times produces the same result):

// IDEMPOTENT: Use a unique IdempotencyKey per operation
public class CreateScholarshipCommand
{
    // If retry resends the same command, server detects duplicate
    public Guid IdempotencyKey { get; init; } = Guid.NewGuid();
    public string Name { get; init; } = string.Empty;
    public decimal Amount { get; init; }
}

// In handler
public async Task<ScholarshipDto> Handle(CreateScholarshipCommand request, CancellationToken ct)
{
    // Check if this operation was already processed
    var existing = await _repository.GetByIdempotencyKeyAsync(request.IdempotencyKey, ct);
    if (existing is not null)
    {
        return existing.ToDto(); // Return previous result without duplicating
    }

    // Process normally...
}

7. Resilience Checklist

Before going to production

  • Health checks configured: /health, /health/ready, /health/live
  • SQL Server health check registered as mandatory
  • Polly resilience pipelines for all HttpClient calling external services
  • Circuit breaker configured for each external service (Banner, Oracle HCM, Sigma)
  • Timeout configured in all HTTP calls (30s default, adjust per service)
  • Retry with exponential backoff + jitter (3 attempts maximum)
  • Graceful shutdown implemented with 30-second timeout
  • Resilience logs: each retry, each circuit breaker open/close
  • Write operations with retry are idempotent
  • Health check UI enabled only in development (don't expose in production)
  • Production health checks protected with authentication or IP whitelist
  • AddStandardResilienceHandler() as minimum for HttpClients without specific configuration
  • Alerts configured for prolonged open circuit breakers

8. NuGet Packages

PackageVersionPurpose
Microsoft.Extensions.Http.Resilience10.*Polly + HttpClientFactory integration (.NET 10)
Polly.Core8.*Polly v8 core (ResiliencePipeline)
Polly.Extensions8.*Extensions for DI and telemetry
Microsoft.Extensions.Http.Polly10.*Polly + HttpClientFactory (legacy alternative)
AspNetCore.HealthChecks.SqlServer8.*Health check for SQL Server
AspNetCore.HealthChecks.Redis8.*Health check for Redis
AspNetCore.HealthChecks.AzureBlobStorage8.*Health check for Azure Blob Storage
AspNetCore.HealthChecks.Uris8.*Health check for external URLs
AspNetCore.HealthChecks.UI8.*UI to visualize health checks (dev only)
AspNetCore.HealthChecks.UI.Client8.*Serializer for detailed responses

9. References

FileContent
.claude/skills/resilience-patterns/Resilience patterns skill
.claude/skills/console/SKILL.mdGraceful shutdown in Worker Services
_patron/01_Estructura_Tecnica/ESTRUCTURA_TECNICA.mdLoad-balanced infrastructure (section 4)
_patron/06_Observabilidad/GUIA_OBSERVABILIDAD.mdResilience logging and metrics

External documentation

ResourceURL
Polly v8 Documentationgithub.com/App-vNext/Polly
Microsoft Resilience Extensionslearn.microsoft.com/dotnet/core/resilience
Health Checks in ASP.NET Corelearn.microsoft.com/aspnet/core/host-and-deploy/health-checks
Cloud Design Patterns - Retrylearn.microsoft.com/azure/architecture/patterns/retry
Cloud Design Patterns - Circuit Breakerlearn.microsoft.com/azure/architecture/patterns/circuit-breaker

Document generated: 2026-02-10 la organización

Fuente: ovillo/_patron/07_Resiliencia/GUIA_RESILIENCIA_EN.md

Ovillo v1.31.0tag v1.31.0 (2026-10-06) · sin decisiones posteriores69 skills · 19 agents · 22 hooks · 3 reglas · 15 tools · 103 decisionesfuente: ovillo/_patron/07_Resiliencia/GUIA_RESILIENCIA_EN.mdCopyright 2026 Hugo Valer Rojas · Apache-2.0