System Design · core

State Machines

States, transitions, guards.

system-designlow-level-design

Mental model

A state machine is a fixed set of states plus the rules for moving between them. The win is that illegal transitions become impossible by design — if you find yourself writing many "if status == X and Y" checks, you are simulating a state machine the hard way.

How to study State Machines

Begin by restating the mental model in your own words, then connect it to a concrete system you have built or operated. Name the mechanism, the constraint it addresses, and the trade-off it introduces. Use Refactoring Guru — State, Welcome to the world of Statecharts, Statecharts: A Visual Formalism for Complex Systems (Harel, 1987) to check details, but close the source before writing your explanation. Retrieval is the learning step; rereading is only preparation.

Next, compare State Machines with the neighboring concepts in its roadmap. Ask what changes in correctness, latency, resource use, operability, and failure recovery. Complete Design a vending machine and preserve the command, input, output, and one failed attempt as evidence. Finish by explaining the idea without jargon to someone who has not studied the track.

Proof of understanding

  • Explain the mechanism from first principles and identify the state it reads or changes.
  • Give one situation where the concept is the right choice and one where it is not.
  • Predict a realistic failure mode before running the drill, then compare the prediction with evidence.
  • Connect the result to a roadmap or build artifact instead of treating the concept as isolated trivia.

Learn from primary sources

Practice and explain it back

Design a vending machine

Draw the state machine for a vending machine: Idle → Coin → Selecting → Dispense → Idle, plus refund / out-of-stock branches. List every transition trigger.

Expected evidence: A state diagram with 5-7 states, transitions labeled by input event.

Open the interactive drill →

Review prompts

  • Your code is full of `if (paid && !refunded && shippedAt == null)`. What does converting to a state machine actually buy you?

Build evidence

Use a roadmap capstone to turn this concept into working evidence.

Prerequisites

Related concepts

None assigned yet.

Learning paths