Timeouts, Retries, Backoff & Circuit Breakers
Build a Go HTTP client that calls a flaky upstream without amplifying failure — timeout, bounded retries, exponential backoff with jitter, and a circuit breaker.
The patterns that keep one service's bad day from becoming everyone's: timeouts, retries, backoff and circuit breakers. Here we build a small Go client for calling an unreliable upstream and add each pattern a few lines at a time, watching how each one prevents a specific failure — a hung request, a retry storm, a thundering herd, a cascading outage. The goal is a client that fails gracefully instead of making things worse.
What you'll build
- Bound every outbound call with a timeout
- Retry a fixed number of times, only for retryable errors
- Space retries with exponential backoff and jitter
- Stop hammering a dead upstream with a circuit breaker
Contents
- An empty Go module
- A client for the upstream
- Never wait forever
- Try again, but a bounded number of times
- Wait between attempts
- Back off exponentially
- Add jitter
- Use the backoff
- Don't retry what won't change
- A circuit breaker
- Ask before you call
- Trip on sustained failure
- Reset on success
- Put it all together