Skip to main content

Circuit Breaker Pattern in Python

Overview

The Circuit Breaker pattern is a design pattern used to improve system resilience by preventing continuous failures in a system. It acts as a safeguard by monitoring requests and stopping repeated failures from affecting performance.

How It Works

  1. Closed State: The system operates normally and allows requests.
  2. Open State: If failures exceed a threshold, the circuit "opens," stopping further requests for a cooldown period.
  3. Half-Open State: After a cooldown, a few test requests are allowed. If they succeed, the circuit closes; otherwise, it stays open.

Why Use Circuit Breaker?

  • Prevents cascading failures in distributed systems.
  • Improves system reliability and recovery.
  • Reduces unnecessary load on failing services.

Example Usage in Python

import uvicorn
from fastapi import FastAPI
from fastapi.exceptions import HTTPException
from circuit_breaker import CircuitBreakerMiddleware, CircuitBreakerInputDto

app = FastAPI()


@app.get("/success-endpoint")
async def success_endpoint():
    return "success"


@app.get("/failure-endpoint")
async def faulty_endpoint():
    raise HTTPException(status_code=500)


app.add_middleware(
    CircuitBreakerMiddleware,
    circuit_breaker_input=CircuitBreakerInputDto(
        exception_list=(Exception,),
        half_open_retry_count=3,
        half_open_retry_timeout_seconds=30,
    ),
)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

Release files for circuit_breaker_fastapi 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for circuit_breaker_fastapi 0.1.0
File Size Uploaded
circuit_breaker_fastapi-0.1.0.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for circuit_breaker_fastapi 0.1.0
File Interpreter ABI Platform
circuit_breaker_fastapi-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size:8.7 kB

Release files / circuit_breaker_fastapi-0.1.0.tar.gz

Download URL circuit_breaker_fastapi-0.1.0.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
020e3860107f1f685e90c68a4f13be06b421d48c50f42a94a70855613b7c3b51
BLAKE2b-256 checksum
How to use checksums
6d7b1d624c4c05eca33e94a316e28e540b82269f007a7ae9c51cbae94fa7e9e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.5

Release files / circuit_breaker_fastapi-0.1.0-py3-none-any.whl

Download URL circuit_breaker_fastapi-0.1.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a449f7bae82b8f8ef72d7e3be5cc09c7c13a31757157881ff9e75a4fa51cbd91
BLAKE2b-256 checksum
How to use checksums
52ceeceabebdf5efdc02c9384685d26bc74865937c79311863171d7ce51663e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page