Uploaded October 2026 | Updated November 2026, 4 days ago
Delivered. Then, a moment later, picked up again. The same order, moving backwards. How does that happen?
A food order looks like one thing, but inside it moves through many states, and every step is a chance for things to go wrong. If the status is just a text column that any service can write anything into, the order ends up in a state that can't exist. The fix is an order state machine.
Step 1, list the states: placed, confirmed, preparing, ready, picked up, delivered, plus two exits, cancelled and refunded. An order is in exactly one state at a time.
Step 2, write down the allowed transitions. Placed goes to confirmed or cancelled, ready goes to picked up, picked up goes to delivered. Anything not on the list is rejected. Delivered back to preparing? Never.
Step 3, make every transition atomic: set the status to picked up, but only if the current status is ready. The database checks and updates in a single step. Zero rows updated means someone else changed the state first, so if a cancel and an accept arrive together, only one wins.
Step 4, every transition is an event, written in the same transaction into an outbox table. It notifies the customer, updates the rider app, feeds the ETA and builds the history for support, and the state can never change while the event gets lost.
Even timers are transitions: no acceptance in five minutes (an example value) moves placed to cancelled, with a refund event. No person has to remember.
The catch: events arrive late, and sometimes twice. Apply every event blindly and a delivered order goes back to picked up. The same transition table fixes it: if a move is not allowed from the current state, ignore it. A duplicate event changes nothing, because the state is already there.
Next: in today's System Design Interview reel, designing Swiggy or Zomato. Comment and tell me: has an order of yours ever shown delivered, but never arrived? Follow for the full series.
kgptalkie.com
#shorts #SystemDesign #StateMachine #BackendEngineering #DistributedSystems #FoodDelivery #OutboxPattern #SoftwareEngineering #SystemDesignConcepts #SystemDesignInterview #Microservices #KGPTalkie
Delivered. Then, a moment later, picked up again. The same order, moving backwards. How does that happen?
A food order looks like one thing, but inside it moves through many states, and every step is a chance for things to go wrong. If the status is just a text column that any service can write anything into, the order ends up in a state that can't exist. The fix is an order state machine.
Step 1, list the states: placed, confirmed, preparing, ready, picked up, delivered, plus two exits, cancelled and refunded. An order is in exactly one state at a time.
Step 2, write down the allowed transitions. Placed goes to confirmed or cancelled, ready goes to picked up, picked up goes to delivered. Anything not on the list is rejected. Delivered back to preparing? Never.
Step 3, make every transition atomic: set the status to picked up, but only if the current status is ready. The database checks and updates in a single step. Zero rows updated means someone else changed the state first, so if a cancel and an accept arrive together, only one wins.
Step 4, every transition is an event, written in the same transaction into an outbox table. It notifies the customer, updates the rider app, feeds the ETA and builds the history for support, and the state can never change while the event gets lost.
Even timers are transitions: no acceptance in five minutes (an example value) moves placed to cancelled, with a refund event. No person has to remember.
The catch: events arrive late, and sometimes twice. Apply every event blindly and a delivered order goes back to picked up. The same transition table fixes it: if a move is not allowed from the current state, ignore it. A duplicate event changes nothing, because the state is already there.
Next: in today's System Design Interview reel, designing Swiggy or Zomato. Comment and tell me: has an order of yours ever shown delivered, but never arrived? Follow for the full series.
kgptalkie.com
#shorts #SystemDesign #StateMachine #BackendEngineering #DistributedSystems #FoodDelivery #OutboxPattern #SoftwareEngineering #SystemDesignConcepts #SystemDesignInterview #Microservices #KGPTalkie










