React Native · State management
Making like, comment and cart actions feel instant without lying to the user
A social feed felt sluggish even though the API was fast enough. The fix wasn’t a faster backend — it was applying the user’s action to the UI immediately and only reconciling with the server afterward, with a rollback path for when the server disagrees.
Context
A feed screen let users like posts, add quick comments and adjust a cart quantity. Each action went through a standard request/response cycle: disable the control, show a spinner, wait, update state. On a good connection this was barely noticeable; on a flaky mobile connection — the normal case for a lot of real usage — every tap had a visible, variable delay before anything on screen moved.
Problem
The app wasn’t actually slow; it was honest in a way that read as slow. Waiting for a server round trip before changing a heart icon or a counter means the perceived latency of every interaction is the network latency, every time, even for actions that succeed almost all of the time. The deeper risk sat on the other side of the fix: once you update the UI before the server confirms anything, you need a real plan for what happens when the request fails, times out, or comes back with a different value than what the user tapped — otherwise “optimistic” just means “eventually wrong.”
Approach — apply, track, reconcile, roll back
- Apply the change locally first. On tap, the reducer updates the local post — liked state, count, comment list — synchronously, so the screen reflects the user’s intent in the same frame.
- Keep the previous value, not just a flag. Each optimistic mutation captures a snapshot of what it's changing before applying the update, so rollback is restoring known-good state rather than guessing at an inverse operation.
- Track pending mutations explicitly. Every optimistic change gets an id and a status (
pending→committed/rolled_back), so the UI can show a subtle in-flight indicator and so a second tap on the same control queues or coalesces instead of racing the first request. - Reconcile on the response, not just on error. A successful response can still disagree with the optimistic guess (e.g. someone else’s like landed first); the mutation resolves to the server’s value, not blindly to “keep what the UI already shows.”
- Make failure visible and specific. On failure, the snapshot is restored, a small toast names what happened, and for actions worth retrying (not every one is) a retry affordance is offered instead of silently dropping the attempt.
- Reserve pessimistic updates for actions that shouldn’t be guessed at. Payment confirmation and irreversible destructive actions stayed request-then-update; optimism is a UX tool for low-stakes, highly-reversible actions, not a default for everything with a network call.
Illustrative outcome
Figures below are illustrative, from working through this pattern on a representative feed screen — not a specific client’s production metrics.
- Perceived latency for like/comment actions dropped to effectively zero on a successful path — the UI no longer waited on the network to look done.
- Rollback being a first-class, tested path (not an afterthought) meant raising the simulated failure rate during QA didn’t surface any state where a like count or comment list drifted from what the server actually held.
- Treating pending mutations as real state, with ids and statuses, made double-tap and rapid-toggle bugs easy to reason about instead of being a source of race-condition reports.
What I took from it
Optimistic UI is a trade: you buy back perceived latency by taking on the responsibility of being right eventually, not just immediately. The pattern only earns its complexity for actions that are cheap to be wrong about and quick to correct visually — a like, a comment count, a quantity stepper — and it needs an explicit snapshot-and-rollback path from day one, not bolted on after the first bug report about a count that “sometimes doesn’t match.”
The interactive demo below makes the mechanism visible: toggle between optimistic and pessimistic strategies on the same feed, dial up latency and failure rate, and watch what rollback actually looks like when a request comes back negative.