skip to content

Optimistic State & Rollback

An optimistic write shows the change before the server confirms it, keeps a pending marker across relaunches, and rolls back on a late rejection. Interviewers ask what the user sees when it fails.

on this pageshow

explore

questions

5

In an offline community-events mobile app, where must an optimistic RSVP live so it still shows, marked pending, after the app is killed and relaunched?

level: juniorimportance: must knowfreq 52%

answer

  1. memory dies with the process
  2. write the store before the network
  3. the marker is a stored field
  4. same transaction as the queued request
  5. screens observe the store, not the request

basics

~20 s

In the app's persistent local store, written in the same transaction as the RSVP's queued request, with a pending marker on the record itself. Screens observe the store, so the RSVP and its pending badge reappear after any relaunch.

solid answer

~50 s

Not in a view model or an in-memory cache: both native platforms may end a background app's process to reclaim memory with no guaranteed chance to run cleanup code, so anything held only in memory is gone on relaunch. Write the RSVP into the local database first, with a stored `syncState` (`pending`, then `failed` or cleared once confirmed), in one transaction with the entry the outbox will send. Every screen that shows the event — its page, the member's list of events, the attendee count — reads from that store, so they all show the RSVP at once and again after a cold start. The marker is data, not a spinner: it drives a quiet "not synced yet" badge, lets a failed write offer retry or discard, and tells a refresh not to overwrite the row with an older server copy.

code

json · 8 lines
json
{
  "eventId": "evt_8812",
  "title": "Riverside litter pick",
  "myRsvp": "going",
  "syncState": "pending",
  "pendingChanges": 1,
  "lastError": null
}

go deeper

for a junior

Recall that anything only in memory is lost when the OS ends the app, so the optimistic RSVP goes into the local database with a stored pending marker.

for a middle

Explain the single transaction covering the record and its queued request, and why screens observing the store all update from one commit.

for a senior

Show how the marker drives the failed state and protects pending rows from a refresh, and why a per-record boolean breaks with two unsent changes.

for a principal

Frame the pending marker as an app-wide contract every team's records carry, so sync, refresh and failure UX behave the same on every screen.

## The question behind the question An **optimistic write** shows the result of a user action before the server has confirmed it. In a community-events app, a member taps **Going** on a local event while on an underground train; the button flips at once and the event appears in their list. The interviewer then asks the follow-up that separates a demo from a design: the member pockets the phone, the OS ends the app overnight, and in the morning the app starts cold, still offline. Does the RSVP still show, and does it still say it has not reached the server? The answer depends on one decision: **where the optimistic state lives**. ## Why memory is not enough State held in a view model, a screen object or an in-memory query cache exists only while the process does. Both native platforms may end a background app's process to reclaim memory: one platform's guide states plainly that its teardown callback is not guaranteed to run when the system kills the process, and the other's says a background or suspended scene can be disconnected at any time to reclaim its resources. The member can also swipe the app away. Design as if no cleanup code runs. So: - an RSVP kept only in memory **disappears** on relaunch, although the user saw it succeed; - a request held only in memory is **never sent**; - a flag such as `isSending` held in memory is **meaningless** after a restart. The optimistic state therefore belongs in the **persistent local store** — the embedded database the screens already read from. ## What gets written, and when The sequence for a tap on **Going** is: 1. Open one local transaction. 2. Write the RSVP onto the event's local record (or an RSVP row) with `syncState = pending`. 3. Write the request that will carry it to the server — the outbox entry, whose ordering and retry policy are a separate design. 4. Commit, then let the screens update. The single transaction matters. If the record change and its queued request are committed separately, a kill between them leaves either a pending RSVP that nothing will ever send, or a queued request for an RSVP the screen no longer shows. One commit makes both true or neither. Because the screens **observe the store** rather than the network call, every screen that shows the event — the event page, the member's "my events" list, the attendee count, a home-screen summary — updates from the same commit. Nobody has to tell each screen separately. ## The pending marker is data The marker is a **stored field**, not a spinner on a button. It answers three questions after any restart: - **What does the user see?** The row renders with a small "not synced yet" badge instead of a blocking spinner; the app stays usable. - **What can fail?** If the server later refuses the write, the marker moves to `failed`, which drives an inline retry-or-discard state instead of a silent revert. - **What must a refresh respect?** A pull from the server must not overwrite a pending row with the server's older copy, or the RSVP flickers off and back on. A single boolean per record is the simplest form, and it is fine when a record can have only one change in flight. When a member can RSVP and then edit their note while both are unsent, a boolean cleared by the first acknowledgement lies about the second. Keep a **count of pending changes**, or derive the marker from the queued entries that reference the record, so the badge clears only when the last one is confirmed. ## How the marker renders | Stored state | What the member sees | What they can do | |---|---|---| | `pending` | the RSVP as made, with a muted "waiting to sync" badge | keep using the app; undo if they change their mind | | `failed` | the RSVP marked as not accepted, with the reason | retry, edit, or discard | | confirmed (marker cleared) | the RSVP with no badge | nothing special | The confirmed state is the absence of a marker, so the normal case carries no visual noise. ## Mistakes interviewers listen for - Keeping the optimistic RSVP in memory "until the server answers" — the kill is the normal case on a phone, not the edge case. - Clearing the marker when the request **leaves** the device rather than when the server **acknowledges** it; a request that left can still be lost or refused. - Using a transient on-screen spinner as the only record that something is unsent. - Writing the record and its queued request in two transactions. The design rule to state in the interview: **the store is written first, the marker is stored with it, and the screens read the store.**

  • Should the pending marker be one boolean per record, or something richer?
    A boolean works only while a record can have one unsent change. If the member RSVPs and then edits their note offline, the first acknowledgement clears the boolean while the second change is still unsent, so the badge lies. Keep a count of pending changes on the row, or derive the marker by querying the queued entries that reference the record; the badge clears only when the last one is confirmed.
  • What should a pull-to-refresh do to a row that is still pending?
    It must not replace the row with the server's copy, which predates the member's change; the RSVP would vanish and reappear when the write lands. Either skip rows with pending changes when applying the refresh, or store the server's copy as the confirmed base and re-apply the pending changes on top before showing it.

  • Holding the optimistic RSVP in screen memory is enough until the server answers.
  • The OS always lets the app finish saving before it ends the process.
  • A spinner on the button is all the pending state the app needs.
  • The pending marker can be cleared as soon as the request is sent.
  • Writing the record and its queued request in separate transactions is safe enough.
open as a page

In a community-events mobile app where the server assigns ids, how do you replace an offline-created event's temporary id everywhere once the create is acknowledged?

level: middleimportance: must knowfreq 48%

basics

~20 s

Swap it in one local transaction across the event row, every row that references it and still-queued requests, and keep an old-to-new mapping so open screens and saved links resolve. A device-minted UUID the server accepts avoids the swap.

open as a page

Hours after a member of a community-events mobile app creates an event offline and invites friends to it, the server rejects the create; how do you roll back and tell the user?

level: seniorimportance: must knowfreq 46%

basics

~20 s

Undo what claimed success — the event shown as published, the invitations shown as sent — but keep the member's work as an unpublished draft with the reason, offering fix-and-resend or discard; never revert silently.

open as a page

In a community-events mobile app, an event edit is acknowledged while a later edit is still queued; how should the response update the local copy?

level: middleimportance: should knowfreq 40%

basics

~20 s

Store the response as the record's confirmed copy, so server-computed fields replace the device's guesses, then re-apply the still-queued later edit on top and keep the record marked pending until that edit is acknowledged too.

open as a page

In a community-events mobile app, how should an RSVP to a capacity-limited event be shown while the server has not yet confirmed a seat?

level: seniorimportance: should knowfreq 42%

basics

~10 s

Not as "going". Record the request durably but show a distinct "requested — seat not confirmed" state, hold back anything that assumes a seat, and when online simply wait briefly for the server's answer.

open as a page