HARP logo

Circuit breaker

In HARP, tripping a circuit ejects one URL from the pool; clients only see failures when no URL is left.

The pattern

In distributed systems, calls to remote services fail for many reasons: slow networks, timeouts, temporary unavailability. Short-lived faults can be smoothed over with the retry pattern, which retries after a brief pause. But some failures, like partial connectivity loss or a full outage, take longer to resolve, and retrying them only wastes resources. The system needs to recognize those quickly and stop trying.

This matters under load, where one failing component can cascade: callers block waiting for timeouts, tying up memory and threads until they are exhausted and other parts of the system break too. The fix is to fail fast when success is unlikely, rather than wait for every call to time out.

The circuit breaker pattern detects failures and stops repeated, unsuccessful attempts. When failures are detected, the circuit trips and the failing service is left alone. This isolates faults, lets it recover, and prevents cascading failures across the system.

A circuit breaker acts as a proxy for operations that may fail. It tracks recent failures and uses that signal to decide whether to forward a call or reject it right away.

The circuit breaker can be seen as a state machine that mediates remote calls:

Half-OpencheckingClosedOpenevery URLstarts herefirst proxied2xx or 3xx,or a probesuccessfailures reachthe thresholdfailures reachthe thresholdprobe successmarked down inthe dashboard,after check_after
  • Closed: Forwards requests to the URL, in round-robin with the other URLs of the active pool. When failures reach the configured threshold, the URL moves to the Open state.
  • Open: Stops routing requests to the failing URL, reducing pressure on the struggling service. A URL taken out comes back after a probe succeeds, which closes the circuit directly (the breaker never moves it to Half-Open); without a probe, when you bring it back from the dashboard or HARP restarts. A URL you mark down from the dashboard is in this same state, but returns to Half-Open after check_after seconds (10 by default), unless a probe closes it first.
  • Half-Open: The URL is in the pool, on trial. Every URL starts in this state. The first proxied 2xx or 3xx response, or a probe success, closes the circuit; a failure that reaches the threshold opens it again.

How HARP implements it

The circuit breaker operates at the level of a remote's URL pool. Requests go round-robin through the active pool, and each URL has its own circuit. Tripping one takes only that URL out of the rotation, so the others keep serving.

You set min_pool_size (1 by default) to define how many active URLs must remain before HARP falls back. When the active pool drops below that size, HARP adds any configured fallback URLs to it. If no viable URL is left, it returns an HTTP 503 to the client.

With a liveness algorithm set, a tripped URL comes back after a probe succeeds; without a probe, when you bring it back from the dashboard or HARP restarts. A probe periodically checks URL health: HARP probes every URL, including those currently outside the active pool. The probe is the one described in Health monitoring.

Enable a probe whenever you enable the circuit breaker. With naive, failure_threshold = 1 (the default) and no probe, one network error takes the URL out until you bring it back from the dashboard or HARP restarts. A remote with a single URL and no fallback answers 503 meanwhile.

HARP does not retry a failed request on another URL of the pool: the next request goes to the next URL, round-robin.

With naive, only a probe success clears the failure count, and a URL the probe brings back starts from zero. A URL brought back from the dashboard, or by check_after, or closed by a proxied 2xx or 3xx keeps its count: after a trip, a single failure takes it out again. Without a probe nothing clears it, so naive adds up failures over the URL's whole life.

With leaky, each failure adds 1 to a bucket that drains at rate per second. The URL opens when the level reaches threshold. The level is never reset: it only drains with time. A probe success closes it again once the level is under threshold.

Configuration

The circuit breaker is disabled by default, and is enabled by choosing a liveness algorithm: naive or leaky.

toml
[[proxy.endpoints]]
name = "api"
port = 4000

[proxy.endpoints.remote]
# Active URLs to keep in the pool before the fallback URLs join it (1 by default).
min_pool_size = 2
# What trips a URL: network_error, unhandled_exception, http_5xx and http_4xx.
# The default is network_error and unhandled_exception, so a 5xx trips nothing until
# you add http_5xx. Leave http_4xx out: it is a client error, not the upstream failing.
break_on = ["network_error", "unhandled_exception", "http_5xx"]
# Seconds before a URL marked down from the dashboard goes back to Half-Open (10 by default).
check_after = 10.0

[[proxy.endpoints.remote.endpoints]]
url = "https://api1.example.com/"

[[proxy.endpoints.remote.endpoints]]
url = "https://api2.example.com/"

[[proxy.endpoints.remote.endpoints]]
url = "https://fallback.example.com/"
pools = ["fallback"]

# The circuit breaker is off until you choose an algorithm here.
[proxy.endpoints.remote.liveness]
# naive: the URL opens after failure_threshold failures, and closes again after
# success_threshold probe successes (both 1 by default). Only probe successes count
# toward it: a proxied 2xx or 3xx in Half-Open closes the circuit at the first one.
type = "naive"
failure_threshold = 1
success_threshold = 1

# leaky, a leaky bucket, instead of naive: each failure adds 1 to the bucket.
# type = "leaky"
# rate = 1.0       # the bucket drains by 1 per second (1.0 by default)
# capacity = 60.0  # the bucket never holds more (60.0 by default)
# threshold = 10.0 # the URL opens when the level reaches this (10.0 by default);
#                  # a probe success closes it once the level is under this