State Machines — Notes#
Interview heuristic#
When the prompt mentions a lifecycle ("order", "booking", "cabin"), draw the state diagram first, even before classes. It anchors invariants for the rest of the design.
Common bugs#
- Forgetting to handle the "rollback" path from a successful state (refund after shipped).
- Two events racing toward the same state — pin both to optimistic concurrency.
- Implicit transitions hidden in timers / TTLs — make them explicit ("idle → expired after 10 min").
Libraries#
- xstate (JS/TS) — visualisable, statecharts.
- Stateless (.NET / Java port).
- Spring StateMachine.
- Akka FSM (Scala / Java).
- Temporal / Cadence — durable workflow state machines.
Refs#
- David Harel: "Statecharts: A Visual Formalism" (1987).
- Miro Samek: Practical UML Statecharts in C/C++.
- xstate docs.
- "Designing Reactive Systems with Akka FSM" talks.