AWS Builder Center

Your Lambda sent the same email twice. Which fix would you ship?

A choose-your-fix debugging exercise for Lambda and Amazon SQS, with a crash test that exposes the gap between retrying a message and repeating a side effect.

Imagine this: a Lambda worker reads an order-confirmation job from a standard Amazon SQS queue. The email provider accepts the request. A moment later, the function times out. When the job runs again, the customer gets a second confirmation.
Pick the first change you'd ship:
  • A. Give the function a longer timeout.
  • B. Enable partial batch responses.
  • C. Give the confirmation a stable operation ID and make the send idempotent.

What each fix buys you

A can help if the original timeout was too tight. It still leaves a window where the provider has accepted the send and your function hasn't finished. A crash at that exact point is enough.
B reduces the blast radius of a failure. With partial batch responses configured, the handler can report specific failed records so Lambda can retry those records. That keeps successfully processed records out of that batch retry. The failed record can still repeat a side effect it already started.
C targets the duplicate email. Give the operation a business key such as  order-42:confirmation:v1 . Reuse it across deliveries and producer retries. When the provider supports idempotency keys, pass that same key on every send attempt and check how long the provider honors it. Make sure the retry window fits that guarantee.
A conditional write in DynamoDB can help track which worker owns the attempt and which operations completed. There is still a gap between an external send and updating your database. Marking the job done before sending can lose the email. Marking it done after sending can duplicate it if the worker crashes in between. If the provider offers no deduplication or way to reconcile a send, decide which failure your product can tolerate and make that choice visible to operators.

Try to break it in a sandbox

  1. Route email to a fake sender and record the operation ID and whether a new delivery occurred.
  1. Make the worker fail immediately after the sender accepts one record. Include a healthy record in the batch as a control.
  1. Replay the failed record, then enqueue the same business operation as a new message. That second step catches deduplication tied only to the SQS message ID.
  1. Count actual deliveries per operation ID. The expected result is one confirmation for each order, even when attempts repeat. For a stronger test, send two copies concurrently.
Your call: Your email provider has no idempotency key and no delivery lookup. Would you automatically retry the ambiguous send, or send it to a review queue? What would change your answer if this were a password reset email?
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