Lesson 3 of 3 AWS Step Functions

Handling Transient Failures with Retries, Backoff and Circuit Breakers

This lesson explains how distributed applications handle transient failures, such as throttling and timeouts, without overwhelming the services involved.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish errors that are worth retrying from errors that are not.
  • Explain how exponential backoff and jitter prevent retry storms.
  • Describe how timeouts, circuit breakers and other techniques stop a failing dependency from spreading failure.
  • Explain how the Retry fields of a Step Functions Task state control backoff.

Retrying safely

A retry is useful only when the error is temporary. Throttling errors (HTTP 429) and server errors (HTTP 5xx) usually reflect a momentary condition, such as a burst of traffic or a brief service problem, so the same request is likely to succeed a little later. Most other client errors (HTTP 4xx), such as a validation error or AccessDenied, are caused by the request itself and will fail every time until the code or permissions change, so retrying them only adds load and delay. The following table summarises the rule.

ErrorRetry?
Throttling (429) and 5xx server errorsYes, with exponential backoff and jitter
Other 4xx client errors, such as validation or access deniedNo, they will fail again

Even temporary errors need careful retries. If every client retries at once, the retries themselves can overload the service again. The following list describes the techniques that make retries safe.

  • Exponential backoff: wait longer after each attempt.
  • Jitter: add randomness so many clients do not retry in lockstep.
  • SDK retry modes: standard uses backoff with a retry quota; adaptive also slows the client when it detects throttling. Maximum attempts are configurable per client.

Example. Exponential backoff with full jitter in application code:

import random, time

def call_with_backoff(fn, attempts=5, base=0.2, cap=10):
    for attempt in range(attempts):
        try:
            return fn()
        except TransientError:
            if attempt == attempts - 1:
                raise
            time.sleep(random.uniform(0, min(cap, base * 2 ** attempt)))

The following diagram compares retries with and without jitter for two clients throttled at the same moment.

Retries with exponential backoff. Without jitter, two clients that were throttled together retry together after 1, 2 and 4 seconds and keep colliding. With jitter, each client waits a random part of the growing interval, so their retries spread out and succeed sooner. Throttling and 5xx errors are worth retrying; other 4xx errors are not.
The timings are illustrative. Jitter spreads the retries out, so the service is not hit by every client at the same instant.
Sort into groups

Retry or not?

Exam skills 1.1.3

Decide which errors are worth retrying.

Log in to try this activity Interactive activities are free with an account.

Protecting against failing dependencies

When a dependency fails or slows down, the danger is that the failure spreads: callers wait, their resources fill up, and soon they fail too. Four techniques contain the damage.

A timeout limits how long a caller waits. Without one, a slow dependency holds threads, database connections or Lambda execution environments until the caller runs out of them, so timeouts should be shorter than the caller's own deadline. A circuit breaker tracks failures. After a threshold is reached it opens, and calls fail at once or return a fallback, such as cached data, without contacting the dependency. After a pause, it lets a test request through and closes again if the dependency has recovered. This gives the dependency time to recover instead of being flooded with retries.

A bulkhead isolates dependencies from one another, so a problem with one cannot consume the capacity needed for others. In Lambda, giving each function reserved concurrency, or sending each kind of work to its own queue, means a backlog in one area does not starve the rest. A dead-letter queue sets aside messages that keep failing, so they stop consuming retries and blocking other work, and can be inspected and redriven later. The following table summarises the techniques.

TechniqueWhat it does
TimeoutsStop a slow dependency tying up threads, connections or Lambda concurrency
Circuit breakerAfter repeated failures, stop calling the dependency and fail fast or return a fallback, then test later whether it has recovered
BulkheadsIsolate dependencies with separate concurrency limits, such as reserved concurrency or separate queues
Dead-letter queuesSet aside work that keeps failing instead of retrying for ever

The following diagram shows the three states of a circuit breaker and what moves it between them.

The three states of a circuit breaker. While closed, calls pass through and failures are counted. When failures reach a threshold, it opens, and calls fail at once or return a fallback without contacting the dependency. After a pause it is half-open and lets one test request through: success closes it, failure opens it again.
While the breaker is open, the failing dependency receives no calls, which gives it time to recover.
Match the pairs

Resilience techniques

Exam skills 1.1.3

Match each resilience technique with what it does.

Log in to try this activity Interactive activities are free with an account.

Retries in Step Functions

A Task state's Retry field lists the errors to retry, with IntervalSeconds, MaxAttempts, BackoffRate, MaxDelaySeconds and JitterStrategy; Catch sends remaining errors to a fallback state.

Example. With an interval of 2, a backoff rate of 2 and 4 attempts, the retries wait 2, 4, 8 and 16 seconds, giving up about 30 seconds after the first failure. "JitterStrategy": "FULL" randomises each wait.

AWS's reference circuit breaker uses a Step Functions workflow that records each dependency's status in DynamoDB and skips calls while the circuit is open.

Fill in the value

Calculate the backoff

Exam skills 1.1.3

Work out how long Step Functions retries wait.

Log in to try this activity Interactive activities are free with an account.
Worked example

A retry storm

A payment provider has a brief outage. Every client retries immediately and repeatedly; when the provider recovers, it is overwhelmed by the backlog of retries and fails again.

PracticeEffect
Exponential backoffEach retry waits longer: 0.2 s, 0.4 s, 0.8 s...
JitterRandom delays spread retries out so clients do not retry in sync
Maximum attemptsStops retrying after a limit and reports the failure
Circuit breakerAfter repeated failures, stop calling the provider for a period and fail fast or use a fallback
Idempotency keysRetried payments are not charged twice

The AWS SDKs apply backoff with jitter automatically; Step Functions applies it through Retry with BackoffRate and JitterStrategy. Code that calls third parties directly must implement it itself.

Check yourself

Why should retries use exponential backoff with jitter?

Show answer

Backoff spaces retries out so a struggling dependency can recover. Jitter randomises the delays so that many clients do not retry at the same moment and cause a retry storm.

Exam Tips

Remember for the exam

  • Throttling from an AWS API calls for exponential backoff with jitter.
  • To stop hammering a failing third-party service, use a circuit breaker.
  • Do not retry 4xx client errors, other than throttling.
  • To make a client slow down automatically when throttled, use the SDK's adaptive retry mode.
  • To contain the impact of a failing dependency, use bulkheads with separate concurrency limits.