What if your orders could never get lost or stuck in the wrong step again?
Why Order tracking state machine in LLD? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you run an online store and try to track every order manually using notes or simple lists. You write down when an order is placed, packed, shipped, delivered, or canceled. But as orders grow, it becomes a mess to know exactly what stage each order is in.
Manually tracking order states is slow and confusing. You might forget to update the status, mix up steps, or miss important transitions. This causes delays, unhappy customers, and extra work fixing mistakes.
An order tracking state machine clearly defines all possible order states and allowed moves between them. It automatically controls the flow, ensuring orders only move through valid steps. This reduces errors and makes tracking simple and reliable.
if order.status == 'placed': order.status = 'shipped' # skips packing step if order.status == 'delivered': notify_customer()
state_machine.transition('placed', 'packed') state_machine.transition('packed', 'shipped') state_machine.transition('shipped', 'delivered') if state_machine.is_final('delivered'): notify_customer()
It enables smooth, error-free order progress tracking that scales effortlessly as your business grows.
Amazon uses order tracking state machines to update customers in real-time about their package status, from order confirmation to doorstep delivery.
Manual order tracking is error-prone and hard to scale.
State machines enforce valid order state transitions automatically.
This leads to reliable, clear, and scalable order tracking systems.
Practice
Order Tracking State Machine in system design?Solution
Step 1: Understand the role of state machines
A state machine models states and transitions between them based on events.Step 2: Apply to order tracking context
In order tracking, it shows order stages like placed, shipped, delivered, and controls valid moves.Final Answer:
To represent the different stages an order goes through and control transitions -> Option DQuick Check:
State machine = stages and transitions [OK]
- Confusing state machine with data storage
- Mixing order calculation with state control
- Assuming it handles user login
Solution
Step 1: Identify valid order flow transitions
Orders move forward: placed -> shipped -> delivered; backward or invalid transitions are not typical.Step 2: Check each option's direction and event
transition('placed', 'shipped', event='ship_order')correctly moves from placed to shipped on ship_order event; others reverse or skip states incorrectly.Final Answer:
transition('placed', 'shipped', event='ship_order') -> Option BQuick Check:
Valid forward transition =transition('placed', 'shipped', event='ship_order')[OK]
- Defining backward transitions without valid reason
- Skipping intermediate states
- Using wrong event names
state = 'placed'
event = 'ship_order'
if state == 'placed' and event == 'ship_order':
state = 'shipped'
elif state == 'shipped' and event == 'deliver_order':
state = 'delivered'
print(state)What will be the output if
event = 'deliver_order' when state = 'placed'?Solution
Step 1: Check condition for event 'deliver_order' when state is 'placed'
The first if checks for 'ship_order' event; it does not match 'deliver_order'. The elif checks for 'shipped' state, but current state is 'placed'.Step 2: Determine state after conditions
No condition matches, so state remains unchanged as 'placed'.Final Answer:
placed -> Option CQuick Check:
No matching transition keeps state same [OK]
- Assuming event triggers transition regardless of current state
- Confusing elif with else
- Expecting error without exception handling
state = 'shipped'
event = 'cancel_order'
if state == 'placed' and event == 'cancel_order':
state = 'cancelled'
elif state == 'shipped' and event == 'cancel_order':
print('Cannot cancel after shipping')
else:
state = 'cancelled'
print(state)Solution
Step 1: Analyze conditions for state 'shipped' and event 'cancel_order'
The if does not match. The elif matches, prints 'Cannot cancel after shipping' but leaves state unchanged. However, if event were different (e.g., 'deliver_order') with state='shipped', if and elif fail, else wrongly sets state='cancelled'.Step 2: Identify why this is a bug
The else acts as a catch-all, allowing cancellation of shipped orders for unhandled events, contradicting the intent to prevent cancellation after shipping.Final Answer:
The else block cancels order even after shipping -> Option AQuick Check:
Else wrongly cancels unhandled shipped cases [OK]
- Ignoring else block effects
- Assuming print prevents state change
- Not testing all branches
Solution
Step 1: Understand complexity of order states
Orders have normal states (placed, shipped, delivered) and exceptions (cancelled, returned) which can be grouped logically.Step 2: Evaluate design approaches for scalability and clarity
Hierarchical states allow grouping related states, reducing complexity and improving maintainability compared to flat or oversimplified models.Final Answer:
Model states hierarchically with sub-states for exceptions and normal flow -> Option AQuick Check:
Hierarchical states = scalable and clear [OK]
- Using flat states causing many transitions
- Ignoring exceptions in state machine
- Oversimplifying states losing detail
