Bird
0
0
LLDsystem_design~25 mins

State management (idle, moving up, moving down) in LLD - System Design Exercise

Choose your learning style9 modes available
Design: Elevator State Management System
Design focuses on the state management logic and transitions for a single elevator. Hardware control and multi-elevator coordination are out of scope.
Functional Requirements
FR1: Track elevator states: idle, moving up, moving down
FR2: Allow state transitions based on commands and current state
FR3: Handle requests to move elevator up or down
FR4: Prevent invalid state transitions (e.g., moving up when already moving up)
FR5: Provide current state information on demand
Non-Functional Requirements
NFR1: System should respond to state change requests within 100ms
NFR2: Support up to 100 concurrent elevator state requests
NFR3: Ensure state consistency at all times
NFR4: System availability target: 99.9% uptime
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
State machine to manage elevator states
Command handler to process move requests
State storage to keep current state
Event logger for state transitions
API or interface to query current state
Design Patterns
Finite State Machine (FSM) pattern
Observer pattern for state change notifications
Command pattern for encapsulating requests
Singleton pattern for centralized state management
Reference Architecture
 +---------------------+
 | Elevator Controller |
 +----------+----------+
            |
            v
 +---------------------+
 |   State Machine      |
 | (Idle, Moving Up,    |
 |  Moving Down)        |
 +----------+----------+
            |
            v
 +---------------------+
 | State Storage       |
 +---------------------+
            |
            v
 +---------------------+
 | Event Logger        |
 +---------------------+
Components
Elevator Controller
Custom logic module
Receives commands and interacts with the state machine
State Machine
Finite State Machine implementation
Manages elevator states and valid transitions
State Storage
In-memory store or lightweight database
Stores current elevator state persistently
Event Logger
Logging system
Records state changes for auditing and debugging
Request Flow
1. User sends a move up or move down command to Elevator Controller.
2. Elevator Controller forwards the command to the State Machine.
3. State Machine checks current state and validates transition.
4. If valid, State Machine updates the state to moving up or moving down.
5. State Storage is updated with the new state.
6. Event Logger records the state transition event.
7. Elevator Controller confirms the state change back to the user.
8. When movement completes, a command to set state to idle is processed similarly.
Database Schema
Entity: ElevatorState - id (primary key) - current_state (enum: idle, moving_up, moving_down) - last_updated_timestamp No complex relationships needed as this is a single entity tracking current state.
Scaling Discussion
Bottlenecks
High frequency of state change requests causing race conditions
State storage becoming a single point of failure
Event Logger performance degradation with large logs
Handling multiple elevators with isolated states
Solutions
Implement locking or atomic operations in state machine to prevent race conditions
Use distributed cache or replicated storage for state persistence
Archive old logs and use asynchronous logging to improve performance
Extend design to support multiple elevator instances with separate state machines
Interview Tips
Time: Spend 10 minutes understanding requirements and clarifying assumptions, 20 minutes designing the state machine and components, 10 minutes discussing scaling and edge cases, 5 minutes summarizing.
Explain the importance of clear state definitions and valid transitions
Describe how the finite state machine ensures consistency
Discuss how to handle concurrent requests safely
Mention persistence and logging for reliability and debugging
Outline scaling strategies for multiple elevators or high request volumes