Retry strategies and rate control
Retry strategies decide whether a failed service request can run again and how long the client waits. This page explains how botocore retry rules work in async execution, how standard and special checks select retryable failures, and how adaptive rate control limits repeated requests. See Retry and resilience for the wider resilience model.
Retry handler adaptation
The retry handler connects service results to the client request loop. It turns retry configuration into checks and delay actions, then evaluates those actions through async callbacks without blocking the event loop.
Retry event contract
The handler receives the attempt number, the service response, and any exception from the request flow. It runs the configured checks first. When a check succeeds, it returns an action result that gives the delay for the next attempt. When no check succeeds, it returns no retry decision.
The handler can combine default rules with operation-specific rules. It also enforces the maximum attempt count. When that limit is reached, response metadata records the limit so callers can distinguish a final retryable failure from a normal response.
References: aiobotocore/retryhandler.py• L19-60 aiobotocore/retryhandler.py• L90-145
Async check evaluation
Some checks need async response data. The handler accepts both direct results and awaitable results, so synchronous botocore checks and async checks share one decision path. This allows response-body checks to read content without blocking the event loop.
References: aiobotocore/retryhandler.py• L98-114 aiobotocore/retryhandler.py• L170-220
Standard retry strategy
The standard strategy is the normal retry path for service clients. It combines an attempt limit with checks for transient errors, throttling responses, modeled service errors, and selected special conditions. A retry quota must also have capacity before the handler schedules another request.
Retry decision flow
The following flow shows how a request moves from a retry event to another attempt or a final result.
flowchart TD
A[Request result or error] --> B[needs-retry event]
B --> C{Retry policy allows it?}
C -- No --> F[Return final result or error]
C -- Yes --> D{Retry quota available?}
D -- No --> F
D -- Yes --> E[Compute backoff delay]
E --> G[Wait without blocking the event loop]
G --> H[Send another attempt]
The policy checks the attempt limit and the retry conditions. If it allows a retry, the quota limits retry work. Backoff then supplies the delay. A completed request later releases quota capacity.
References: aiobotocore/…/standard.py• L30-100 aiobotocore/…/standard.py• L103-145 aiobotocore/…/standard.py• L155-192
Backoff and attempt limits
Standard retries use exponential backoff. The service name selects the retry setup, and throttling detection can affect the backoff choice. The maximum attempt value can come from service defaults or the caller's setting.
The retry handler places the maximum-attempt value in request metadata before the first request. This keeps the retry header consistent across the first attempt and later attempts.
References: aiobotocore/…/standard.py• L30-76 aiobotocore/…/standard.py• L78-100
Retry quota
Retry quota is a shared allowance for retry work. The handler acquires quota before it computes a retry delay. If quota is not available, the request is not retried. Long-polling operations have a special path that waits for the polling delay but still prevents a later handler from scheduling another retry.
References: aiobotocore/…/standard.py• L60-76 aiobotocore/…/standard.py• L107-145
Adaptive rate control
Adaptive rate control changes the allowed send rate when the service reports throttling. It works beside retry delay logic: standard retries decide whether a failed request may run again, while the adaptive limiter controls when requests may enter the send path.
Rate adjustment lifecycle
The limiter watches responses and records the observed request rate. Successful responses raise the calculated rate. Throttling responses lower or reset the rate and enable request gating. The new rate is bounded so one response cannot cause an abrupt increase beyond the measured rate scale.
The following flow shows how response signals update the gate used by later requests.
sequenceDiagram
participant R as Request
participant L as Adaptive limiter
participant S as Service
R->>L: before-send
L-->>R: Wait for token when gating is enabled
R->>S: Send request
S-->>L: Response or throttling error
L->>L: Measure rate and adjust target rate
L-->>R: Apply new token rate to future sends
References: aiobotocore/…/adaptive.py• L19-55 aiobotocore/…/adaptive.py• L88-120
Async backend selection
The limiter selects async primitives that match the transport backend. The aiohttp path uses asyncio primitives. The HTTPX path can use AnyIO primitives, so the same rate-control behavior works with asyncio or Trio.
References: aiobotocore/…/adaptive.py• L19-29 aiobotocore/…/adaptive.py• L123-129
Special retry conditions
Special conditions cover failures that need more context than a general status or exception check. The standard condition set includes an identity-provider communication failure and a DynamoDB response checksum failure.
Checksum retry condition
The DynamoDB-specific check applies only to the matching service and only when the response has the expected checksum. It reads the response body, compares the computed value with the service value, and marks a mismatch as retryable. A response without the required service context or header is not retried by this check.
References: aiobotocore/…/special.py• L1-22 aiobotocore/…/standard.py• L162-179
Configured response checks
The configuration-based handler supports service error codes, HTTP status codes, socket errors, and response-body CRC checks. These checks can be combined, so a request can retry when any configured rule identifies a retryable result.
References: aiobotocore/retryhandler.py• L29-87 aiobotocore/retryhandler.py• L170-181
Token-bucket rate control
The token bucket is the wait mechanism behind adaptive sending. It represents request capacity as tokens that refill over time. A request consumes tokens and waits for new capacity when the bucket cannot satisfy it.
Capacity and waiting
The bucket refills from elapsed time up to its current capacity limit. A blocking acquire waits asynchronously for enough tokens, so rate control delays the current request without blocking other event-loop tasks. A non-blocking acquire reports that capacity is unavailable instead of waiting.
When the adaptive limiter changes the rate, the bucket first accounts for tokens earned under the old rate. It then updates the fill rate and wakes waiting requests so they can use the new limit. The rate cannot fall below the minimum rate, and the bucket limits stored capacity when the new rate is lower.
References: aiobotocore/…/bucket.py• L9-31 aiobotocore/…/bucket.py• L44-69 aiobotocore/…/bucket.py• L79-119
Transport-compatible buckets
The HTTPX transport uses an AnyIO token bucket. Its public behavior matches the asyncio bucket, but its wait and condition operations use AnyIO primitives. This keeps rate limiting aligned with the execution model chosen by the transport.
References: aiobotocore/…/bucket.py• L123-138
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the retry event inputs aligned with the request context. The standard handler and the legacy configuration handler must both receive the attempt, response, exception, and retry context they expect.
- Keep maximum-attempt metadata and retry decisions consistent. A change to attempt counting can alter both request headers and the point at which a final error is returned.
- Keep quota acquisition paired with quota release after completed calls. A mismatch can stop later requests from retrying even when their policy allows it.
- Keep asyncio and AnyIO wait primitives matched to their transport backends. A limiter that uses the wrong primitive can fail at runtime or block the wrong scheduler.
- Keep adaptive response signals connected to the before-send gate. Updating the rate without applying it to later sends removes the protection against repeated throttling.
- Keep token accounting consistent when the fill rate changes. Refill under the old rate before applying the new rate, then cap stored capacity to the new limit.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Retry policy allows a retry but retry quota is exhausted | The request ends without another attempt | Acquire quota before computing the delay and release it after the call completes |
| Maximum attempts are reached for a retryable result | The client returns the final result or error and marks the response as having reached the limit | Preserve the attempt count and the MaxAttemptsReached response metadata |
| A response body is checked without async content handling | Retry evaluation can fail while reading the body | Use the async checksum checker and resolve awaitable checker results |
| Adaptive control receives a throttling response but the send gate is not enabled | Later requests continue at an unsafe rate | Enable the limiter after throttling and apply its updated token rate |
| An asyncio limiter or bucket is used with the AnyIO transport | The rate-control wait path can use an incompatible scheduler primitive | Select AnyIO classes for the HTTPX backend |
| The bucket changes its fill rate without first refilling under the old rate | Capacity earned before the change is counted at the wrong rate | Refill before changing the rate and cap current capacity to the new maximum |
Key files
| File | Why to read it |
|---|---|
| aiobotocore/…/standard.py | Defines the standard retry lifecycle, backoff setup, retry conditions, quota use, and event registration. |
| aiobotocore/retryhandler.py | Shows how configuration-based botocore retry checks and actions become async handler decisions. |
| aiobotocore/…/adaptive.py | Shows how throttling feedback changes the allowed request rate and connects rate control to client events. |
| aiobotocore/…/bucket.py | Defines the async token-bucket behavior used to wait for request capacity and the transport-specific wait primitives. |
| aiobotocore/…/special.py | Defines the async DynamoDB checksum condition used by the standard strategy. |