CoursesBackend engineering from first request to production

Puzzle 1 · 2 of 18

Did the first write happen?

Make a lost reply safe to retry.

All puzzles in this course

The problem

The save route accepts a bookmark name. Your app times out after tapping Save. The phone cannot tell whether the server wrote the row just before the reply disappeared. What must stay the same on retry?

See it

Did the first write happen? Write river Commit Reply lost Retry river

Scroll sideways to see the full diagram.

  1. The phone chooses one stable name.

    Step 1 of 4
  2. The database commits a single row.

    Step 2 of 4
  3. The phone never sees the reply.

    Step 3 of 4
  4. The second write finds the same key.

    Step 4 of 4

The building block

The database stores information in a table. Think of each row as one bookmark and each column as one property. SQL is a language for reading and changing these tables. A unique key lets the database enforce one row per name, even when two callers write together. Use river as the identity of the desired bookmark. The database accepts at most one row for that name. The insert asks it to ignore a conflicting key. The handler waits for the transaction to commit before replying. The first insertion returns 201. Repeating the same write returns 200 and leaves one row. Checking for the row in application code and inserting later leaves a gap where two callers can both see it missing.

Lose the reply after a successful save

Ada sends a write request for river. The database commits one row, but the connection closes before Ada sees the reply. Her phone knows only that the outcome is unknown. On the second attempt the database checks the same key and finds river already there. The reply changes from a lost 201 to a visible 200, yet the durable result is still one bookmark. This property is called idempotence: repeating the intended operation preserves its intended effect. Starting a new operation called hill is a different intention.

Try it

Run the browser experiment

Optional local lab

Run both writes. Predict the list after a third write with a new name. Check it and explain which component enforces uniqueness.

Open the local service source files

Check yourself

What if the retry invents a fresh name?

Right. A stable bookmark name and a unique database key let repeated writes converge on one row.

Not quite. The key identifies the name supplied by the client. Re-read that part ↑

Does a client timeout tell you whether the insert committed?

Not quite. Losing the reply and losing the write are different events. Re-read that part ↑

Right. Losing the reply and losing the write are different events.

What evidence demonstrates a successful replay?

Right. The intended effect is the stored bookmark.

Not quite. The intended effect is the stored bookmark. Re-read that part ↑

Sources

What you now know

A stable bookmark name and a unique database key let repeated writes converge on one row.