Designing Resilient Serverless Systems with AWS Lambda
Instruction: Explain how you would design a resilient serverless system using AWS Lambda, considering aspects such as error handling, retry logic, and idempotency.
Updated
Official answer
Start with the delivery contract. A synchronous caller decides whether to retry; Lambda's asynchronous queue and an SQS/stream mapping have different retry, retention and failure controls. Set bounded timeouts and retries with backoff/jitter where supported, and avoid multiplying independent retry layers.
Use a stable business operation ID that survives redelivery. An invocation-attempt ID alone may change between attempts. Record idempotency/progress durably and coordinate it with the effect:
- If the effect and deduplication marker share a database transaction, commit them together.
- For an external payment API, use that provider's supported idempotency key and reconcile uncertain outcomes.
- A marker written before the effect can suppress work after a crash; a marker written after the effect can allow a duplicate after a crash. A conditional write alone does not close both windows.
Reject permanent invalid requests clearly, and retain failures according to the source-specific policy. For SQS, use appropriate visibility timeout, partial batch responses and queue redrive. Alarm on backlog age, failure-delivery problems and downstream saturation. Limit concurrency when the sink cannot absorb more load.
For an order workflow, test a timeout after payment succeeds but before local acknowledgement, duplicate delivery, a poison message and dependency unavailability. Success means the intended business result is recorded and recoverable, not merely that the function returned without an exception.