Beginner · About 10 minutes · Reliable applications
One Order. Two Emails.
Maya ordered once. Her confirmation arrived twice. How would you prevent the second email?
Answers stay in this browser until you save them on this device.
What happened?
Maya buys a pair of shoes online. Her order number is 4821.
The store receives a message saying that the order was placed. It sends her a confirmation email.
A few seconds later, the same order message arrives again. The store sends another email.
Maya placed one order, but received two confirmation emails.
1. One order
Maya places Order #4821.
Order
#4821
2. The same message arrives twice
Two messages to the store, both for Order #4821.
Message to store
Order #4821
Message to store
Order #4821
3. Two confirmation emails
Two emails to Maya for Order #4821.
Email to Maya
Order #4821
Email to Maya
Order #4821
Sent twice
Your task
How would you stop the second confirmation email while still allowing emails for new orders?
Assumptions for this first challenge
Keep this first challenge simple: messages are handled one at a time. Sending an email and saving its record both succeed.
You do not need to write code. Explain your idea in your own words.
Related path: System Design Interview Sprint
Interview answer
Explain it in an interview
Maya placed shoe order 4821 once and got two confirmation emails. How would you stop the second send?
Maya bought one pair of shoes and the store assigned order 4821. The first order-placed message triggered a confirmation email to her. A few seconds later the same message arrived again, and the store sent a second email. I treat order 4821 as the identity of that confirmation, not each arrival of the message.
Step 1
Order 4821 was confirmed twice
The store handled one order-placed message for 4821, sent Maya the confirmation, and then saw that same message again. The second delivery was processed as a new event, so another email went out. Maya did not place a second order. The duplicate came from the message being delivered twice.
Step 2
A memory check misses the next copy
Skipping the send because this process already saw 4821 fails after a restart, and it fails if another handler takes the repeat. A primary key on the order stops a second row, but it does not make the email send and the insert one operation. If I send and crash before the save, the retry looks new and Maya can get another email.
Step 3
One saved confirmation per order
Before I send, I look up order 4821. When a row already exists for the same recipient, I return already_sent and I do not call the sender. When the stored recipient is different, I return a conflict and leave the row alone. A different order number is a new confirmation, even for the same person.
Step 4
Commit the row after the send returns
The handler calls the simulated sender and only then inserts the confirmation row. The caller hears success after that insert commits. The row means this order was confirmed for that exact recipient, and there is no separate status field. Comparison of the address is exact, so I do not trim or rewrite it.
Step 5
Retry only when the row is known
If the row is already there, a repeat is safe and returns the earlier result. If the sender fails, I save nothing, so a later attempt can try again. A failure after the send may mean the email went out and the save did not, so I do not automatically retry and I do not tell Maya the outcome is certain.
Step 6
Do not claim exactly-once email
I can promise at most one saved confirmation row for order 4821 in this local store. I cannot promise her inbox received that message once, because the send and the save are separate steps. This is one sequential process with a simulated sender. It is not a mail provider, and a 500 response leaves the result uncertain.
Do not stop here
I would ignore the second request in memory, or send the email and trust a unique order number to make the send happen only once.
Say this boundary
I return the saved confirmation for order 4821 when the recipient matches, and I admit a crash between send and save can still produce a second email.
Leave these claims out
- Exactly-once email delivery
- The primary key makes send and save atomic
- A 500 means the email was not sent
For order 4821 I would show the lookup, the already_sent path, and the gap between the simulated send and the commit. That gap is the boundary I can defend from this lab.
Now say the same idea in your own words, using the story from this challenge.