Introducing a new way to review your PRs.

Read the announcement

aio-libs / aiobotocore

asyncio support for botocore library using aiohttp

View on GitHub

18 documents · Updated Nov 20, 2026

docs / retry-and-resilience

Retry and resilience

Retry and resilience preserve botocore's service retry rules while requests run in an async event loop. The system selects a retry mode from client configuration. It then connects botocore's retry decisions to non-blocking waits, rate control, request replay, and response metadata.

Retry lifecycle

The retry lifecycle turns one service call into a controlled sequence of attempts. The client installs handlers for the request and response events. The endpoint asks those handlers whether to retry after each response or transport error.

This diagram shows how a call moves from its first attempt to either a final result or a retry.

flowchart TD
    A[Client retry configuration] --> B[Retry mode registration]
    B --> C[Create and send request]
    C --> D[Receive response or error]
    D --> E{Retry policy allows another attempt?}
    E -- No --> F[Return result or raise error]
    E -- Yes --> G[Apply quota and delay or rate limit]
    G --> H[Reset request body and sign a new request]
    H --> C
    G --> I[Record retry count]
    I --> F

The endpoint counts the first send as an attempt and reports only later sends as retries. A retried stream is reset before the new request is made. A successful response includes the retry count in ResponseMetadata so callers and downstream logs can distinguish retries from total attempts. References: aiobotocore/client.py• L143-176, aiobotocore/endpoint.py• L114-153

Async event boundaries

The retry system keeps botocore's event contract while allowing handlers to return awaitable results. Standard retry checks and adaptive rate checks run at the same request and response boundaries as botocore. The HTTP session decides whether those waits use asyncio or AnyIO primitives.

The async boundary matters because a retry delay must yield control to other tasks rather than pause the whole process. The standard handler uses an async sleep. The adaptive handler uses a matching lock and token bucket for the selected HTTP backend. References: aiobotocore/…/standard.py• L30-38, aiobotocore/…/standard.py• L103-152, aiobotocore/…/adaptive.py• L19-55, aiobotocore/_async_primitives.py• L8-16

Retry modes

Retry mode is the top-level policy choice for a client. Standard mode uses botocore's current retry policy. Adaptive mode adds client-side rate control on top of standard mode. Legacy mode translates botocore's older service retry configuration into an async handler.

ModeMain purposeResilience control
StandardUse the current botocore retry behaviorRetry conditions, attempt limit, backoff, and retry quota
AdaptiveReduce request pressure during throttlingStandard behavior plus a measured send-rate limit
LegacyKeep older botocore service retry rulesService policy checks and async retry actions

The client registers only the handlers required by the selected mode. Adaptive mode registers both standard and adaptive handlers, so rate limiting does not replace the normal retry decision. References: aiobotocore/client.py• L166-188, aiobotocore/client.py• L190-219

Standard controls

Standard mode decides whether a response or error is retryable and whether another attempt is available. Its policy combines an attempt limit with transient errors, throttling errors, modeled service errors, and selected special checks. A retry quota must also have capacity before the request is retried.

The async version keeps these botocore policy parts and changes the waiting path to an awaitable one. It also keeps service-specific attempt limits and seeds the maximum attempt value into request metadata. When quota is exhausted, the normal result is no further retry. Long-polling operations have a special wait path in the current retry policy. References: aiobotocore/…/standard.py• L40-100, aiobotocore/…/standard.py• L103-145, aiobotocore/…/standard.py• L155-193

Adaptive controls

Adaptive mode adds a client-side send-rate limit that responds to throttling. The limiter measures request rate and changes its token bucket rate after each response. A throttling response enables the bucket. Later requests must obtain capacity before they are sent.

Successful responses can raise the allowed rate. Throttling responses lower it. The limiter caps a rate increase relative to the measured rate and keeps a minimum bucket rate. This prevents the control from stopping progress completely. References: aiobotocore/…/adaptive.py• L31-55, aiobotocore/…/adaptive.py• L58-120, aiobotocore/…/bucket.py• L44-69

Async bucket controls

The token bucket is a shared rate gate for adaptive requests. It refills with elapsed time and holds a bounded amount of capacity. A request can wait for capacity without blocking the event loop. When adaptive control changes the rate, the bucket updates pending capacity before applying the new rate and wakes waiting requests so the new rate can take effect.

The asyncio HTTP path uses asyncio conditions. The HTTPX path uses AnyIO conditions so the same control works with asyncio or Trio. The bucket can also reject a request when a caller asks for non-blocking acquisition and capacity is not available. Adaptive request handling uses the blocking form because its purpose is to pace sends rather than fail them only because the current rate is full. References: aiobotocore/…/bucket.py• L9-42, aiobotocore/…/bucket.py• L44-69, aiobotocore/…/bucket.py• L71-119, aiobotocore/…/bucket.py• L123-138, aiobotocore/…/adaptive.py• L88-90

Special-case controls

Some service faults need a check that is not part of the general retry categories. The standard policy includes a DynamoDB checksum check. It reads the response body asynchronously and marks the response retryable when its CRC32 value does not match the service checksum.

Legacy retry configuration also supports response checks and retryable exceptions. The async retry handler preserves those checks and adapts checker results before it applies the configured action. References: aiobotocore/…/standard.py• L167-177, aiobotocore/…/special.py• L4-22, aiobotocore/retryhandler.py• L29-87

Retry strategy execution

The mode and control choices above describe why the library retries. The child page Retry strategies describes how each strategy evaluates an attempt, selects a delay, and hands control back to the async endpoint.

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the retry mode registration and event-handler ownership aligned. Adaptive mode depends on both standard retry handling and adaptive rate handling.
  • Keep the endpoint's attempt and retry counters aligned with RetryAttempts and the amz-sdk-request maximum-attempt metadata.
  • Preserve the async primitive selected by the HTTP backend. A blocking lock, condition, or sleep can stall unrelated tasks.
  • Keep retry-body reset and request recreation together. A retried streaming request must send a complete body and receive a fresh signature.
  • Keep legacy retry configuration translation compatible with the async checker and action interfaces.
  • Keep token-bucket rate changes synchronized with capacity refill and waiting request notification. A stale capacity value can pace requests at the wrong rate.

Important failure modes

TriggerConsequenceGuard
A retry handler uses a blocking sleep or conditionOther tasks stop while the client waitsUse the asyncio or AnyIO async wait selected for the HTTP backend
Adaptive registration replaces standard registration instead of adding to itThrottled calls lose normal retry and attempt-limit behaviorRegister standard handling before adaptive handling
A request body is not reset before a retryA streamed request can resend only part of its bodyReset the stream and create a new signed request for every retry
Retry quota has no capacityA retryable result is returned without another requestTreat quota exhaustion as the end of the retry sequence
A response body checksum does not matchThe service response is treated as unreliableApply the service-specific checksum retry check
A rate change does not refill or wake the token bucket correctlyWaiting requests use stale pacing and can wait too longRefill before setting the rate and notify the condition after the change

Key files

FileWhy to read it
aiobotocore/client.pyShows how client configuration selects legacy, standard, or adaptive handling.
aiobotocore/endpoint.pyShows the attempt loop, request replay, and retry metadata flow.
aiobotocore/…/standard.pyShows the botocore-compatible standard policy and async retry delay path.
aiobotocore/…/adaptive.pyShows how throttling changes the client send rate.
aiobotocore/…/bucket.pyShows the async capacity gate and rate-change coordination used by adaptive control.
aiobotocore/…/special.pyShows the service-specific DynamoDB checksum retry rule.
aiobotocore/retryhandler.pyShows how legacy retry configuration becomes an async event handler.
aiobotocore/_async_primitives.pyShows how the HTTP backend selects asyncio or AnyIO synchronization primitives.