Everyday Programmer
Work with Jimmy
Systems

Your Save Button Needs a Concurrency Policy

Two tabs. Two valid edits. One silent overwrite. A version check can keep the second save from erasing work the editor never saw.

7 min readfrontend architecture, API design, concurrency, HTTP, testing
Two isometric editors send changes toward a shared record. A teal edit passes the validation gate, while an amber conflicting draft is retained safely in a side tray.

Two tabs. Two valid edits. One silent overwrite. A version check can keep the second save from erasing work the editor never saw.

Imagine a small notes editor. You open the same note in two tabs. Both load version 7.

In the first tab, you correct the title and save. The server stores version 8. In the second tab, you change the body and save the older form. If the endpoint replaces the note without checking its version, the old title can come back with your new body.

Both requests can return success. The application still loses an edit.

This is a hypothetical example, but it is a useful review exercise. A Save button needs an answer to a question that its disabled state cannot settle: what happens when the document changed after this form loaded?

Decide what a save is allowed to replace

Disabling Save while a request is running can prevent a double-click in one tab. Debouncing can reduce the number of autosaves. A queue can keep that tab's writes in sequence.

None of those controls coordinates a second tab, another device, or another editor.

I would start with a small contract: save this edit only if the resource still matches the version it was based on. If it has changed, preserve the draft and ask the user to resolve the difference.

HTTP already has a mechanism for expressing that condition. An API can return a strong ETag with the representation, then require the client to send it back in If-Match when saving. The server evaluates the precondition before performing the write. A failed condition normally produces 412 Precondition Failed. Weak ETags are unsuitable for this comparison. HTTP conditional requests

Here is an illustrative exchange, with an intentionally small note representation:

http

The editor retains that ETag alongside the saved snapshot. Its later request includes:

http

If another save has moved the note to version 8, the API can reject this stale replacement instead of silently accepting it. A successful save must give the editor a way to obtain the new representation and validator for the next edit.

The tag is an opaque validator, not a permission grant. Authentication, authorization and input validation still apply. If-Match: * only checks whether a current representation exists; it does not protect the particular version the user edited. An API can also reject a missing required precondition with 428 Precondition Required. That policy has to be implemented. If-Match semantics, 428 status

Make the check and the write one operation

Reading the version, comparing it in application code, then issuing an unconditional update leaves a gap. Another writer can commit between the comparison and the update.

For a simple row-backed resource, a database update can include the expected version in its predicate:

sql

This is a conceptual fragment, not a complete endpoint. The parameters must be bound safely, and the application must enforce authorization and tenant boundaries. The strong ETag must identify the selected representation, including any representation variants. A row version is sufficient only if its mapping accounts for every change to that representation. Every path that changes the protected content must advance its version too.

In PostgreSQL's Read Committed mode, an UPDATE encountering a concurrent update waits and then reevaluates its WHERE condition against the updated row. That is what makes the version predicate useful here. The content change and version increment happen together. A separate check followed by an unconditional write does not provide the same protection. PostgreSQL transaction isolation

Zero updated rows are not success. The endpoint needs deliberate handling for a stale version, a missing resource and access restrictions, without leaking information the caller should not see. Changes spanning several records may need a broader transaction design.

A conflict needs somewhere for the draft to go

A 412 response protects the stored note. The frontend still has to protect the person's work.

I would keep three things separate: the version originally loaded, the current local draft, and the latest server version fetched after the conflict.

That gives the interface useful options:

  • Compare the local draft with the latest saved version.
  • Merge the changes, then save against the version used for that merge.
  • Save a copy when the product supports it.
  • Discard the local draft and reload only after an explicit choice.

The dangerous shortcut is fetching the newest ETag and automatically resubmitting the unchanged draft. That turns a detected conflict into the overwrite the check was meant to prevent.

Conflict resolution can race too. Someone else may save while the comparison screen is open. A merged edit still needs a conditional save, and a second conflict must not erase the draft.

Keep saved and saving honest

There is also a smaller race inside one editor. A request can save snapshot A while the user continues typing snapshot B.

When A's response arrives, the interface can acknowledge A. It should not mark B as saved. Track the submitted snapshot separately from the current draft so a successful response cannot clear unsaved changes that were never part of that request.

Cancellation needs equally careful wording. Aborting a browser request does not establish that the server abandoned the write. React Router's concurrency guidance calls out this boundary explicitly: canceled requests can still be processed server-side. A failed connection may leave the outcome unknown. Browser cancellation and server races

Keep the draft while reconciling. If the API exposes operation status, check it. Otherwise, retrieve current state and follow the application's recovery policy. Do not automatically tell the user that nothing was saved.

Idempotency helps with a different question: recognizing a retry of the same operation. Stripe's API, for example, documents idempotency keys for safely retrying requests. Two different edits with two different keys can still be based on the same stale version. A retry policy does not replace a concurrency policy. Idempotent requests

Test the collision deliberately

The happy-path test proves that one editor can save. Add tests that control the contested order:

  1. Both clients read version 7. A saves. B attempts its different edit against version 7. Assert that B gets the defined conflict response and A's content remains stored.
  2. Release two real backend writes with the same expected version together. Assert that exactly one changes the row. Repeat without assuming which request wins.
  3. Hold a save response, type another edit, then release the response. The newer draft must remain unsaved.
  4. Commit the write but drop its response. The UI should preserve the draft and reconcile an uncertain outcome.
  5. Edit the resource again while the conflict screen is open. A stale merge must encounter the same protection.

Controlled network mocks can exercise the interface paths. They cannot prove the database update is atomic; that needs an integration test against the real write path. Playwright's request interception tools are useful for controlling frontend response timing without relying on arbitrary sleeps. Network testing

The review question is straightforward: if someone saves a form based on an older version, whose work survives, and how does the interface explain what happened?

Answer that before polishing the Saved message.

Keep reading

Related articles