You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
usingPolly;usingPolly.Retry;varclient=newElasticsearchClient(newElasticsearchClientSettings(newUri("https://my-cluster:9200")));varresilient=client.WithPolly(p =>p.AddRetry(newRetryStrategyOptions{MaxRetryAttempts=3,Delay=TimeSpan.FromSeconds(2),BackoffType=DelayBackoffType.Exponential,ShouldHandle=ElasticTransientErrors.IsTransient,}));// Every call is now wrapped in the Polly pipeline.varresponse=awaitresilient.ExecuteAsync((c,ct)=>c.GetAsync<Product>("products","doc-id",ct));
2. Dependency injection
// Program.cs / Startup.csbuilder.Services.AddSingleton(newElasticsearchClient(newElasticsearchClientSettings(newUri("https://my-cluster:9200"))));builder.Services.AddPollyElasticsearch(pipeline =>pipeline.AddRetry(newRetryStrategyOptions{MaxRetryAttempts=3,Delay=TimeSpan.FromSeconds(1),BackoffType=DelayBackoffType.Exponential,ShouldHandle=ElasticTransientErrors.IsTransient,}).AddTimeout(TimeSpan.FromSeconds(10)));// Inject ResilientElasticsearchClient into your servicespublicclassProductService(ResilientElasticsearchClientclient){publicTask<SearchResponse<Product>>SearchAsync(stringq,CancellationTokenct)=>client.ExecuteAsync((c,ct2)=>c.SearchAsync<Product>(s =>s.Index("products").Query(q2 =>q2.Match(m =>m.Field(f =>f.Name).Query(q))),ct2),ct);}
// Use in any Polly strategy:ShouldHandle=ElasticTransientErrors.IsTransient
Condition
Why it's transient
ElasticTransientException (HTTP 429)
Rate limited — back off and retry
ElasticTransientException (HTTP 503)
Cluster down / maintenance — retry later
ElasticTransientException (HTTP 504)
Proxy/load-balancer timeout — retry
TransportException
Network failure or connection refused
Note:ElasticTransientException is thrown automatically by ResilientElasticsearchClient when the response HTTP status code is in ElasticTransientErrors.StatusCodes (429, 503, 504). You do not need to throw it yourself.
client.WithPolly(p =>p.AddTimeout(TimeSpan.FromSeconds(30))// total call timeout.AddRetry(newRetryStrategyOptions{MaxRetryAttempts=4,Delay=TimeSpan.FromSeconds(1),BackoffType=DelayBackoffType.Exponential,UseJitter=true,ShouldHandle=ElasticTransientErrors.IsTransient,}).AddCircuitBreaker(newCircuitBreakerStrategyOptions{FailureRatio=0.5,SamplingDuration=TimeSpan.FromSeconds(30),MinimumThroughput=10,BreakDuration=TimeSpan.FromSeconds(15),ShouldHandle=ElasticTransientErrors.IsTransient,}));
Observability via Polly events
.AddRetry(newRetryStrategyOptions{ShouldHandle=ElasticTransientErrors.IsTransient,OnRetry= args =>{logger.LogWarning("Elasticsearch retry {Attempt} after {Delay}ms — {Exception}",args.AttemptNumber,args.RetryDelay.TotalMilliseconds,args.Outcome.Exception?.Message);returnValueTask.CompletedTask;},})
API reference
ResilientElasticsearchClient
Member
Description
Inner
The underlying ElasticsearchClient
ExecuteAsync<TResponse>(operation, ct)
Executes operation through the pipeline; throws ElasticTransientException for 429/503/504
ElasticTransientErrors
Member
Description
IsTransient
PredicateBuilder for ElasticTransientException + TransportException
StatusCodes
IReadOnlySet<int> — {429, 503, 504}
ElasticTransientException
Member
Description
StatusCode
The HTTP status code that triggered the exception
Message
Human-readable description including the status code
Extension methods
Method
Description
client.WithPolly(pipeline)
Wraps an ElasticsearchClient with a pre-built ResiliencePipeline
client.WithPolly(configure)
Builds a pipeline inline and wraps the client
DI extensions
Method
Description
services.AddPollyElasticsearch(configure)
Registers ResiliencePipeline + ResilientElasticsearchClient (requires ElasticsearchClient already in DI)
services.AddPollyElasticsearch(uri, configure)
Registers ElasticsearchClient for uri, then pipeline + resilient client
ASP.NET Core health checks for Polly v8 circuit breakers — expose circuit-breaker state (Closed, HalfOpen, Open, Isolated) as /health endpoint responses
Polly v8 resilience pipelines for Entity Framework Core — wrap every EF Core query and SaveChanges with retry, timeout and circuit-breaker via a single AddPollyResilience() call
Polly v8 resilience for RabbitMQ.Client v7+ — retry, circuit-breaker, and timeout for IChannel operations, with built-in RabbitMqTransientErrors predicate covering AlreadyClosedException, BrokerUnreachableException, OperationInterruptedException, and ConnectFailureException
Polly v8 resilience pipelines for Npgsql (PostgreSQL) — retry, timeout, and circuit-breaker for NpgsqlConnection queries and commands, plus a built-in PostgresTransientErrors predicate covering all common PostgreSQL transient SQLSTATE codes
Polly v8 resilience pipelines for Azure Cosmos DB — retry, timeout, and circuit-breaker for Container operations, plus a built-in CosmosTransientErrors predicate covering rate limiting (429), timeouts (408), partition failovers (410), and service unavailability (503)
Polly v8 resilience pipelines for MongoDB.Driver — wrap Find, InsertOne, UpdateOne, DeleteOne and other IMongoCollection calls with retry, timeout, circuit-breaker, and more using a single ResilientMongoCollection decorator
Polly v8 resilience pipelines for Dapper — wrap QueryAsync, ExecuteAsync, and other Dapper calls with retry, timeout, circuit-breaker, and more using a single ResilientDbConnection decorator
Polly v8 resilience pipelines for MediatR — add retry, timeout, circuit-breaker, rate-limiting, hedging, and chaos engineering to any MediatR request handler with a single line of DI registration
Polly v8 resilience pipelines for Microsoft.Data.SqlClient (SQL Server and Azure SQL) — retry, timeout, and circuit-breaker for SqlConnection queries and commands, plus a built-in SqlServerTransientErrors predicate covering all common SQL Server and Azure SQL transient error numbers
Polly v8 resilience pipelines for Azure Blob Storage — wrap BlobClient and BlobContainerClient operations with retry, timeout, circuit-breaker, and more using ResilientBlobClient and ResilientBlobContainerClient decorators