AWS Builder Center

Your Lambda Ran Twice. Should Your App Apologize Twice?

A spot-the-bug challenge for serverless builders: one workshop registration, two welcome emails, and a retry that exposes the difference between reliable delivery and reliable outcomes. Pick your fix before reading the walkthrough.

You register for a student workshop.
One click. One seat. Two welcome emails.
Apparently, the cloud is very excited that you are attending.
Here is a small architecture puzzle. No benchmark claims, no invented production incident—just a hypothetical failure worth thinking through before it becomes a real one.

Pick your fix before scrolling

A registration API publishes a job to an Amazon SQS Standard queue. A Lambda worker saves the registration and asks an external email service to send the welcome message.
The email service accepts the request. Then the worker times out before it finishes.
Later, the message is processed again.
What would you change first?
  • A: Increase the timeout.
  • B: Record that the job is done before sending.
  • C: Record that the job is done after sending.
  • D: Give the business operation a stable identity and design recovery around uncertain outcomes.
Keep your choice. The uncomfortable part is that both B and C leave a hole.

The bug is between two successful steps

This pseudocode looks reasonable:
# Deliberately incomplete pseudocode: spot the failure window.
if not already_processed(registration_id):
save_registration(registration_id)
send_welcome_email(registration_id)
mark_processed(registration_id)
Now place a crash between the email being accepted and  mark_processed .
The retry sees an unfinished job. It sends another email.
Move  mark_processed  before the send and you create the opposite failure: a crash after the marker but before the email can leave the student with no message at all.
There is a concurrency problem too. Two workers can both pass a separate “already processed?” check before either writes its marker.
A boolean called  done  is carrying more responsibility than it applied for.

First, accept the delivery contract

AWS documents that Lambda event source mappings can process records more than once. Your handler needs to tolerate repeat processing.
With an SQS Standard queue, design for duplicate deliveries. A longer timeout may help a slow operation, but it does not establish that the business action happens only once.
When a batch contains several jobs, partial batch responses can reduce unnecessary retries of successful records. Enable  ReportBatchItemFailures  on the event source mapping and return failed message IDs in the documented response format.
That improves batch recovery. It does not make an external email send atomic with a database update.

Give the operation an identity

For this example, I would define the intended action as:
 workshop_id + registration_id + "welcome-email" + template_version 
The producer must reuse that identity when retrying the same action. Generating a new random key on each retry defeats the purpose.
Including a version is a product decision: is sending the revised welcome email a new operation, or should it still count as the original one?
Also validate the payload associated with a key. Reusing an identity for a different recipient should raise a conflict, rather than quietly return an unrelated earlier result.

Track the uncertainty explicitly

A design sketch:
State
Meaning
Possible next step
New
No recorded attempt
Atomically claim the operation
In progress
A worker owns an attempt
Concurrent attempts wait or retry
Complete
A confirmed result was stored
Reuse the stored outcome
Uncertain
The external outcome is unknown
Reconcile before repeating the action
“Uncertain” is a conceptual application state here, not a built-in Powertools status.
An atomic claim avoids the check-then-write race. In DynamoDB, a conditional write can create a claim only when an item with that key does not already exist; recovery of stale claims needs additional ownership and expiry logic. Powertools for AWS Lambda provides idempotency utilities with persistent state, cached results, and handling for concurrent or expired attempts. Its key selection and expiry settings still need to match your workflow.
Most importantly, an idempotency wrapper around a function cannot turn two independent systems into one transaction.

Put protection where the effect happens

If the email provider supports idempotent requests, pass the stable operation key and verify its retention period and retry semantics. Recovery after a timeout should consult the provider's documented behavior.
If the provider offers a status lookup or webhook, record its operation identifier and use it to reconcile an ambiguous response.
If it offers neither, be honest about the trade-off. Automatic retries may duplicate a message; refusing to retry may lose one. A database marker alone cannot prove which external outcome occurred.
A transactional outbox can help keep an internal registration change consistent with the intent to send a message. The dispatcher still needs a strategy for duplicate sends and uncertain provider responses.

A tiny failure lab

Before calling this reliable, try these cases in a sandbox:
  1. Deliver the same business operation twice.
  1. Start two workers for the same key simultaneously.
  1. Simulate a timeout after the provider accepts the request.
  1. Retry after your idempotency retention window expires.
  1. Replay an old failed job after the welcome template changes.
Check the actual registration and email outcomes, alongside the function logs. Five green invocations do not prove five correct business results.

Your turn: defend the trade-off

For a workshop welcome email, would you prefer an occasional duplicate or a possible missing message when the provider's outcome cannot be recovered?
Would your answer change for a seat reservation?
Comment with A, B, C, or D from the opening challenge—and the failure window you would fix first. If you chose D, the harder question is: where does your guarantee stop?

References

#AWSLambda #AmazonSQS #Serverless #DistributedSystems #CloudEngineering
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