0
0
LLDsystem_design~25 mins

Order state machine in LLD - System Design Exercise

Choose your learning style9 modes available
Design: Order State Machine
Design focuses on the order state machine logic, APIs for state transitions and queries, concurrency control, and audit logging. Does not cover payment processing, shipping logistics, or UI design.
Functional Requirements
FR1: Support order lifecycle states: Created, Paid, Shipped, Delivered, Cancelled, Returned
FR2: Allow valid state transitions only (e.g., Created -> Paid, Paid -> Shipped)
FR3: Reject invalid transitions (e.g., Delivered -> Paid)
FR4: Provide ability to query current state of an order
FR5: Support concurrent updates safely to prevent invalid state changes
FR6: Log state changes for audit and debugging
FR7: Handle up to 1000 state transitions per second
Non-Functional Requirements
NFR1: Latency for state transition API should be under 100ms p99
NFR2: Availability target 99.9% uptime
NFR3: System must be scalable to handle 10,000 concurrent orders
NFR4: State transitions must be consistent and atomic
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
❓ Question 6
Key Components
State machine engine enforcing transitions
Order state database
API layer for state change requests and queries
Concurrency control mechanism (e.g., optimistic locking)
Audit log storage
Cache for fast state queries
Design Patterns
State machine pattern
Event sourcing for audit logs
Optimistic concurrency control
Command Query Responsibility Segregation (CQRS)
Retry and idempotency for state change requests
Reference Architecture
          +----------------+
          |  Client/API    |
          +-------+--------+
                  |
                  v
          +-------+--------+          +----------------+
          | State Machine  | <------> | Order Database |
          | Engine        |          +----------------+
          +-------+--------+
                  |
                  v
          +-------+--------+
          | Audit Log Store|
          +----------------+
Components
State Machine Engine
Custom logic module
Enforces valid order state transitions and handles concurrency
Order Database
Relational DB (e.g., PostgreSQL)
Stores current order states and metadata
API Layer
RESTful API server
Exposes endpoints for clients to request state changes and query order state
Audit Log Store
Append-only log storage (e.g., Kafka or DB table)
Records all state changes for audit and debugging
Cache
In-memory cache (e.g., Redis)
Speeds up frequent order state queries
Request Flow
1. Client sends state transition request to API layer
2. API layer validates request format and authentication
3. API layer calls State Machine Engine to process transition
4. State Machine Engine checks current state from Order Database
5. State Machine Engine verifies if requested transition is valid
6. If valid, State Machine Engine updates Order Database atomically with new state
7. State Machine Engine writes state change event to Audit Log Store
8. API layer returns success or failure response to client
9. For state queries, API layer first checks Cache, then falls back to Order Database
Database Schema
Entities: - Order: order_id (PK), current_state, updated_at - StateTransitionLog: log_id (PK), order_id (FK), from_state, to_state, timestamp Relationships: - One Order has many StateTransitionLog entries - current_state in Order reflects latest valid state
Scaling Discussion
Bottlenecks
Database write contention on orders with frequent state changes
Audit log storage growing large and slowing writes
API layer becoming overloaded with high request volume
Cache consistency with database state
Solutions
Use optimistic locking or version numbers to handle concurrent updates safely
Partition audit logs by order or time to scale storage and queries
Deploy API layer behind load balancer with horizontal scaling
Use cache invalidation or write-through caching to keep cache consistent
Interview Tips
Time: Spend 10 minutes clarifying requirements and constraints, 20 minutes designing components and data flow, 10 minutes discussing scaling and trade-offs, 5 minutes summarizing
Clearly define valid states and transitions
Explain concurrency control to prevent invalid transitions
Describe how audit logging supports debugging and compliance
Discuss trade-offs between consistency and availability
Show understanding of scaling bottlenecks and mitigation