ExResilience.Hedge (ex_resilience v0.4.0)

Copy Markdown View Source

Tail latency reduction by racing redundant requests.

Starts the given function in a Task, waits for :delay milliseconds, and if no result has arrived, fires up to :max_hedged additional Tasks running the same function. The first successful result wins.

This is useful when a backend has variable latency and you would rather pay the cost of a redundant request than wait for a slow one.

Options

  • :name -- optional atom for telemetry metadata. Default :hedge.
  • :delay -- milliseconds to wait before firing hedge requests. Required.
  • :max_hedged -- max additional requests to fire. Default 1.

Examples

iex> ExResilience.Hedge.call(fn -> {:ok, 42} end, delay: 100)
{:ok, 42}

Summary

Functions

Executes fun with hedge logic for tail latency reduction.

Types

option()

@type option() ::
  {:name, atom()} | {:delay, non_neg_integer()} | {:max_hedged, pos_integer()}

Functions

call(fun, opts)

@spec call((-> term()), [option()]) :: term()

Executes fun with hedge logic for tail latency reduction.

Starts fun as the primary request. If it does not complete within :delay milliseconds, fires up to :max_hedged additional copies. Returns the first successful result, or the first error if all fail.

Examples

iex> ExResilience.Hedge.call(fn -> {:ok, :fast} end, delay: 50)
{:ok, :fast}