You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit cb6f906
Browse filesBrowse the repository at this point in the historyBrowse files
feat(circuit_breaker): support composite primary key in DynamoDB persistence (#8316)
* feat(circuit_breaker): support composite primary key in DynamoDB persistence
Add optional `sort_key_attr` and `static_pk_value` parameters to
CircuitBreakerDynamoDBPersistence so circuit state can be stored in a
single-table (composite key) design, mirroring the Idempotency utility.
When `sort_key_attr` is set, the circuit name is written to the sort key
and `static_pk_value` to the partition key (default
`circuit_breaker#<function-name>`); omit it for the current
partition-key-only behavior. The composite key is threaded through the
get, conditional put (half-open probe election) and update paths.
Includes Stubber-based tests for both modes, docs, and an example.
Closes#8315
* fix(circuit_breaker): rename the composite-key example to a unique basename so mypy no longer clashes with the idempotency example.
* feat(circuit_breaker): warn when static_pk_value is passed without sort_key_attr since it is otherwise silently ignored.
---------
Co-authored-by: Leandro Damascena <lcdama@amazon.pt>
Copy file name to clipboardExpand all lines: docs/utilities/circuit_breaker.md
+23-1Lines changed: 23 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -91,7 +91,7 @@ Unless you're looking to [customize each attribute](#customizing-the-dynamodb-ta
91
91
| Partition key |`id`| Holds the circuit name |
92
92
| TTL attribute name |`expiration`| Using AWS Console? This is configurable after table creation |
93
93
94
-
You **can** use a single DynamoDB table for all your circuits.
94
+
You **can** use a single DynamoDB table for all your circuits. Already have a single-table (composite key) design you want to reuse? See [Using a composite primary key](#using-a-composite-primary-key).
95
95
96
96
##### DynamoDB IaC example
97
97
@@ -218,6 +218,28 @@ Set **`POWERTOOLS_CIRCUIT_BREAKER_DISABLED`**{: .copyMe} to a truthy value to by
218
218
219
219
`CircuitBreakerDynamoDBPersistence` accepts attribute-name overrides (`key_attr`, `state_attr`, `failure_count_attr`, `opened_at_attr`, `half_open_owner_attr`, `expiry_attr`) and the usual boto3 escape hatches (`boto3_session`, `boto3_client`, `boto_config`) for reusing an existing table layout or client.
220
220
221
+
#### Using a composite primary key
222
+
223
+
Use the `sort_key_attr` parameter when your table is configured with a composite primary key _(hash+range key)_, as in a single-table design.
224
+
225
+
When enabled, the circuit name is saved in the sort key instead, and the partition key defaults to `circuit_breaker#{LAMBDA_FUNCTION_NAME}` — this **namespaces circuits per function** so two functions can use the same circuit name without sharing state. (Without `sort_key_attr`, the default remains partition-key-only with the circuit name as the partition key.)
226
+
227
+
Set `static_pk_value` to a fixed value (as below) when you instead want **multiple functions to share the same circuit** — for example, every function guarding the same downstream dependency should trip together.
When unit testing the function a circuit protects, set `POWERTOOLS_CIRCUIT_BREAKER_DISABLED=true` to bypass the circuit and persistence layer entirely, so your tests exercise the business logic without needing DynamoDB.
0 commit comments