Beginner · About 10 minutes · Distributed Systems
Optional Python lab
The Driver Received Your Message Twice.
Maya sent one message. Her phone tried again, and the driver saw two copies. How would you prevent this?
Answers stay in this browser until you save them on this device. Drafts are not sent to analytics.
What happened?
Maya books a ride. She sends her driver:
Please meet me at the side entrance.
The server saves her message. Before her phone receives confirmation, its connection drops.
Her phone tries again. The driver sees the same message twice.
Your approach
How would you let the phone retry while keeping only one saved copy of that message?
Optional hints
- The message and the confirmation travel separately.
- Give each new message a number that stays the same when the phone retries.
- The same words can be part of two different messages.
Related path: System Design Interview Sprint
Interview answer
Explain it in an interview
Maya sent one message to her driver. Her phone retried after the connection dropped, and the driver saw two copies. How do you prevent that?
Maya sent one note on ride 4821, client message msg-101, asking the driver to meet her at the side entrance. The connection dropped before she got a confirmation. The phone sent the same message again. The driver saw two copies.
Step 1
The retry looked like a new note
The first request may have been saved before the response was lost, and the second request carried the same words with no link to the first save. The driver timeline appended both. Maya did not choose to send a second note. The duplicate was the phone retrying msg-101.
Step 2
The phone's memory is not enough
If the phone forgets the id when the app restarts, the retry is a new message. Deduping only the visible text also fails when she later sends the same sentence on purpose. Reusing msg-101 with different words must not silently overwrite the first note. The server has to store the id.
Step 3
One row per sender and client id
Maya's sender id plus msg-101 is the key, checked inside one immediate transaction. The same ride and the same body return the original server message id with result already_saved. A different body or a different ride for that id is a conflict, not a second row. The first insert is the only copy the driver should see in this database.
Step 4
Commit the insert before you say saved
The first success returns saved only after commit. The lookup and the insert share the transaction, so two racing retries cannot both insert msg-101. A client outbox on one simulated process can remember that id and resend the same body. That outbox does not coordinate several devices.
Step 5
Return the saved row on retry
A matching retry returns already_saved and the same message id, so this database does not grow a second copy. If storage is busy, the identical request can be tried again. I do not mint a new client id for a retry of the same note. A changed body for msg-101 stays a conflict.
Step 6
Saved once is not delivered once
This lab saves at most one row per sender and client message id. It does not guarantee exactly-once network delivery, a live push to the driver, or a read receipt. The outbox is one simulated process. I would not tell the interviewer the driver was notified exactly once.
Do not stop here
I would resend the text whenever the confirmation is lost, and drop duplicates only if the phone still remembers the last screen.
Say this boundary
I store one row for Maya and msg-101 on ride 4821, and every matching retry returns that same message id.
Leave these claims out
- Exactly-once delivery to the driver
- The phone's memory is the source of truth
- The same client id can change its text
I would send msg-101 twice in the explanation and show one saved row plus an already_saved reply. The duplicate is stopped in the database, and I do not claim the network delivered it once.
Now say the same idea in your own words, using the story from this challenge.