Migration Guide from v7 to v8
Welcome to the migration guide for Polly’s v8 release. Version 8 of Polly brings major new enhancements and supports all of the same scenarios as previous versions. In the following sections, we’ll detail the differences between the v7 and v8 APIs, and provide steps on how to transition smoothly.The v7 API is still available and fully supported even when using the v8 version by referencing the Polly package. See v7 Compatibility for more details.
Major Differences
The term Policy is now replaced with Strategy
In previous versions, Polly used the term policy for retries, timeouts, etc. In v8, these are referred to as resilience strategies.Introduction of Resilience Pipelines
A resilience pipeline combines one or more resilience strategies. This is the foundational API for Polly v8, similar to the Policy Wrap in previous versions but integrated into the core API.Unified sync and async flows
Interfaces such asIAsyncPolicy, IAsyncPolicy<T>, ISyncPolicy, ISyncPolicy<T>, and IPolicy are now unified under ResiliencePipeline and ResiliencePipeline<T>. The resilience pipeline supports both synchronous and asynchronous execution flows.
Native async support
Polly v8 was designed with asynchronous support from the start.No static APIs
Unlike previous versions, v8 doesn’t use static APIs. This improves testability and extensibility while maintaining ease of use.Options-based configuration
Configuring individual resilience strategies is now options-based, offering more flexibility and improving maintainability and extensibility.Built-in telemetry
Polly v8 now has built-in telemetry support.Improved performance and low-allocation APIs
Polly v8 brings significant performance enhancements and provides zero-allocation APIs for advanced use cases.Migration Process
When you do your migration process it is recommended to follow these steps:- Upgrade the
Pollypackage version from 7.x to 8.x- Your previous policies should run smoothly without any change
- Migrate your V7 policies to V8 strategies gradually, such as one at a time
- Test your migrated code thoroughly
- After you have successfully migrated all your legacy Polly code then change your package reference from
PollytoPolly.Core
Migrating Execution Policies
This section describes how to migrate from execution policies to resilience pipelines.- v7
- v8
Things to remember:
- Use
ResiliencePipelineBuilder{<TResult>}to build a resiliency pipeline - Use one of the
Add*builder methods to add a new strategy to the pipeline - Use either
ExecuteorExecuteAsyncdepending on the execution context
Migrating Policy Wrap
In v8, there’s no need to use policy wrap explicitly. Instead, policy wrapping is integrated intoResiliencePipelineBuilder.
- v7
- v8
Migrating Retry Policies
Basic Retry
- v7
- v8
Wait and Retry
- v7
- v8
Retry with Result Handling
- v7
- v8
Migrating Rate Limit Policies
The rate limit policy is now replaced by the rate limiter strategy which uses theSystem.Threading.RateLimiting package. Polly does not implement its own rate limiter anymore.
- v7
- v8
Migrating Bulkhead Policies
The bulkhead policy is now replaced by the rate limiter strategy usingConcurrencyLimiter.
In v7, the bulkhead was presented as an individual strategy. In v8, it’s not separately exposed because it’s essentially a specialized type of rate limiter: the
ConcurrencyLimiter.- v7
- v8
Migrating Timeout Policies
- v7
- v8
Migrating Circuit Breaker Policies
- v7 Standard Circuit Breaker
- v7 Advanced Circuit Breaker
- v8
Migrating Context
The successor of thePolly.Context is the ResilienceContext. The major differences:
ResilienceContextis pooled for enhanced performance and cannot be directly created. Instead, use theResilienceContextPoolclass to get an instance.Contextallowed directly custom data attachment, whereasResilienceContextemploys theResilienceContext.Propertiesfor the same purpose.- In order to set or get a custom data you need to utilize the generic
ResiliencePropertyKeystructure.
- v7
- v8
Things to remember:
- Use
ResilienceContextPool.Sharedto get a context and return it back to the pool - Use the
ResiliencePropertyKey<TValue>to define type-safe keys for your custom data
Migrating Policy Registries
In v7, the following registry APIs are exposed:IConcurrentPolicyRegistry<TKey>IPolicyRegistry<TKey>IReadOnlyPolicyRegistry<TKey>PolicyRegistry<TKey>
ResiliencePipelineRegistry<TKey>: Allows adding and accessing resilience pipelinesResiliencePipelineProvider<TKey>: Read-only access to resilience pipelines
- It’s append-only, which means removal of items is not supported to avoid race conditions
- It’s thread-safe and supports features like dynamic reloading and resource disposal
- It allows dynamic creation and caching of resilience pipelines using pre-registered delegates
- Type safety is enhanced, eliminating the need for casting between policy types
- v7
- v8
Polly V8 does not provide an explicit API to directly update a strategy in the registry. On the other hand it does provide a mechanism to reload pipelines dynamically.
Migrating No-Op Policies
Interoperability Between Policies and Resilience Pipelines
In certain scenarios, you might not be able to migrate all your code to the v8 API. For interoperability, you can define V8 strategies and use them with your v7 policies.Summary
Migrating from Polly v7 to v8 involves:- Understanding the shift from policies to strategies
- Using resilience pipelines instead of individual policies or policy wraps
- Adopting options-based configuration for all strategies
- Leveraging unified sync/async execution with a single pipeline instance
- Working with the new context pooling mechanism
- Using the new registry system for managing pipelines