Skip to content

Workflows as state machines

How to describe an order, a payment or a booking as a list of states and a table of legal moves, so that a wrong or repeated message cannot put it in a wrong state.

  • Lesson 66 of 125
  • 12 min read
  • Core
  • Free to read, no account
On this page
  1. Why interviewers ask this
  2. The idea in plain words
  3. Pause and predict
  4. Worked example
  5. Trade-offs
  6. Common misconceptions
  7. How to say it in the interview
  8. Check your understanding
  9. Answers
  10. Sources

What you need first

This lesson uses ideas from these earlier lessons. Read them first if any idea is new to you.

01Why interviewers ask this

Orders, payments and bookings move through steps. The interviewer asks: "What stages can an order have? How do you stop a cancelled order from being shipped?"

A weak answer keeps a status text that any code can change. A strong answer lists the stages (called states) and a table of allowed changes, and checks every change against it. At SDE-1 level, list the states. At SDE-2 level, enforce the table. At SDE-3 level, handle repeated and late messages.

02The idea in plain words

A parcel can be delivered only after it is packed. It cannot go from "delivered" back to "packed".

A state is the stage a thing is in now, such as PLACED. A move (also called a transition) changes the state, such as PLACED to ACCEPTED. A legal move is a move the rules allow. A final state has no legal move out, such as DELIVERED or CANCELLED. A state machine is the list of states plus the table of legal moves. At any time the thing has exactly one state.

Examples:

  • Order: PLACED, ACCEPTED, PREPARING, READY, PICKED_UP, DELIVERED, and CANCELLED.
  • Payment: CREATED to PENDING, then SUCCEEDED or FAILED. SUCCEEDED can move to REFUNDED.
  • Booking: HELD, then CONFIRMED or EXPIRED. CONFIRMED can move to CANCELLED.

Three rules make it work:

  1. One table, one place. The table lists each legal move and who may make it (the customer, the restaurant, the system). All code asks the table.
  2. The check and the change are one statement. Use a conditional update (lesson 22), which changes the row only if it still has the old state: UPDATE orders SET status = 'ACCEPTED' WHERE id = 7 AND status = 'PLACED'. The database reports 1 row changed, or 0 rows. Two requests at the same time cannot both pass.
  3. When 0 rows change, read the state. If the order is already in the state you wanted, this is a repeat. Return the current state as a success, so the move is idempotent (lesson 9). If not, the move is illegal. Return 409 Conflict (lesson 9).

Save each change as a row of history: old state, new state, time and who.

Stuck on something? The AI tutor has read this lesson. Ask it with a free account

03Pause and predict

Pause. Guess first.

An order is DELIVERED. A late message arrives that says "cancel this order". Should the system accept it?

Write your guess before you read on. The answer is in the worked example.

04Worked example

A food app. Assumptions (made up to teach with): 50,000 orders per day, and 0.2% of messages arrive late or twice.

The table has 9 legal moves:

FromToWho
PLACEDACCEPTEDrestaurant
ACCEPTEDPREPARINGrestaurant
PREPARINGREADYrestaurant
READYPICKED_UPdelivery partner
PICKED_UPDELIVEREDdelivery partner
PLACED, ACCEPTED, PREPARING or READYCANCELLEDcustomer or restaurant

The last row is 4 moves, so the table has 5 + 4 = 9 legal moves. With 7 states there are 7 x 6 = 42 pairs of different states. The other 42 - 9 = 33 pairs are illegal, and the table rejects all of them.

The late cancel. The order is DELIVERED, which is a final state. No row in the table starts at DELIVERED, so the cancel is illegal. The cancel update only matches orders in PLACED, ACCEPTED, PREPARING or READY, so it changes 0 rows. The order is not CANCELLED, so the system returns 409. This answers the predict box: no.

The size of the problem. 0.2% x 50,000 = 100 messages per day. Without the table, a late "delivered" message would mark a cancelled order as delivered, after its Rs 400 refund. If all 100 were like this, 100 x Rs 400 = Rs 40,000 would be refunded on delivered orders.

A repeat. The restaurant taps "accept" twice. The second update changes 0 rows. The state is already ACCEPTED, so the system returns success.

05Trade-offs

It solves wrong and repeated moves, and keeps the rules in one place. It costs a table that must change when the business changes. I would split the machine when states multiply. One status for payment (3 states) and delivery (4 states) needs 3 x 4 = 12 states. Two machines need 3 + 4 = 7. The cost of two machines is a rule that links them, such as "do not ship before payment".

06Common misconceptions

"The status is a text field, so any code can set it." Then one bug can set DELIVERED on a cancelled order. Only the table should decide.

"A repeated message must return an error." The sender may have lost our answer. Return the current state as success. Reject only a move that is not legal.

07How to say it in the interview

Say it like this

I would model the order as a state machine: a list of states and a table of legal moves, with who may make each move. Each change is one conditional update, so a late or repeated message cannot put the order in a wrong state.

08Check your understanding

  1. What is a final state? Name two final states of a booking that is HELD, CONFIRMED, EXPIRED or CANCELLED.
  2. A booking machine has 4 states and 3 legal moves. How many pairs of different states are there, and how many are illegal? If 20,000 bookings arrive in a day and 0.5% of messages are late, how many late messages arrive?
  3. A customer taps "Cancel" twice. The first cancel worked. What should the second tap return, and why?

09Answers

Show the answers
  1. No legal move leaves a final state. EXPIRED and CANCELLED.
  2. 4 x 3 = 12 pairs, so 12 - 3 = 9 are illegal. Late messages: 0.5% x 20,000 = 100.
  3. The order is already CANCELLED. The update changes 0 rows, and the state is the one the customer wanted, so return the current state as success. Do not refund twice.

10Sources

  • Status codes 409 and 200: RFC 9110, https://www.rfc-editor.org/rfc/rfc9110.html (opens in a new tab) (checked 20 Oct 2026).
  • The state names and the cancel rule follow the Tradeoff machine coding problem "Food Delivery Orders".
  • All order counts, the late-message rate and the refund amount are assumptions made for teaching.

Try it now, no sign-up

More practice, in the app (free account)

Ask the AI tutor about this lesson

The tutor has read this lesson. Ask in your own words, any time. Free account, 20 questions a day.

Reading never needs an account. The account is for the tutor, the quiz, your notes and your progress.

All lessons in Module 11: Workflows, money and bookings