Circuit Breakers interview problems

A circuit breaker question asks you to stop calling a dependency that keeps failing and to probe it carefully before trusting it again. The breaker has three states. Closed passes calls through and counts failures. Open fails fast with an exception like CircuitOpenError until a cool-down passes. Half-open lets a limited number of trial calls through and closes on success or reopens on failure. In Python this is a small class with an Enum for state, time.monotonic() timestamps and an async call(fn, *args) wrapper.

Interviewers look for an explicit state machine instead of scattered booleans, and for a failure count that resets on success. Another check is whether the half-open state limits concurrent probes. Without that, a hundred coroutines all slip through the moment the cool-down ends. Other mistakes are counting every exception as a failure, including CancelledError or a client-side ValueError, and checking state before an await but updating it after without accounting for interleaving. A strong answer injects the clock, decides which exceptions count, and can extend to a rolling failure-rate window.

1 open problem

  1. 16Async Circuit Breakerhard

    Implement an asyncio circuit breaker with closed, open and half-open states, a single recovery probe, and protection against stale in-flight calls.