Optimistic Updates
Apply a write to the UI before the server confirms, roll back on error, and keep the window small so a failed update does not confuse the user.
On this page
Introduction
This guide covers optimistic updates with rollback, a recurring concern in the data-fetching scenario. Waiting for the server to confirm a like or a checkbox toggle makes the UI feel slow. Applying the change to the UI first, sending the write in the background, and rolling back on error makes the interaction feel instant while staying correct, as long as the rollback is reliable.
Apply immediately
Set the new value in local state before the network call so the UI responds in the same frame as the click, which is the perceived speed win.
The trade-off worth naming is that set the new value in local state before the network call so the ui responds in the same frame as the click, which is the perceived speed win adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
const prev = item.checked;
setItem({ ...item, checked: !prev });
try { await api.toggle(item.id); }
catch { setItem({ ...item, checked: prev }); toast.error('Failed'); }
Keep the previous value
Snapshot the old value before the optimistic change so rollback restores exactly what was there, not a recomputed guess that may have drifted.
The trade-off worth naming is that snapshot the old value before the optimistic change so rollback restores exactly what was there, not a recomputed guess that may have drifted adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
const prev = item.checked;
setItem({ ...item, checked: !prev });
// on error: setItem({ ...item, checked: prev });
Roll back on error
On a failed write, restore the previous value and show a message, so the UI does not lie about state the server rejected.
The trade-off worth naming is that on a failed write, restore the previous value and show a message, so the ui does not lie about state the server rejected adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Small window
Keep the time between optimistic apply and server confirm short so a rollback is rare and brief, which is what keeps the pattern honest.
The trade-off worth naming is that keep the time between optimistic apply and server confirm short so a rollback is rare and brief, which is what keeps the pattern honest adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Conflict handling
If two updates target the same item, sequence them or use a per-item pending flag so one rollback does not undo the other optimistic value, which is the race.
The trade-off worth naming is that if two updates target the same item, sequence them or use a per-item pending flag so one rollback does not undo the other optimistic value, which is the race adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test both paths
Assert the UI shows the new value on success and the old value with a message on error, so the optimistic contract holds both ways.
The trade-off worth naming is that assert the ui shows the new value on success and the old value with a message on error, so the optimistic contract holds both ways adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: TanStack mutations, UX of optimistic.
FAQ
- When is optimistic update a bad idea?
- When conflicts are likely and the rollback would surprise the user, or when the write is expensive and a failure undoes visible work; for those, a confirmed update is clearer.
- How do I handle concurrent optimistic updates to the same item?
- Use a per-item pending state or a sequence number so the rollback of one does not clobber the optimistic value of another, which is the subtle bug.
Report a problem with this articleOpens a pre-filled issue on GitHub.