AWS Builder Center

Making AWS Lambda Retries Safer with Idempotency

When an AWS Lambda function processes an event, a timeout or temporary failure can cause the event to be delivered again. That is useful for reliability, but it also means the same business operation might run twice.

When an AWS Lambda function processes an event, a timeout or temporary failure can cause the event to be delivered again. That is useful for reliability, but it also means the same business operation might run twice.
For example, an order event could trigger two payment attempts, or a repeated message could create duplicate database records. The function should be designed so that processing the same event more than once does not repeat the business change.
A common approach is to give each operation a stable idempotency key. The key might come from an order ID, payment request ID, or another identifier that stays the same across retries. Before applying the change, the function records or checks that key using a conditional write in a store such as Amazon DynamoDB. If the key has already been processed, the function can skip the duplicate operation.
The record should represent the operation’s state, not just that a request was seen. For longer workflows, states such as IN_PROGRESS and COMPLETE can help the function distinguish a retry from work that failed halfway through. Expiration rules can prevent old keys from being retained forever.
Idempotency does not remove the need for retries, monitoring, or error handling. It makes retries safer by ensuring that duplicate deliveries do not create duplicate business outcomes.
When building event-driven systems with Lambda, SQS, or other sources, ask: “If this event arrives again, what stops the operation from happening twice?” That question is a useful starting point for a more reliable design.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article