Polly v8 Rewrote Itself From Scratch. Here's What Actually Changed
Polly showed up briefly in an earlier post on modernising a legacy enterprise codebase on this blog, wiring resilience into a set of external calls that used to fail with nothing but a bare try/catch. It deserved more room than a section of a bigger post, because v8 wasn't an incremental update to v7. It didn't just add features to the fluent Policy API: it introduced a different resilience…
The transition from Polly v7 to Polly v8 represents a complete overhaul of the resilience model, introducing a new pipeline-based approach. Unlike v7, which used a policy object chained together through a fluent API, v8 introduces a ResiliencePipeline built from named strategies, offering a more modular and composable architecture.
With Polly v8, each resilience strategy - such as retry, circuit breaker, timeout, fallback, hedging, and rate limiting - is configured through its own options type and added to the ResiliencePipelineBuilder. The pipeline is then executed once, with strategies composing through a single ResiliencePipeline. This approach eliminates the need for nested policy-wrap calls and offers a more streamlined and efficient implementation.
The six strategies in Polly v8 are split into reactive and proactive categories. Reactive strategies respond to failures that have already occurred, including retry, circuit breaker, fallback, and hedging. Proactive strategies, on the other hand, aim to prevent failures from happening in the first place and include timeout and rate limiting.
One notable difference is the introduction of a TimeProvider in Polly v8, which allows time-based strategies to use a custom clock instead of relying on the real clock. This enables testing strategies without waiting for real delays. In the example provided, the RetryDemo class demonstrates how to use a TimeProvider with the Retry strategy, configuring it with a specified maximum retry attempts, delay, and backoff type.
The ShouldHandle predicate can be used to narrow the exceptions that the Retry strategy should handle, defaulting to handling any exception except OperationCanceledException.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.