Beginner · About 10 minutes · Updates arriving out of order
Your Order Status Went Backward
Shipped arrived before Packed. How would you stop a delayed update from replacing the newer status?
Answers stay in this browser until you save them on this device.
What happened?
Maya orders a pair of shoes. Her order number is 4821.
The warehouse sends “Packed,” update number 2. Later, it sends “Shipped,” update number 3.
A delay causes update 3 to arrive first. The store shows Shipped.
Update 2 arrives afterward. The store blindly uses the last message it received, so Maya's order goes back to Packed.
1. Update 3 arrives first
Update #3
Shipped
Store shows Shipped.
2. Update 2 arrives late
Update #2
Packed
Delayed message
Without a version check…
3. Status goes backward
Order #4821
Packed
Wrong: older update replaced newer
Illustrated example
Static scenarios—not a live queue or backend
Incoming message
Update 2 · Packed
Previously saved
Update 3 · Shipped
Skip older — keep Shipped at update 3. The warehouse number decides, not arrival order.
Your task
How would you keep an older update from replacing the latest saved status?
Visible assumptions
- One warehouse system assigns increasing update numbers for each order before sending messages.
- Each message contains the complete current status, not a change that must be added to another value.
- Updates can arrive late or more than once.
- For this exercise, the goal is to show the latest received version of an order's status.
- You can explain the solution in plain English. Code is optional.
Related path: System Design Interview Sprint
Interview answer
Explain it in an interview
For Maya's order 4821, Shipped arrived before Packed and the status went backward. How do you stop a late update from winning?
Maya ordered shoes as order 4821. Update 3 said Shipped and arrived first. Delayed update 2 said Packed and arrived afterward. The store applied whichever message came last, so her status moved from Shipped back to Packed.
Step 1
A late Packed update rewrote Shipped
Update 3 was already the newest snapshot for order 4821, and it said Shipped. Update 2 was older warehouse news that showed up late and said Packed. Last-arrival wins stored Packed as current even though the warehouse did not walk the order backward. The database followed the wrong clock.
Step 2
Status words are the wrong order
Ranking Packed below Shipped looks fine until a higher update number corrects an earlier word. Arrival time is also wrong, because the network delayed update 2. I would not invent a new update number to force the late message in. The number from the trusted source is the order.
Step 3
Apply only a higher update number
One trusted warehouse source assigns strictly increasing positive update numbers for each order. Every message is a full status snapshot, not a command to nudge the previous row. I apply it only when its number is higher than the number already saved. Gaps are allowed, so an unseen order can start at update 3.
Step 4
Compare and write in one transaction
The update statement changes the row only for a higher number, inside one immediate transaction, so the write and the outcome stay together. Accepted calls come back as applied, ignored_older, or duplicate. Each call uses its own connection. I do not depend on a process-wide lock or a cache.
Step 5
Replay the same snapshot on retry
A retry of the identical snapshot is safe for this local state and comes back duplicate or ignored_older. The same current number with different contents is a conflict, and I investigate the source instead of minting a number. A lock-wait failure means I retry that same message after a delay. Other errors do not prove the commit failed.
Step 6
Do not claim consensus or side effects
SQLite serializes writers to this one local file, and that is not multi-region consensus. The response snapshot can be overtaken by a later request as soon as this one commits. No email or payment is triggered by the status write. A live screen should also reject older versions, and this lab does not build that screen.
Do not stop here
I would store whichever status message arrives last, or rank Packed below Shipped so the word itself decides.
Say this boundary
I keep update 3 for order 4821 and ignore update 2, because 2 is not a higher update number.
Leave these claims out
- Status words have a fixed ranking
- This database is multi-region consensus
- Any error proves the earlier write rolled back
I would say order 4821 stays Shipped because update numbers, not arrival, decide the row. The conflict path is how I refuse to paper over two different snapshots with the same number.
Now say the same idea in your own words, using the story from this challenge.