Timeouts, retries, circuit breakers
Keep failures from cascading using Envoy primitives.
Three primitives that catch most failures
Real backends fail. Slow networks happen. Without protection, one struggling upstream can drag the entire fleet down. Envoy gives you three knobs:
timeout— how long Envoy will wait for the upstream before giving up on the request.retry_policy— what to retry on, how many times, and with what spacing.circuit_breakers— caps on concurrency that shed load before things melt.
Plus outlier detection (passive health checks) which we'll mention but not configure here.
Used together, these stop a flaky upstream from turning into an outage.
How they fit on a request timeline
If the total exceeds timeout, Envoy gives up with 504. If
max_retries is hit, URX lands in your access log.
Timeouts and retries (per-route)
routes:
- match: { prefix: "/" }
route:
cluster: api
timeout: 3s
retry_policy:
retry_on: "5xx,connect-failure,refused-stream,reset"
num_retries: 3
per_try_timeout: 1s
What this says, in English: "give the upstream up to 3 seconds total. If it returns a 5xx or fails to connect, retry up to 3 times, each attempt capped at 1 second."
retry_on is a comma-separated list. The handy values:
5xx— any 500-class responsegateway-error— 502/503/504 specificallyconnect-failure— TCP couldn't connectrefused-stream— HTTP/2 stream refusedreset— connection resetretriable-status-codes— combine withretriable_status_codes: [...]
Retrying a POST /charge can charge a customer twice. Use
x-envoy-retry-on headers (set by the application) or limit
retries to safe routes. The defaults are conservative for a reason.
Circuit breakers (per-cluster)
Circuit breakers cap concurrency. If you exceed a limit, Envoy fails fast
with 503 UpstreamOverflow instead of piling more work on a struggling
backend.
clusters:
- name: api
circuit_breakers:
thresholds:
- priority: DEFAULT
max_connections: 100
max_pending_requests: 50
max_requests: 200
max_retries: 3
Tune these to your backend's actual capacity. The right values come from load tests, not guesses.
Hands-on: trigger every primitive
httpbin.org is the perfect chaos monkey for this lesson — its
/status/{code} endpoint returns whatever HTTP status you ask for, and
/delay/{n} waits N seconds.
Use the hello-world Envoy that proxies to httpbin.org from Lesson 5. Add the timeout + retry_policy + circuit_breakers blocks above, then:
1. See retries. In one terminal, tail Envoy's stdout. In another:
curl -i localhost:10000/status/503
httpbin returns 503; Envoy retries 3 times before giving up. Look at
http://localhost:9901/stats?filter=upstream_rq_retry — that counter
should jump.
2. See timeouts.
curl -i localhost:10000/delay/5
Envoy gives up after 3s with a 504 (gateway timeout). The
upstream_rq_timeout stat will increment.
3. See circuit breakers. Lower max_connections to 2
in your config and restart. In one terminal, run a slow request:
for i in 1 2 3 4 5; do curl -s localhost:10000/delay/10 & done
The first couple succeed; the rest get fast 503s with reason
"upstream_overflow". Your upstream_cx_overflow counter
proves it.
Envoy attaches an x-envoy-response-flags-style note to its
access log: UH (no healthy upstream), UO (overflow),
UT (timeout), URX (retry limit exceeded). When
something fails, those two-letter codes tell you exactly which primitive
fired.
You have most of what you need now
Listeners, clusters, routes, load balancing, TLS, and resilience. With just these, you can build a perfectly serviceable edge proxy. The next lesson is about deploying new versions without giving anyone a heart attack.
An upstream is failing slowly. Which Envoy feature SHEDS load by capping concurrent connections to it?