Rollback on Error and Conflict Resolution
Restore previous state when a mutation fails and handle server-rejected optimistic changes gracefully.
Rollback on Error and Conflict Resolution is a free React Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Rollback Complexity Scales with Mutation Type
Rolling back an optimistic add (remove the item) or delete (restore the item) is straightforward. Rolling back an update is more complex: you must restore the exact previous value. If the item was updated multiple times optimistically, you must track each prior value separately, not just the original server state.
The Update Conflict Scenario
Consider this scenario: the server has item at value Z. You optimistically update to X. While your request is in-flight, another user updates the same item to Y on the server. Your request arrives and the server rejects it (conflict). Your rollback target is Z, but the server's current state is Y. Restoring Z would overwrite Y incorrectly.
Refetch on Error as the Safe Default
The safest rollback strategy for updates is to refetch the item from the server on any mutation error rather than restoring a locally-captured snapshot. This guarantees you display the authoritative server state, regardless of what other users may have changed concurrently. Refetch is slower but always correct.
HTTP 409 Conflict Status
A well-designed API returns HTTP 409 Conflict when an optimistic update fails due to a concurrent modification. The response body typically includes the current server state. Your error handler should detect 409, use the response body to update your local state with the server's current value, and notify the user that their changes were not saved.
Idempotency for Safe Retries
An idempotent mutation produces the same result when applied multiple times. Designing mutations to be idempotent (using PUT instead of POST, including the full resource state, using idempotency keys) makes it safe to retry on failure without risk of double-applying the change. This simplifies rollback logic significantly.
Deduplication with isSubmitting
Double-clicks on a submit button can fire two identical mutations. Prevent this with an isSubmitting flag: set it to true when mutation starts, reset it when complete (whether success or failure). Disable the trigger element when isSubmitting is true. This eliminates duplicate mutations at the UI level.
Idempotency Keys for Server Deduplication
For server-side deduplication, include a unique Idempotency-Key header with each mutation request. Generate it with crypto.randomUUID() when the user initiates the action. The server detects duplicate keys and returns the same response as the original request without re-applying the operation.
Eventual Consistency Indicators
While a mutation is in-flight, you can show a subtle indicator that the optimistic state is unconfirmed: a small pulsing dot, a "Saving..." text, or a reduced opacity on the item. This communicates uncertainty without blocking interaction. Clear the indicator on success or rollback on failure.
Error Boundaries for Mutation Failures
Unexpected errors in rollback logic (e.g., accessing properties of undefined during state restoration) can crash the component. Wrap mutation-heavy components in an error boundary so that catastrophic rollback failures show a graceful error UI instead of a blank screen. Log these errors for debugging.
Versioning for Conflict Detection
A reliable conflict detection strategy: include a version number or ETag with every mutation. The server compares the client-sent version with its current version. If they differ (another update occurred), it returns 409. This is optimistic concurrency control — you assume no conflict but detect it when it occurs.
Testing Rollback Paths
Rollback logic is often untested because network failures are hard to simulate. Use tools like Mock Service Worker (MSW) to return error responses in tests. Write explicit tests for: 409 Conflict handling, network timeout rollback, double-click deduplication, and state consistency after failure. These edge cases are where bugs hide.
Conflict Detection HTTP Status
Which HTTP status code does a well-designed API return when an optimistic update fails due to a concurrent modification by another user?
Lesson Recap: Rollback and Conflicts
Update rollback is more complex than add/delete — always refetch on error to avoid stale snapshot issues. HTTP 409 Conflict signals optimistic concurrency failures; use the response body to restore the server's current state. Design mutations to be idempotent for safe retries. Prevent double-fires with isSubmitting and server-side idempotency keys. Test rollback paths explicitly using MSW to simulate errors.
Frequently asked questions
Is the “Rollback on Error and Conflict Resolution” lesson free?
Yes — the full text of “Rollback on Error and Conflict Resolution” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “Rollback on Error and Conflict Resolution”?
Restore previous state when a mutation fails and handle server-rejected optimistic changes gracefully. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Rollback on Error and Conflict Resolution” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What Is Optimistic UI and When to Use It
- Implementing Optimistic Updates Manually
- Rollback on Error and Conflict Resolution
- Optimistic Patterns with React Query and Zustand