liveBook · Manning

Circuit breaker pattern

As explained in Python Concurrency with asyncio

A circuit breaker pattern is a fault-tolerance pattern that stops sending requests to a failing or slow service and resumes requests after a configured reset condition.

A circuit breaker prevents repeated calls to a failing or slow service from delaying users. After a configured number of errors within a time window, it opens, skips the service, and fails fast until the reset interval expires. section 10.3.5

Model the breaker with these states and transitions:

  • Closed: send requests normally and record failures. section 10.3.5
  • Open: stop calling the unhealthy service and return an error immediately. section 10.3.5
  • After the reset interval, test the service; a successful test closes the breaker, while a failed test keeps it open. A simplified implementation may omit this half-open state. section 10.3.5

Protecting an inventory request

Wrap an asynchronous inventory callback with a breaker configured for a 0.5-second timeout, a five-second failure window, three allowed failures, and a 30-second reset interval. Repeated slow calls then fail fast instead of blocking the backend-for-frontend response. section 10.3.5 section 10.3.5

Figure 10.3 A circuit breaker that opens after two failures. Once opened, all requests will fail instantly.
10-03

Trace the breaker opening after two failures and observe subsequent requests failing instantly. section 10.3.5

How Python Concurrency with asyncio covers it

How deep it goes
Explains closed, open, and half-open states, then implements a simplified asynchronous breaker with configurable thresholds and timing.
Examples
Demonstrates slow cart calls, an inventory callback, repeated breaker requests, and a reset after five seconds.
What it assumes
Assumes asynchronous service calls and introduces the resilience problem through microservice aggregation.
What you can do after
Build it from scratch with asyncio, configure failure and reset thresholds, and apply it to asynchronous service requests.
How it connects
Connects microservices, backend-for-frontend aggregation, retries, concurrency, and downstream failure handling.

Also explained in

Compare how 6 books explain circuit breaker pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.