Skip to main content

Circuit breaker pattern implementation fastapi

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

circuit_breaker_fastapi-0.1.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

circuit_breaker_fastapi-0.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file circuit_breaker_fastapi-0.1.0.tar.gz.

File metadata

File hashes

Hashes for circuit_breaker_fastapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 020e3860107f1f685e90c68a4f13be06b421d48c50f42a94a70855613b7c3b51
MD5 2dea748b4d0def2ab5ddf03b8d93e712
BLAKE2b-256 6d7b1d624c4c05eca33e94a316e28e540b82269f007a7ae9c51cbae94fa7e9e6

See more details on using hashes here.

File details

Details for the file circuit_breaker_fastapi-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for circuit_breaker_fastapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a449f7bae82b8f8ef72d7e3be5cc09c7c13a31757157881ff9e75a4fa51cbd91
MD5 8b40a5feac82cc705ace102324b66e75
BLAKE2b-256 52ceeceabebdf5efdc02c9384685d26bc74865937c79311863171d7ce51663e7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page