envoy-guide
Module 3 · Making It Useful·14 min·intermediate

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:

  1. timeout — how long Envoy will wait for the upstream before giving up on the request.
  2. retry_policy — what to retry on, how many times, and with what spacing.
  3. 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)

YAML
  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 response
  • gateway-error — 502/503/504 specifically
  • connect-failure — TCP couldn't connect
  • refused-stream — HTTP/2 stream refused
  • reset — connection reset
  • retriable-status-codes — combine with retriable_status_codes: [...]
Don't retry non-idempotent writes blindly

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.

YAML
  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.

Hands-on · Watch retries fire and circuit breakers shed

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:

Shell
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.

Shell
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:

Shell
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.

Read the response flags

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.

Real-world example · A fintech with a slow third-party KYC vendor
Problem
Their identity-verification provider sometimes takes 8 seconds to respond. When it does, request threads in their app pile up, the app pool exhausts, and unrelated checkout requests start to fail. One vendor, whole site down.
Envoy solution
Envoy sits in front of the KYC vendor with a timeout: 3s, a retry_policy capped at 1 retry, and tight circuit_breakers. Slow vendor responses now fail fast at Envoy. The application code falls back to a "we're verifying — please wait" path instead of locking up. The vendor goes down; checkout doesn't.

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.

Quick check

An upstream is failing slowly. Which Envoy feature SHEDS load by capping concurrent connections to it?

Practical task
Finish the quick check and tick off the task to unlock.
Quick checkTasks 0/3