0
0
LLDsystem_design~25 mins

Order tracking state machine in LLD - System Design Exercise

Choose your learning style9 modes available
Design: Order Tracking State Machine
Design the state machine logic, API interface, and data model for order states. Out of scope: payment processing, inventory management.
Functional Requirements
FR1: Track the state of an order from placement to delivery
FR2: Support states: Created, Confirmed, Packed, Shipped, Out for Delivery, Delivered, Cancelled
FR3: Allow valid transitions only (e.g., cannot ship before packing)
FR4: Notify users on state changes
FR5: Handle concurrent updates safely
FR6: Provide API to query current order state
FR7: Support at least 10,000 orders concurrently
Non-Functional Requirements
NFR1: API response latency p99 < 100ms
NFR2: System availability 99.9%
NFR3: State transitions must be consistent and atomic
NFR4: Scalable to handle peak loads during sales events
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
❓ Question 6
Key Components
State machine engine or library
Order state database
API server for client interaction
Notification service
Concurrency control mechanism
Design Patterns
State machine pattern
Event sourcing for state changes
Observer pattern for notifications
Optimistic locking for concurrency
RESTful API design
Reference Architecture
API Server
Notification Service
Components
API Server
Node.js/Express or Python/Flask
Expose REST API endpoints for querying and updating order states
State Machine Logic
Custom code or state machine library
Enforce valid state transitions and business rules
Order State Database
Relational DB (PostgreSQL) or NoSQL (DynamoDB)
Persist order states and transition history
Notification Service
Message queue (RabbitMQ) + Email/SMS gateway
Send asynchronous notifications on state changes
Concurrency Control
Optimistic locking or transactions
Prevent conflicting state updates
Request Flow
1. Client sends API request to update order state
2. API Server receives request and forwards to State Machine Logic
3. State Machine Logic validates requested transition against current state
4. If valid, State Machine Logic updates Order State Database atomically
5. State Machine Logic triggers Notification Service asynchronously
6. API Server responds with updated order state
7. Client can query current order state via API
Database Schema
Entities: - Order: order_id (PK), current_state, user_id, timestamps - OrderStateHistory: id (PK), order_id (FK), previous_state, new_state, changed_at Relationships: - One Order has many OrderStateHistory records - current_state in Order reflects latest state - State transitions validated before updating current_state
Scaling Discussion
Bottlenecks
Database write contention on popular orders during peak times
Notification service overload with many state changes
API server CPU/memory limits under high concurrent requests
State machine logic becoming a single point of failure
Solutions
Use database sharding or partitioning by order_id to distribute load
Implement batching and rate limiting in Notification Service
Deploy API servers behind load balancer with autoscaling
Design State Machine Logic as stateless microservice instances with shared DB
Interview Tips
Time: 10 min for requirements and clarifications, 15 min for architecture and data model, 10 min for scaling and trade-offs, 10 min for Q&A
Clearly define states and allowed transitions
Explain concurrency control to maintain consistency
Discuss asynchronous notifications for better performance
Justify technology choices based on requirements
Highlight scalability strategies for peak loads