State Machines: Why the Same Request Can Get a Different Answer
A state machine describes a system through defined states and rules for moving between them. A state is its current condition, such as signed out or signed in. A transition is a permitted change from one condition to another.
The current state matters because an identical request can have a different meaning at a different point. Asking to open a private page before signing in shouldn't be treated like asking after the required identity check has succeeded.
An event is something the machine responds to, such as receiving a request. A condition can decide whether a transition is allowed. The model can also specify an action to perform as part of the change.
What Is a Turing Machine?
A small login model
Consider this small login design:
| Current state | Event and condition | Result |
|---|---|---|
| Signed out | A user submits sign-in details | Start checking; move to Checking |
| Checking | The identity check succeeds | Start a session; move to Signed in |
| Checking | The identity check fails | Refuse the sign-in; return to Signed out |
| Signed in | The user signs out | End the session; move to Signed out |
A session is the application's maintained signed-in interaction with the user. The table describes a few steps in a login, leaving other requests for the designer to consider.
What should happen to a request while the application is still Checking? The developer has to define that behavior rather than assume every incoming request arrives at a convenient time.
The same is true of failure. A model that shows only the successful path leaves unanswered what happens after a rejected sign-in. Naming the states helps expose those missing decisions.
The World Wide Web Consortium's state-machine specification describes states, event-driven transitions, conditions and actions. Its notation is one way to express these ideas.
Network connections have stages too
Transmission Control Protocol, or TCP, supplies rules for communication between programs. Its specification includes a state machine with named conditions such as LISTEN, SYN-SENT and ESTABLISHED.
- Make LEDs blink and sensors report in an afternoon with more than 22 guided lessons.
- Start with no soldering: everything plugs into the breadboard.
- See how code controls real hardware, the first step toward understanding any device.
LISTEN describes waiting for connection requests. SYN-SENT describes having sent an initial connection request. ESTABLISHED describes the connection after setup has succeeded. Incoming messages and actions by the local program drive the permitted changes between these and other states.
The TCP specification uses these distinctions to define how communication proceeds. A message isn't interpreted solely by its contents; the present connection state also matters.
A simple state machine has one active state at a time. More elaborate models can include states within states or track several parts of a system at once, each with its own active state.
The diagram remains a description of intended behavior. The software must actually enforce the transitions and conditions it shows. Drawing a missing identity check into the table is useful only if the implementation performs the check before moving to Signed in.
Stimulus: The Input That Gives a System Something to Respond To

Synchronization: Getting Clocks—and Work—in Agreement

TCP's Three-Way Handshake: What Those Messages Do

QUOTE:
"Amateurs hack systems, professionals hack people."
