Command Palette

Search for a command to run...

256kb

Optimistic UI with rollback

A mutation pattern that updates the interface immediately, then keeps or reverts that state based on the server response.

2026-04-25 · intermediate · React, TypeScript

Problem

Some interactions should feel instant even when the server has not confirmed the change yet. A slow mutation can make a good product feel broken, especially when the expected result is obvious to the user.

Optimistic UI is the pattern of applying the expected state first, then reconciling with the server response. The hard part is not the optimistic update. The hard part is the rollback path.

Demo

Force next mutation to fail

Keep this on to see the rollback path.

Validate input contractshipped
Render preview statequeued
Persist server mutationqueued

Toggle failure and ship a queued task.

Why it works

The demo captures the previous state before making an optimistic change. If the server accepts the mutation, the optimistic state becomes the real state. If the server rejects it, the UI restores the captured previous state and explains what happened.

That previous-state snapshot is the important part. Without it, a failed request can leave the interface halfway between what the user expected and what the system actually saved.

Implementation notes

Use optimistic updates for actions where the user's intent is clear and the failure rate is low: starring, liking, archiving, marking complete, reordering, or small preference changes.

Be more careful with destructive actions, money movement, permissions, and anything where a failed mutation has serious consequences. Those flows need stronger confirmation and clearer server acknowledgement.

Checklist

Capture previous state

Save enough information to restore the UI exactly if the request fails.

Apply the expected state

Update the interface immediately so the product feels responsive.

Reconcile with the server

Keep the state on success. Roll back, retry, or show a targeted error on failure.

Preserve user trust

Make rejected mutations visible. Silent rollbacks feel like bugs.