A production-grade, asynchronous HTTP client for Python engineered to tolerate downstream service outages, network instability, and latency spikes.
Project description
๐ก๏ธ Resilient HTTP Client
A production-grade, asynchronous HTTP client for Python engineered to tolerate downstream service outages, network instability, and latency spikes. It implements proven resilience patterns including Circuit Breakers, Retry Policies, Fallback Mechanisms, and a Distributed Failure Store to enable reliable service-to-service communication.
Built on top of httpx, the library is designed for modern distributed systems where resilience is a first-class requirement.
๐ Features
- Circuit Breaker Pattern โ Prevents cascading failures using a strict state machine (
CLOSED,OPEN,HALF-OPEN). - Distributed Failure Store โ Share circuit state and failure metrics across workers and service instances.
- Automatic Retries โ Configurable retry budgets with exponential backoff for transient failures.
- Graceful Fallbacks โ Return degraded responses or execute alternative logic instead of surfacing raw exceptions.
- Fully Asynchronous โ Built on
httpxfor high-concurrency, non-blocking I/O. - Pluggable Components โ Storage and resilience behavior can be customized to fit different deployment environments.
- Production Ready โ Suitable for microservices, containerized workloads, and distributed deployments.
๐ System Architecture
The following diagram illustrates how the components of resilient-http-client coordinate request delivery, state checking, retries, and fallback execution.
flowchart TD
classDef main fill:#1E88E5,stroke:#1565C0,stroke-width:2px,color:#fff;
classDef support fill:#43A047,stroke:#2E7D32,stroke-width:2px,color:#fff;
classDef store fill:#E53935,stroke:#C62828,stroke-width:2px,color:#fff;
Client["ResilientHttpClient"]:::main
CB["CircuitBreaker"]:::main
Store["FailureStore"]:::store
Retry["RetryPolicy"]:::support
Fallback["FallbackHandler"]:::support
Executor["HttpExecutor (httpx)"]:::support
Client -->|1. Check state / record metrics| CB
Client -->|2. Manage retry attempts| Retry
Client -->|3. Fallback on final error| Fallback
Client -->|4. Dispatch requests| Executor
CB -->|Query & persist state / counters| Store
๐ Circuit Breaker State Machine
The client implements a fully compliant circuit breaker state machine with lazy cooldown transitions and probe request gating.
stateDiagram-v2
[*] --> Closed : Initial State
Closed --> Open : failures >= threshold
Closed --> Closed : success
Open --> HalfOpen : cooldown expired
Open --> Open : block requests
HalfOpen --> Closed : success >= successes_needed
HalfOpen --> Open : any failure
State Behavior
CLOSED
Requests flow normally.
- Successful requests reset failure counters.
- Consecutive failures are tracked.
- Reaching the configured threshold transitions the circuit to OPEN.
OPEN
Requests fail immediately without contacting the downstream service.
- Prevents latency amplification and resource exhaustion.
- Remains open for the configured cooldown period.
- Automatically transitions to HALF-OPEN after cooldown expires.
HALF-OPEN
Allows a limited number of probe requests.
- Successful probes close the circuit.
- Any failed probe immediately reopens the circuit.
- Prevents unstable services from causing repeated outages.
โก Quick Start
import asyncio
import redis.asyncio as redis
from resilient_http_client import (
FailureStore,
ResilientHttpClient,
)
async def main():
# Example using Redis-backed storage
redis_client = redis.Redis(
host="localhost",
port=6379,
decode_responses=True,
)
store = FailureStore(
redis=redis_client,
service="stripe_payment",
)
async with ResilientHttpClient(
service="stripe_payment",
store=store,
) as client:
response = await client.request(
method="POST",
url="https://api.stripe.com/v1/charges",
json={
"amount": 2000,
"currency": "usd",
},
)
print(response)
if __name__ == "__main__":
asyncio.run(main())
โ๏ธ Configuration
Customize resilience behavior through ResilienceConfig.
from resilient_http_client import ResilienceConfig
config = ResilienceConfig(
failure_threshold=5,
cooldown=30,
max_retries=3,
timeout=5.0,
half_open_max_calls=1,
half_open_successes_needed=1,
)
client = ResilientHttpClient(
service="my-api",
store=store,
config=config,
)
Configuration Reference
| Parameter | Type | Default | Description |
|---|---|---|---|
failure_threshold |
int |
5 |
Consecutive failures required to open the circuit |
cooldown |
int |
30 |
Seconds before an open circuit transitions to half-open |
max_retries |
int |
3 |
Number of retry attempts before failure |
timeout |
float |
5.0 |
Request timeout in seconds |
half_open_max_calls |
int |
1 |
Maximum probe requests allowed while half-open |
half_open_successes_needed |
int |
1 |
Successful probes required to close the circuit |
๐งฉ Components
Circuit Breaker
Prevents repeated requests to unhealthy downstream services.
States:
- Closed โ Requests flow normally.
- Open โ Requests fail immediately.
- Half-Open โ Limited recovery probes are allowed.
Retry Policy
Automatically retries transient failures using configurable exponential backoff.
Typical retry conditions include:
- HTTP 5xx responses
- Connection failures
- Network timeouts
Fallback Handler
Fallbacks are executed when:
- The circuit is open.
- Retry attempts are exhausted.
- A non-retryable failure occurs.
Failure Store
Persists resilience state used by the circuit breaker.
Responsibilities include:
- Failure counters
- Circuit state
- Cooldown timestamps
- Cross-worker coordination
The library includes a Redis-backed implementation and can be extended with custom storage backends.
๐ ๏ธ Project Structure
src/
โโโ resilient_http_client/
โโโ __init__.py
โโโ client.py
โโโ circuit_breaker.py
โโโ config.py
โโโ failure_store.py
โโโ fallback.py
โโโ http.py
โโโ retry.py
โโโ types.py
tests/
โโโ test_circuit_breaker.py
โโโ test_failure_store.py
โโโ test_fallback.py
โโโ test_flaky_resilience.py
โโโ test_integration.py
โโโ test_retry_policy.py
Module Overview
- ๐ฐ๏ธ
client.pyโ Main orchestration layer. - ๐
circuit_breaker.pyโ Circuit breaker state machine. - ๐๏ธ
failure_store.pyโ Distributed state management. - ๐
retry.pyโ Retry policy implementation. - ๐ญ
fallback.pyโ Fallback registration and execution. - โ๏ธ
config.pyโ Configuration definitions. - ๐
types.pyโ Shared enums and type definitions. - ๐
http.pyโ HTTP request execution layer.
๐งช Running the Tests
Run the full test suite:
uv run pytest
Coverage Includes
- Failure tracking
- Circuit opening thresholds
- Cooldown transitions
- Half-open probe gating
- Recovery behavior
- Retry policies
- Distributed state persistence
- Fallback execution paths
๐ Examples
The examples/ directory contains complete demonstrations and integrations.
How to run examples
-
Start Redis:
docker compose up -d
-
Run the example:
PYTHONPATH=. uv run python examples/slack_example.py
Available examples:
fastapi_integration.pysimulate_outage.pyslack_example.pystripe_example.py
๐ฏ Use Cases
- Service-to-service communication
- Third-party API integrations
- Payment gateways
- Authentication providers
- Event-driven systems
- Containerized applications
- Kubernetes deployments
- Any environment where downstream dependencies may become unavailable
License
MIT
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ad_tech_inc_resilient_http-0.1.0.tar.gz.
File metadata
- Download URL: ad_tech_inc_resilient_http-0.1.0.tar.gz
- Upload date:
- Size: 368.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8693e0e7bf5a195b94aac7dd2a764556234ef515698f61c50a43d6fa328d39da
|
|
| MD5 |
a667b4fbc82c91d1801dff3f1177efda
|
|
| BLAKE2b-256 |
fc26be4078260fa0e5c6466e6fc6d8b4d707f7169f0e7f35dda6ae1beec6c648
|
Provenance
The following attestation bundles were made for ad_tech_inc_resilient_http-0.1.0.tar.gz:
Publisher:
publish.yml on AD-Technology-Inc/resilient-http-client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ad_tech_inc_resilient_http-0.1.0.tar.gz -
Subject digest:
8693e0e7bf5a195b94aac7dd2a764556234ef515698f61c50a43d6fa328d39da - Sigstore transparency entry: 2124415788
- Sigstore integration time:
-
Permalink:
AD-Technology-Inc/resilient-http-client@385c886fb9358207a88782c104cdfa00a8117acf -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/AD-Technology-Inc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@385c886fb9358207a88782c104cdfa00a8117acf -
Trigger Event:
release
-
Statement type:
File details
Details for the file ad_tech_inc_resilient_http-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ad_tech_inc_resilient_http-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ec73ff174946d332c4c7293314b366ca1e887f8d9fae40d8b2cc94102e26f86
|
|
| MD5 |
8118832eac619db49944d4f71ad2e297
|
|
| BLAKE2b-256 |
f6e23f3272c55240e8794270c9e4d5736118dbebe9a4b4a3944c2d51faaa802b
|
Provenance
The following attestation bundles were made for ad_tech_inc_resilient_http-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on AD-Technology-Inc/resilient-http-client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ad_tech_inc_resilient_http-0.1.0-py3-none-any.whl -
Subject digest:
1ec73ff174946d332c4c7293314b366ca1e887f8d9fae40d8b2cc94102e26f86 - Sigstore transparency entry: 2124415820
- Sigstore integration time:
-
Permalink:
AD-Technology-Inc/resilient-http-client@385c886fb9358207a88782c104cdfa00a8117acf -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/AD-Technology-Inc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@385c886fb9358207a88782c104cdfa00a8117acf -
Trigger Event:
release
-
Statement type: