AWS Builder Center
Why Your AWS Lambda Function Runs Twice: Understanding Idempotency

Why Your AWS Lambda Function Runs Twice: Understanding Idempotency

Duplicate requests, unexpected retries, and repeated executions can cause serious problems in serverless applications. Learn how idempotency, AWS Lambda, DynamoDB, and conditional writes help build reliable distributed systems.

Why Your AWS Lambda Function Runs Twice: Understanding Idempotency
Imagine ordering something online and getting charged twice because the same payment request was processed two times.
Sounds frustrating, right?
This can happen in distributed systems when requests are retried, messages are delivered more than once, or functions fail unexpectedly.
One important concept that helps prevent this is Idempotency.
1. What Is Idempotency?
Idempotency means performing the same operation multiple times produces the same final result as performing it once.
For example:
- Clicking a button to turn OFF a light multiple times keeps it OFF. - Sending the same payment request multiple times should ideally result in only one charge.
That's the basic idea.
2. Why Do Duplicate Requests Happen?
In AWS applications, duplicate processing can occur because of:
- Network failures and client retries. - Lambda execution failures. - Amazon SQS at-least-once message delivery. - Automatic retry mechanisms.
The important thing to understand is that a retry doesn't always mean the previous request failed.
3. How Can AWS Handle This?
Let's consider a payment application.
A simplified architecture:
Customer → API Gateway → AWS Lambda → DynamoDB
When a payment request arrives:
1. Generate a unique idempotency key. 2. Check whether that key already exists in DynamoDB. 3. If it's a new request, start processing. 4. Store the result and mark the request as COMPLETED. 5. If the same request arrives again, return the previous result instead of processing it again.
This prevents duplicate business operations.
4. Why Are DynamoDB Conditional Writes Important?
Imagine two Lambda functions receive the same request simultaneously.
Both might check DynamoDB and find that the request doesn't exist.
This can create duplicate processing.
DynamoDB conditional writes help solve this problem.
Using a condition such as:
"attribute_not_exists(idempotencyKey)"
allows only one execution to create the initial record.
The other execution must handle the condition failure safely.
5. What If Lambda Crashes?
Here's where things get interesting.
Imagine Lambda successfully charges a customer but crashes before updating the DynamoDB record.
A retry might see an incomplete record even though the payment succeeded.
That's why reliable implementations need:
- Processing states. - Recovery mechanisms. - Proper expiration policies. - Downstream idempotency support.
For payment operations, using the same idempotency key with a payment provider that supports it is particularly important.
6. Can AWS Make This Easier?
Yes!
AWS Lambda Powertools provides an Idempotency utility that helps developers manage duplicate requests, execution states, and previously stored results.
It can save developers from implementing the entire mechanism manually.
Final Thoughts
In distributed systems, failures, retries, and duplicate requests are part of reality.
The goal isn't to prevent every failure.
It's to make sure your application behaves correctly even when failures happen.
A reliable application isn't one that never receives duplicate requests. It's one that knows how to handle them.
Let's discuss!
If you were building a payment application using AWS Lambda and DynamoDB, where would you implement idempotency — at the API layer, Lambda layer, or both?
Drop your thoughts below! 👇
#AWS #AWSLambda #DynamoDB #Serverless #CloudComputing #SystemDesign #DistributedSystems
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