Skip to main content
The hedging strategy executes parallel actions when the primary action takes too long, accepting the first successful result.

HedgingStrategyOptions

Configures the hedging resilience strategy.

Properties

TimeSpan
default:"00:00:02"
The maximum waiting time before spawning a new hedged action.Special values:
  • TimeSpan.Zero: Create all hedged actions at once
  • Timeout.InfiniteTimeSpan: Never create a new action before the old one finishes
Use DelayGenerator for greater control over hedging delays.
int
default:"1"
The maximum number of hedged actions to use, in addition to the original action.
  • Must be between 1 and 10
  • Default is 1
Func<HedgingPredicateArguments<TResult>, ValueTask<bool>>
required
A predicate that determines whether hedging should be executed for a given outcome.Default: Hedges on any exception except OperationCanceledException.
Func<HedgingActionGeneratorArguments<TResult>, Func<ValueTask<Outcome<TResult>>>?>
required
A generator that creates hedged actions.Default: Executes the original callback that was passed to the hedging resilience strategy.
Func<HedgingDelayGeneratorArguments, ValueTask<TimeSpan>>?
default:"null"
A generator that produces hedging delays for each hedged action.The DelayGenerator takes precedence over Delay. If specified, Delay is ignored.
Func<OnHedgingArguments<TResult>, ValueTask>?
default:"null"
An event raised when hedging is performed.Hedging is executed when:
  • The current attempt outcome is not successful and ShouldHandle returns true, OR
  • The current attempt did not finish within the Delay

Extension Methods

Add hedging strategies to a resilience pipeline:

OnHedgingArguments

Arguments passed to the OnHedging callback.
  • PrimaryContext: The context received by the hedging strategy
  • ActionContext: A cloned context used for the hedged action
  • AttemptNumber: The zero-based hedging attempt number

Usage Examples

Basic Hedging

Hedging with Multiple Endpoints

Dynamic Hedging Delays

Conditional Hedging

How Hedging Works

  1. The primary action starts executing
  2. If the action doesn’t complete within Delay (or doesn’t succeed):
    • A hedged action is spawned in parallel
  3. This continues up to MaxHedgedAttempts additional actions
  4. The first successful result is returned
  5. Remaining actions are cancelled
Hedging is useful for:
  • Reducing tail latency
  • Improving reliability when multiple redundant services are available
  • Load balancing across multiple endpoints