Skip to main content

# Hedging policies

## A short history​

A hedging policy is a risk management strategy used by organizations to mitigate the impact of adverse events or changes in market conditions. The goal of a hedging policy is to protect the organization from financial losses by using financial instruments to offset or minimize the risk associated with a particular exposure. Hedging policies are commonly used in financial markets to reduce the impact of market volatility. For example, a company may use a hedging policy to protect against fluctuations in currency exchange rates or commodity prices. By using financial instruments such as futures contracts or options, the company can effectively lock in a price for a future transaction, reducing the impact of price changes on their financial position.

In addition to financial markets, hedging policies may also be used in other industries to manage risk. For example, an energy company may use a hedging policy to protect against fluctuations in energy prices, while a manufacturing company may use a hedging policy to protect against changes in the cost of raw materials.

## Hedging in software​

Hedging is an alternative retry policy. With hedging, multiple copies of a single gRPC call are sent aggressively without waiting for a response. These hedged gRPC calls can be executed several times on the server, and the first successful result is used. However, it is crucial to enable hedging only for methods that can be executed multiple times safely without any harmful consequences.

//Let's discuss, what would be good scenarios?

## Pros​

An advantage to hedging is it might return a successful result faster. It allows for multiple simultaneously gRPC calls and will complete when the first successful result is available.

## Cons​

A disadvantage to hedging is it can be wasteful. Multiple calls could be made and all succeed. Only the first result is used and the rest are discarded.

# Configuring a hedging policy

The configuration is done in a similar manner to the retry policies. You can choose one, or the other.

# gRPC hedging options

- `MaxAttempts` : The hedging policy will send up to this number of calls. MaxAttempts represents the total number of all attempts, including the original attempt. This value is limited by GrpcChannelOptions.MaxRetryAttempts which defaults to 5. A value is required and must be 2 or greater.
- `HedgingDelay` : The first call is sent immediately, subsequent hedging calls are delayed by this value. When the delay is set to zero or null, all hedged calls are sent immediately. HedgingDelay is optional and defaults to zero. A value must be zero or greater.
- `NonFatalStatusCodes` - collection of status codes which indicate other hedge calls may still succeed. If a non-fatal status code is returned by the server, hedged calls will continue. Otherwise, outstanding requests will be canceled and the error returned to the app.

Let's try it
Hedging policy channel

          private static GrpcChannel CreateChannel()
            {
                var methodConfig = new MethodConfig
                {
                    Names = { MethodName.Default },
                    HedgingPolicy = new HedgingPolicy
                    {
                        MaxAttempts = 5,
                        NonFatalStatusCodes =
                        {
                         StatusCode.Unavailable
                        }
                    }
                };

                return GrpcChannel.ForAddress("https://localhost:7226", new GrpcChannelOptions
                {
                    ServiceConfig = new ServiceConfig { MethodConfigs = { methodConfig } }
                });
            }

Title

     using var channel = CreateChannel();
     var client = new Retrier.RetrierClient(channel);
     await UnaryRetry(client);