Lesson 12 of 14
❮Prev Next❯

MSD-101 · Deep dives: hard problems, solved

The trip state machine

⏱ 12 min 🎮 Hands-on ☁ Azure

12of 14

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    ⚡ The 30-second answer

    A trip is a state machine: REQUESTED → MATCHED → ARRIVED → ON_TRIP → COMPLETED → PAID, or CANCELLED. One service owns it, rejects invalid moves, writes with optimistic concurrency (Cosmos DB ETag + If-Match, so two writers cannot both win), and publishes changes from the change feed, so an update is never saved without being announced.

    • States and allowed moves
    • ETag and 412 retries
    • Events from the change feed
    • Apps follow the server

    Try it

    Play with the design before you read about it. Everything runs on this page.

    No Azure subscription needed. Read the requirement, see the architecture, build it in a mock portal, send a booking through it, then break it.

    ☁ Simulate on Azure: The trip state machine

    The driver taps Start trip at the same second Priya taps Cancel. Her phone is in a lift, and the trip service restarts during a deployment. The trip must end in one clear state.

    It must

    • A trip moves REQUESTED → MATCHED → ARRIVED → ON_TRIP → COMPLETED → PAID, or CANCELLED
    • Only allowed moves are accepted
    • Every change reaches the rider and the driver
    • Apps always show the server's state

    With these numbers

    Conflicting writes
    0 lost
    Update reaches app
    < 2 s
    Lost events
    0
    Audit
    Every change kept

    🎓 Education-only simulation. Nothing is created in Azure and no subscription is needed.

    1. The problemThe driver taps Start trip at the same second the rider taps Cancel. The database ends up saying ON_TRIP, the driver drives off, and the rider is charged for a trip she cancelled.
    2. The design ideaOne owner, allowed moves only, If-Match on every write.
    3. In real lifeOrder, booking and delivery systems all use the same idea. Teams that draw the state diagram first find most edge cases (“can you cancel after pickup?”) before writing code.

    Start with a question

    The driver taps Start trip at the same second the rider taps Cancel. The database ends up saying ON_TRIP, the driver drives off, and the rider is charged for a trip she cancelled.

    Two people, two phones, one trip. Without rules, the last write wins, and it is often the wrong one.

    Explain it like I'm 10

    A board game has rules: you cannot jump from start to finish. And if two players move the same piece at once, the referee checks who moved first and says “try again” to the other.

    The technical explanation

    States and moves. Write the allowed moves down: MATCHED only from REQUESTED, ON_TRIP only from ARRIVED, CANCELLED only before ON_TRIP (with a fee rule after ARRIVED). The trip service rejects anything else.

    Concurrency. Read the trip and its _etag from Azure Cosmos DB, decide, then replace with If-Match: <etag>. If someone changed it first, Cosmos DB returns 412 Precondition Failed; re-read and decide again on fresh data.

    Events. Do not publish events from request code: a crash after saving loses them. An Azure Functions Cosmos DB trigger reads the change feed and publishes every saved change to Web PubSub and push.

    Apps. Phones show whatever the server says. After every reconnect or app open they re-read the trip with GET /trips/{id}, because socket messages missed while offline are gone.

    FromAllowed to
    REQUESTEDMATCHED, CANCELLED
    MATCHEDARRIVED, CANCELLED, REQUESTED (driver declined)
    ARRIVEDON_TRIP, CANCELLED (with fee)
    ON_TRIPCOMPLETED
    COMPLETEDPAID

    ☁ Simulate on Azure🧩 Patterns

    Real-world scenario

    Order, booking and delivery systems all use the same idea. Teams that draw the state diagram first find most edge cases (“can you cancel after pickup?”) before writing code.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. Two updates to the same trip arrive together. What stops the wrong one from silently winning?
    2. 2. Where should trip events be published from?
    3. 3. A driver sends COMPLETED while the trip is ARRIVED. What should happen?
    4. 4. The rider app was offline for a minute. What must it do on reconnect?
    5. 5. What HTTP status does Cosmos DB return for a stale If-Match?

    Pass the quiz to complete this lesson.

    Next: Push notifications that arrive →

    Questions people ask

    What is a state machine in app design?

    A list of states (like REQUESTED or ON_TRIP) and the allowed moves between them. The server accepts only allowed moves, so the data can never end up in an impossible state.

    What is optimistic concurrency?

    Read a version number with the data, and when you write, say “only if the version is still X”. If someone changed it first, the write fails and you try again with fresh data.

    Is a state machine overkill for a small app?

    No. Even a small app has races like cancel versus start. Writing the allowed moves down takes an hour and prevents wrong charges and stuck trips, which cost far more to fix later.

    Interview questions

    How this topic comes up in system design interviews. Answer out loud first, then open the model answer.

    intermediate Rider cancels while the driver starts the trip. What happens?

    Both commands read version 7; one write succeeds and moves to version 8; the other gets 412, re-reads, sees the new state and applies the rule (e.g. cancel no longer allowed after ON_TRIP, or start not allowed after CANCELLED).

    advanced How do you avoid losing notifications when the service crashes?

    Treat the database as the source of events: a change-feed processor publishes every committed change (the outbox idea), with at-least-once delivery and idempotent consumers.

    Quick revision

    Remember

    • States and allowed moves
    • ETag and 412 retries
    • Events from the change feed
    • Apps follow the server
    • Store every transition as an event with who, when and from which state. Support teams answer “why was I charged?” from that history.

    One line to remember

    “One owner, allowed moves only, If-Match on every write.”

    Official documentation