AWS Builder Center
One Click, Two Payments? Understanding Idempotency in AWS

One Click, Two Payments? Understanding Idempotency in AWS

What happens when an API request is repeated or a Lambda function retries an operation? Learn how idempotency prevents duplicate payments, orders, and unwanted side effects in AWS applications.

Computer Engineering Student | AWS | AI Agents | Cloud Computing | Cybersecurity
A user clicks Pay once, but the network is slow. They click again. Or an AWS Lambda function gets invoked again after a retry.
Suddenly, one order might create two payments, two records, or duplicate notifications.
This is a challenge that reliable applications need to handle: duplicate requests.
The solution has a name: idempotency.

✨ What does idempotency mean?

In simple terms, idempotency means that performing the same operation multiple times produces the same result without repeating its side effects.
Imagine this:
  • A customer submits an order with ID  ORDER-1024 .
  • The application processes the order successfully.
  • The request is accidentally sent again.
  • Instead of creating another order, the application recognizes the request and returns the previous result.
One logical request. One successful operation.

☁️ Why does this matter in AWS?

AWS applications often use event-driven and serverless architectures.
For example:
  • A client sends a request through Amazon API Gateway.
  • AWS Lambda processes the request.
  • Amazon DynamoDB stores the order.
  • Amazon SQS or Amazon EventBridge may deliver events to other services.
Retries help applications recover from temporary failures. However, some event sources provide at-least-once delivery, meaning the same event can be delivered more than once.
Your application should never assume that every event will arrive only once.

🔑 The little trick: Idempotency keys

An idempotency key is a unique identifier for one logical operation.
For example:
 order-1024-payment 
When the application receives a request, it checks whether that key has already been processed.
  • If the key is new, process the operation.
  • If the key already exists, avoid repeating the operation.
  • If the request is still being processed, handle the duplicate safely.
The important detail: the key must remain the same when the same logical request is retried.
Generating a completely new key for every retry defeats the purpose.

🛠️ How can we build this with AWS?

A simple architecture could look like this:
Client → API Gateway → Lambda → DynamoDB
Here's the responsibility of each component:
1. Amazon API Gateway
Receives the client's request and passes it to the backend.
2. AWS Lambda
Validates the request and checks its idempotency key before performing the operation.
3. Amazon DynamoDB
Stores the idempotency key and processing status. Conditional writes help prevent multiple requests from claiming the same key simultaneously.
4. AWS Lambda response
Returns the stored result when the same completed request arrives again.
This gives the application a way to recognize duplicate requests instead of blindly processing them.

💻 A small Python example

DynamoDB supports conditional writes. Here's a simplified example of registering a request only if its key does not already exist:
import boto3

table = boto3.resource("dynamodb").Table("IdempotencyRecords")

def register_request(request_id):
try:
table.put_item(
Item={
"id": request_id,
"status": "IN_PROGRESS"
},
ConditionExpression="attribute_not_exists(id)"
)

return "NEW_REQUEST"

except table.meta.client.exceptions.ConditionalCheckFailedException:
return "DUPLICATE_REQUEST"
This example demonstrates how to claim a request key.
A production system also needs to manage completed results, failures, timeouts, and concurrent requests. For external payment operations, use the same idempotency key with the payment provider whenever supported. A DynamoDB record alone cannot make an external payment and a database update atomic.

🎀 AWS has a helper for this!

You don't always need to build the entire mechanism yourself.
AWS Lambda Powertools for Python provides an Idempotency utility.
It can help developers:
  • Create and manage idempotency keys.
  • Store execution records.
  • Return previous successful results for repeated requests.
  • Handle concurrent requests and certain timeout scenarios.
It supports persistence using Amazon DynamoDB and compatible cache services.

🧠 Five things developers should remember

  1. Never assume a request or event will arrive only once.
  1. Use a stable idempotency key for each logical operation.
  1. Use conditional writes or another reliable deduplication mechanism.
  1. Think carefully about retries after partial failures.
  1. Test duplicate requests and simultaneous requests before deployment.

🌷 A little challenge for beginners

Imagine you're building an online shopping application using AWS.
A customer clicks the Place Order button twice because the page takes too long to respond.
How would you make sure that only one order is created?
Think about where you would store the idempotency key and how your Lambda function would check it.

Final thoughts

Retries are important because failures are inevitable in distributed systems.
But a retry should not accidentally become a second payment, a duplicate order, or another unwanted action.
Building reliable applications isn't only about making services recover from failures. It's also about making sure those recoveries are safe.
Sometimes, one small concept like idempotency makes a very big difference. ☁️
I'd love to know: Have you ever encountered duplicate requests while building an application? How did you handle them?
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