← Lessons

quiz vs the machine

Platinum1820

Frontend

Optimistic Concurrency in UIs

Update the UI immediately, then reconcile or roll back when the server answers.

6 min read · advanced · beat Platinum to climb

Hide the latency

A naive UI disables a button, waits for the server, then updates. That feels sluggish. An optimistic update assumes the request will succeed and updates the interface immediately, before the server confirms.

  • Apply the predicted result to local state right away.
  • Send the request in the background.
  • On success, reconcile with the authoritative server response.
  • On failure, roll back to the previous state and surface an error.

Handling conflicts

Optimism is risky when two clients edit the same data. The server may reject a stale write. Two common guards.

  • Version tokens: each record carries a version or timestamp. The server rejects a write whose base version is outdated, signaling a conflict to resolve.
  • Idempotency keys: retries carry a key so a duplicated request is not applied twice.

React offers useOptimistic to render a temporary optimistic value that automatically reverts if the underlying action fails.

Optimistic concurrency buys responsiveness by trusting success and preparing to undo.

Key idea

Optimistic updates show the expected result instantly, then reconcile or roll back when the server replies, using version tokens to catch conflicts.

Check yourself

Answer to earn rating on the learn ladder.

1. What is an optimistic update?

2. How do version tokens help with concurrency?

3. What does the React useOptimistic hook provide?