| Users | Orders per Second | State Transitions per Second | Storage Size (Order States) | Latency Requirements |
|---|---|---|---|---|
| 100 | 10 | 20 | ~10 MB | Low (seconds) |
| 10,000 | 1,000 | 2,000 | ~1 GB | Medium (sub-second) |
| 1,000,000 | 100,000 | 200,000 | ~100 GB | High (milliseconds) |
| 100,000,000 | 10,000,000 | 20,000,000 | ~10 TB | Very High (milliseconds) |
Order tracking state machine in LLD - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
The first bottleneck is the database handling state transitions. As order states update frequently, the database must handle many writes and reads per second. At around 10,000 users, the database write throughput and latency become critical because each order state change requires a write and often a read to confirm the current state.
- Database Scaling: Use write-optimized databases or NoSQL stores for fast state updates. Add read replicas to handle read-heavy queries.
- Caching: Cache current order states in memory (e.g., Redis) to reduce database reads.
- Horizontal Scaling: Add more application servers behind load balancers to handle increased state transition requests.
- Sharding: Partition orders by user ID or region to distribute database load.
- Event Sourcing: Use event logs to track state changes asynchronously, reducing direct database writes.
- CDN: Use CDN for static content but it has minimal impact on state machine scaling.
- At 10,000 users: ~1,000 orders/sec, ~2,000 state transitions/sec.
- Database must handle ~2,000 writes/sec and ~3,000 reads/sec (including queries).
- Storage: Each order state record ~1 KB, so 1 million orders ~1 GB storage.
- Network bandwidth: Assuming 1 KB per state update, 2,000 updates/sec = ~2 MB/s bandwidth.
- At 1 million users: 100,000 orders/sec, 200,000 state transitions/sec, requiring distributed databases and caching.
Start by explaining the order state machine and its transitions. Then discuss expected load and how it grows with users. Identify the database as the first bottleneck due to frequent writes. Propose caching and sharding to reduce load. Mention horizontal scaling of app servers. Always justify why each solution fits the bottleneck.
Your database handles 1,000 QPS for order state updates. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Add read replicas and implement caching to reduce direct database reads. Consider sharding the database to distribute write load. Also, horizontally scale application servers to handle increased requests.
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
