Lesson 07 of 13 · Hands-on with Envoy Gateway
Timeouts, retries and traffic management
Make the gateway help instead of hurt when backends are slow or failing. Timeouts and how they nest, retries and the extra load they cause (measured: three retries turned 50% failures into 5%, but sent 4× the requests to a dead backend), circuit breaking and outlier detection, and release patterns (canary, blue/green, mirroring) at the gateway, with real output from the lab cluster.
The gateway sees every failure first
When a backend slows down, the gateway's settings decide whether users wait forever, get a quick error, get a successful retry, or whether the gateway makes it worse by retrying into an overloaded service.
Phoning a busy shop: you wait a while (a timeout), hang up and call again (a retry), but not a hundred times a minute, or you make the line even busier. If the shop never answers, you stop calling for a while and try another branch (circuit breaking and outlier detection).
Use the cluster from lesson 4. Add a test backend that can be slow or fail on request (go-httpbin) and a route for it:
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpbin
namespace: shop
spec:
replicas: 1
selector:
matchLabels: { app: httpbin }
template:
metadata:
labels: { app: httpbin }
spec:
containers:
- name: httpbin
image: mccutchen/go-httpbin:v2.15.0
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: httpbin
namespace: shop
spec:
selector: { app: httpbin }
ports:
- { port: 80, targetPort: 8080 }
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: bin
namespace: shop
spec:
parentRefs:
- { name: edge, namespace: infra }
hostnames: ["bin.example.test"]
rules:
- backendRefs:
- { name: httpbin, port: 80 }
timeouts:
request: 2s # whole request, including retries
/delay/3 answers after 3 seconds; /status/200:0.5,503:0.5 returns 200 or 503 at random.
Timeouts
$ curl -s -o /dev/null -w '%{http_code} %{time_total}s\n' -H 'Host: bin.example.test' localhost:8080/delay/1
200 1.014224s
$ curl -s -w ' | %{http_code} %{time_total}s\n' -H 'Host: bin.example.test' localhost:8080/delay/3
upstream request timeout | 504 2.009184s
Timeouts must nest: each layer gives up a little before the layer in front of it.
client (10 s) > CDN (8 s) > gateway request timeout (5 s) > per-try timeout (1.5 s) > service's own DB timeout (1 s)
If an inner timeout is longer than an outer one, the outer layer gives up while the inner one keeps working (and holding resources) for nobody.
Retries: helpful and dangerous
Without retries, against a backend that fails half the time (100 calls):
50 200
50 503
Add retries:
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
name: bin-retries
namespace: shop
spec:
targetRefs:
- { group: gateway.networking.k8s.io, kind: HTTPRoute, name: bin }
retry:
numRetries: 3
retryOn:
httpStatusCodes: [503]
triggers: [connect-failure, retriable-status-codes]
perRetry:
timeout: 500ms
backOff: { baseInterval: 50ms, maxInterval: 300ms }
# 100 calls, 3 retries on 503
95 200
5 503
backend received: 184 requests for 100 client calls
# Backend completely down (/status/503), 10 calls
503 503 503 503 503 503 503 503 503 503
backend received: 40 requests for 10 client calls
Retries turned 50% errors into 5% (close to 0.5⁴ ≈ 6%), at the cost of 84% more backend traffic. Against a dead backend they achieved nothing and quadrupled the load. Across many gateways and service layers that each retry 3 times, load multiplies at every layer: a retry storm that keeps an overloaded service down.
Rules for retries:
- Retry only idempotent requests (GET, PUT, DELETE by design) or requests with an idempotency key; never blindly retry POSTs that charge money.
- Retry only failures that are likely to be temporary: connection failures, 503, resets. Not 500 (a bug) or 4xx (the client's fault).
- Small
numRetries(1 to 2), backoff with jitter, a per-try timeout and an overall deadline. - Retry in one layer only (usually the gateway or the client library), not at every hop.
- Prefer a retry budget (for example, retries may add at most 20% to traffic) over a fixed count where available.
Note one more real detail: with perRetry.timeout: 500ms in place, the /delay/3 request now fails on the per-try timeout (UT upstream_per_try_timeout in the access log, lesson 12) instead of the 2-second request timeout.
Circuit breaking and outlier detection
- Circuit breaking (Envoy style): caps on connections, pending requests and parallel requests to a backend. Beyond them, the gateway fails fast with 503 instead of queueing; this protects both sides.
- Outlier detection (passive health checking): a pod that returns several 5xx in a row is ejected from load balancing for a while, so one bad pod doesn't fail a share of all requests.
- Active health checks: the gateway probes backends itself; useful for backends outside Kubernetes.
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
name: orders-protection
namespace: shop
spec:
targetRefs:
- { group: gateway.networking.k8s.io, kind: HTTPRoute, name: orders }
circuitBreaker:
maxConnections: 1024
maxPendingRequests: 128
maxParallelRequests: 256
healthCheck:
passive:
consecutive5XxErrors: 5
interval: 10s
baseEjectionTime: 30s
maxEjectionPercent: 50
(Example values; size them from measured traffic.)
Release patterns at the gateway
| Pattern | How | Watch |
|---|---|---|
| Canary | Weights 95/5 → 75/25 → 0/100 (lesson 4) | Error rate and latency of the new version vs the old |
| Blue/green | Two full versions; switch weights 100/0 → 0/100 at once | Instant rollback = switch back |
| Header or cookie opt-in | Testers or internal users send a header (lesson 4) | Real traffic before customers see it |
| Mirroring | RequestMirror filter copies requests to v2; its responses are discarded |
Errors and latency of v2 under real load; beware of duplicate side effects (writes) |
| Fault injection | Add delays or errors on purpose (Envoy Gateway faultInjection) |
That timeouts, retries and fallbacks behave as designed |
Metric-driven canaries (Argo Rollouts or Flagger changing weights automatically) are covered in the multi-tenant SaaS course's canary analysis lesson.
Try it
- Set
numRetries: 1and repeat the 100-call test. Compare client errors and backend requests with 0 and 3 retries. - Add a
RequestMirrorfilter to theordersroute that copies traffic toorders-v2, and confirm in v2's logs that it receives requests while clients only see v1. - Add fault injection that delays 10% of requests by 2 seconds, and watch the p99 latency change.
- Write the timeout chain for one of your own APIs, from browser to database, and check that each timeout is shorter than the one before.
Going deeper
- Hedged requests send a second attempt before the first fails (after the p95 latency), trading load for tail latency; use sparingly.
- Load shedding: when a service is overloaded, rejecting low-priority requests quickly keeps high-priority ones fast.
- Meshes offer the same controls between services; set them in one place per call path.
Recap
- Timeouts nest: inner layers give up before outer ones.
- Retries hide transient failures but multiply load; retry only idempotent, transient failures, few times, with backoff and in one layer.
- Circuit breakers fail fast when a backend is saturated; outlier detection removes bad pods.
- The gateway runs canaries, blue/green switches, mirroring and fault injection without changing application code.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.