Optimistic UI and the rollback you must design
Tier 0's numbers said a round trip from Mumbai to Virginia is about 180 ms, and no code makes light faster. Add TLS, server time, the return leg, and a mobile radio that has to wake up before any of it starts. A UI that waits for the server before it changes anything will feel broken on a real network no matter how good the backend is — and the backend team will keep optimising a p99 that was never the problem.
So: apply the change locally, render it immediately, and send it in the background. The user perceives zero latency because, locally, there was none.
Three states, and the bug of having two
Most implementations model done and not done. The correct model has three, and the missing one is where the difficulty lives:
- Pending — applied locally, not yet acknowledged. The user should be able to tell, subtly.
- Confirmed — the server agreed.
- Failed — the server refused, and something must now happen that the user understands.
Collapse pending into confirmed and you have no way to show what is in flight, no way to reason about what depends on what, and no honest way to report a failure — the change simply appears, and then later simply disappears.
Optimism requires predictability
The precondition is that you can predict the server's answer. Where you can, be optimistic. Where you cannot, do not — and the boundary is sharper than it looks:
- Safe: toggling a like, renaming something, reordering a list, marking a task done, creating an item whose identity the client chose. Local rules fully determine the outcome.
- Not safe: anything validated by rules the client does not have, anything where the server assigns a value the user will see (an order number, a final price after promotions and tax), anything involving inventory or money. Showing a guess about someone's money is not a latency optimisation.
Client-generated ids, the technique that unlocks the rest
Let the device create the identifier — a UUID, or better a sortable one like a ULID so that local ordering is stable. Small change, three consequences:
- The object has its permanent identity before the server has heard of it, so nothing has to be renumbered on confirmation and nothing holds a temporary id that leaks into a URL or a cache key.
- Dependent operations work offline. Create a document and rename it twice while disconnected: all three mutations reference an id that already exists.
- It doubles as the idempotency key. Lesson 6.07 required an identity minted by the originator and propagated unchanged; here the originator is the device and the object's own id is that identity. A retried create is recognised as the same create, for free, with no extra header to forget.
Rollback is hard because of causality
The naive rollback — undo the failed change — is wrong, and the reason is timing. By the time the failure returns, the user has done three more things, possibly to the same object, possibly building on the change that failed. Reverting one mutation out of the middle of that sequence discards work they did afterwards and will produce a state neither they nor you can explain.
The queue is therefore ordered and its entries are dependent. "Create doc, rename doc" cannot be reordered or partially applied. Which leaves three honest strategies:
- Make failure nearly impossible. Validate locally with the same rules the server uses, so the only failures left are genuine conflicts and outages. This is the best option, and it is an argument for keeping validation logic shareable between client and server.
- Rebase the queue. Drop the failed mutation, re-apply the rest against the server's state, and accept that some may no longer make sense. Correct, and the most work.
- Surface it as a conflict. Stop, show the user both states, let them decide. Expensive UX, and for genuinely ambiguous cases it is the only truthful option.
The failure experience is the design
The worst available choice is the silent revert: the user's edit vanishes with no explanation, they conclude the app loses data, and they stop trusting everything in it — including the parts that work. That loss of trust is much more expensive than the failed operation.
What to do instead is unglamorous and effective: keep the data, mark it clearly as unsent, say plainly what happened, offer a retry, and never discard user input as a side effect of an error path. An app that says "not saved yet — retry" is trusted. An app that quietly forgets is not.
Optimistic UI on operations where the server holds the authority. "Payment successful" rendered before the server confirmed is not a latency optimisation — it is a false statement about someone's money, and the correction arrives seconds later when they have already left the screen and told someone the money was sent. Reserve optimism for operations whose outcome you can genuinely predict. Everywhere else, show honest progress: a spinner that resolves in 400 ms costs you far less than a confirmation you have to take back.