
What Happens When Your AWS Application Processes the Same Request Twice?
Building Idempotent Systems with AWS Lambda, SQS, DynamoDB, and Dead-Letter Queues.
Imagine placing an order on an e-commerce website.
You click the checkout button, the payment gets processed, but suddenly your internet connection drops. You don't know whether the transaction succeeded, so you click the button again.
What happens next?
If your backend isn't designed properly, you might receive two orders, two payments, or two notifications for a single user action.
This is one of the most important challenges in distributed systems: Duplicate Request Processing.
1. Why Do Duplicate Requests Happen?
Duplicate processing can occur because of:
- Network timeouts
- Client-side retries
- Lambda execution retries
- SQS message redelivery
- Temporary service failures
- Uncertain responses from external APIs
The problem is that a failed response doesn't necessarily mean a failed operation.
Your application needs a way to recognize whether an operation has already been completed.
2. Understanding Idempotency
Idempotency means that executing the same operation multiple times produces the same final result as executing it once.
For example:
- First request: Create order #101.
- Second identical request: Return order #101.
- Third identical request: Return order #101.
Instead of creating three orders, the system processes the operation only once.
A common solution is an Idempotency Key.
When a client sends a request, it includes a unique key:
Idempotency-Key: order-user123-xyz The backend stores this key along with the operation's status and result.
When the same request arrives again, the backend checks the key and avoids repeating the operation.
3. Designing the Solution Using AWS
We can build a reliable architecture using:
- Amazon API Gateway – Receives incoming requests.
- AWS Lambda – Handles business logic.
- Amazon DynamoDB – Stores idempotency keys and processing status.
- Amazon SQS – Manages asynchronous tasks.
- SQS Dead-Letter Queue – Captures messages that repeatedly fail.
- Amazon CloudWatch – Monitors errors and processing metrics.
Request Processing Flow
- The client sends a request with an idempotency key.
- API Gateway forwards it to Lambda.
- Lambda checks DynamoDB for the existing key.
- If the operation is completed, the previous result is returned.
- If it is new, the operation is registered using a conditional write.
- The task is submitted to SQS for asynchronous processing.
- A worker Lambda processes the message.
- The final result is stored in DynamoDB.
If a message is delivered again, the worker checks its processing record before executing the business operation.
4. What If Something Fails?
Consider three scenarios:
Scenario A: Lambda fails before processing
The message can be retried.
Scenario B: Lambda processes the task but crashes before acknowledging it
SQS may deliver the message again. The idempotency check prevents repeating the completed operation.
Scenario C: The message repeatedly fails
After the configured number of retries, it can be moved to a Dead-Letter Queue for investigation.
One important detail: idempotency must cover the actual side effect, not just message reception. For example, payment providers should also receive a stable idempotency key, because a database record alone cannot guarantee that an external payment won't be charged twice.
5. Important Engineering Considerations
When implementing idempotency, consider:
- Use DynamoDB conditional writes to avoid concurrent duplicate registrations.
- Store operation status such as PROCESSING, COMPLETED, and FAILED.
- Define an appropriate idempotency-key expiration period.
- Handle concurrent requests carefully.
- Configure SQS visibility timeout and retry policies.
- Monitor failed messages and DLQ activity.
Remember that SQS Standard queues provide at-least-once delivery. Your consumers should be designed to handle duplicate messages.
6. Final Thoughts
Building a distributed application isn't just about making requests succeed. It's about ensuring that retries, network failures, and unexpected execution behavior don't corrupt your business operations.
Idempotency is a small design decision that can prevent significant production problems.
The next time you build an e-commerce backend, payment system, or event-driven application, don't just ask:
"Can my application process this request?"
Also ask:
"What happens if it processes the same request twice?"
Because reliable systems are designed not only for successful execution but also for unexpected failures.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article