Skip to content

feat: adding circuit breaker feature#8266

Merged
leandrodamascena merged 11 commits into
developfrom
feat/circuit-breaker
Jun 25, 2026
Merged

feat: adding circuit breaker feature#8266
leandrodamascena merged 11 commits into
developfrom
feat/circuit-breaker

Conversation

@leandrodamascena

Copy link
Copy Markdown
Contributor

Issue number: closes #8257

Summary

Changes

This PR adds a Circuit Breaker utility (under the circuit_breaker_alpha namespace) so a Lambda function can stop calling an unhealthy downstream and let it recover, instead of piling on retries.

It ships as alpha on purpose: I want about a month of feedback before we lock the public API and promote it to GA.

The failure counter lives in memory per execution environment, so a healthy circuit writes nothing; we only persist state transitions. State is shared via DynamoDB, fails open if the store is unreachable, and uses a conditional write to elect a single probe during recovery (no thundering herd). You handle rejected requests with an on_circuit_open callback and observe state changes with an on_transition hook.

User experience

Before, you had to build the state machine, shared storage, and recovery logic yourself. Now you wrap the function that makes the downstream call:

from aws_lambda_powertools.utilities.circuit_breaker_alpha import circuit_breaker
from aws_lambda_powertools.utilities.circuit_breaker_alpha.persistence import (
    CircuitBreakerDynamoDBPersistence,
)

persistence = CircuitBreakerDynamoDBPersistence(table_name="CircuitBreakerState")


@circuit_breaker(name="payment-backend", persistence_store=persistence)
def charge(order: dict) -> dict:
    return payment_api.charge(order)

With no config, sensible defaults apply (open after 5 failures, probe after 30s, close after 3 successes). When open, the call is skipped and the callback's value is returned, or a CircuitBreakerOpenError is raised.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@leandrodamascena leandrodamascena requested a review from a team as a code owner June 9, 2026 21:15
@leandrodamascena leandrodamascena requested a review from hjgraca June 9, 2026 21:15
@boring-cyborg boring-cyborg Bot added commons documentation Improvements or additions to documentation tests labels Jun 9, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Jun 9, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 9, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 10, 2026
@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.19588% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.62%. Comparing base (01e1e88) to head (b4af596).
⚠️ Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
...ties/circuit_breaker_alpha/persistence/dynamodb.py 93.68% 3 Missing and 3 partials ⚠️
...powertools/utilities/circuit_breaker_alpha/base.py 99.03% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8266      +/-   ##
===========================================
+ Coverage    96.58%   96.62%   +0.04%     
===========================================
  Files          286      296      +10     
  Lines        14298    14686     +388     
  Branches      1193     1230      +37     
===========================================
+ Hits         13810    14191     +381     
- Misses         357      360       +3     
- Partials       131      135       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 18, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 18, 2026
@leandrodamascena leandrodamascena requested a review from svozza June 18, 2026 10:46
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 18, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 18, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 18, 2026
Comment thread aws_lambda_powertools/utilities/circuit_breaker_alpha/persistence/dynamodb.py Outdated
Comment thread docs/utilities/circuit_breaker.md Outdated
Comment thread docs/utilities/circuit_breaker.md
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 22, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 22, 2026
@mergify

mergify Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 25, 2026
@leandrodamascena leandrodamascena merged commit e7e6745 into develop Jun 25, 2026
19 checks passed
@leandrodamascena leandrodamascena deleted the feat/circuit-breaker branch June 25, 2026 15:53
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commons documentation Improvements or additions to documentation size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RFC: Circuit Breaker with Fallback

2 participants