Bird
0
0
LLDsystem_design~7 mins

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

Choose your learning style9 modes available
Problem Statement
Without clear state management, a system controlling movement can behave unpredictably. For example, commands to move up or down might conflict or be ignored if the system doesn't track whether it is currently idle or already moving. This leads to bugs like moving in the wrong direction or failing to stop.
Solution
State management tracks the current status explicitly, such as idle, moving up, or moving down. By checking the current state before acting, the system avoids conflicting commands and ensures smooth transitions. This makes behavior predictable and easier to maintain.
Architecture
Idle
Moving Up
Moving

This diagram shows the three states: Idle, Moving Up, and Moving Down, with arrows representing allowed transitions triggered by move_up, move_down, and stop commands.

Trade-offs
✓ Pros
Prevents conflicting commands by enforcing valid state transitions.
Makes system behavior predictable and easier to debug.
Simplifies adding new states or transitions in the future.
✗ Cons
Adds complexity by requiring explicit state tracking.
Requires careful design to handle all possible transitions.
Can increase code size compared to naive implementations.
Use when a system has multiple mutually exclusive states that affect behavior, especially when commands can conflict or overlap. Suitable for systems with at least three states or more complex state logic.
Avoid if the system is extremely simple with only one or two states and no conflicting commands, as the overhead of state management may not justify the benefits.
Real World Examples
Elevator systems in buildings
Elevators track states like idle, moving up, and moving down to avoid conflicting commands and ensure safe operation.
Netflix playback controls
Netflix manages player states such as paused, playing forward, and rewinding to provide smooth user experience.
Amazon warehouse robots
Robots track movement states to avoid collisions and coordinate tasks efficiently.
Code Example
The before code moves the elevator position without tracking state, allowing conflicting moves. The after code uses a state variable to track if the elevator is idle, moving up, or moving down, preventing conflicting commands and making behavior predictable.
LLD
### Before: No explicit state management
class Elevator:
    def __init__(self):
        self.position = 0

    def move_up(self):
        # Moves up without checking current state
        self.position += 1

    def move_down(self):
        # Moves down without checking current state
        self.position -= 1

### After: With explicit state management
class Elevator:
    IDLE = 'idle'
    MOVING_UP = 'moving_up'
    MOVING_DOWN = 'moving_down'

    def __init__(self):
        self.position = 0
        self.state = Elevator.IDLE

    def move_up(self):
        if self.state == Elevator.MOVING_DOWN:
            print('Cannot move up while moving down')
            return
        self.state = Elevator.MOVING_UP
        self.position += 1

    def move_down(self):
        if self.state == Elevator.MOVING_UP:
            print('Cannot move down while moving up')
            return
        self.state = Elevator.MOVING_DOWN
        self.position -= 1

    def stop(self):
        self.state = Elevator.IDLE
OutputSuccess
Alternatives
Event-driven callbacks
Instead of explicit states, actions trigger callbacks that handle behavior dynamically.
Use when: Choose when system states are simple and event responses are independent without complex state transitions.
Flag variables
Use multiple boolean flags to represent conditions instead of a single state variable.
Use when: Choose when states are not mutually exclusive and can overlap, but complexity is low.
Summary
State management tracks the current status to avoid conflicting commands and unpredictable behavior.
It enforces valid transitions between states like idle, moving up, and moving down.
This makes systems easier to maintain, debug, and extend.