Azure 65: Retries, Timeouts & Circuit Breakers
easy⏱ 5 mincourseazure
The three guards
Timeout: never wait forever — bound every network call. Retry: re-attempt transient failures, but with exponential backoff + jitter (1s, 2s, 4s ± random) so you don't synchronize a thundering herd. Circuit breaker: after too many failures, open the circuit and fail fast for a cool-down — sparing the dying dependency and your own threads.
// backoff with jitter
// delay = base * 2^attempt + random(0..base)
Breaker states
Closed (normal, counting failures) → past the threshold → Open (fail fast, no calls) → after a cool-down → Half-open (let one trial through): success closes it, failure re-opens. The half-open state is what lets the system self-heal without a deploy.
Build the breaker
Write a CircuitBreaker with a failure threshold of 3. call(fn) runs fn; on 3 consecutive failures it opens and further calls log circuit open — fail fast without running fn. Simulate 3 failing calls then a 4th, logging the transitions including circuit OPEN.
Retries need a budget too
Unbounded retries amplify an outage — each client retrying 3x triples load on the struggling service. Cap total attempts, add jitter, and let the circuit breaker cut retries off entirely once it opens.
Quiz: what does the half-open state do?
Half-open lets a single trial request through after the cool-down; success closes the circuit, failure re-opens it — enabling self-healing.