skip to content

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%

answer

  1. the server knows things the device guessed
  2. confirmed base versus shown copy
  3. re-apply what is still pending
  4. clear only the acknowledged change
  5. same transaction, same commit

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.

solid answer

~40 s

Keep two things per record: the last **confirmed** copy from the server and the **pending changes** not yet acknowledged; the screen shows the confirmed copy with the pending changes applied in order. When an acknowledgement arrives, remove the acknowledged change, replace the confirmed copy with the response — which carries what only the server knows: the revision, the attendee count, the normalised title, timestamps — and rebuild the shown copy by re-applying whatever is still pending. Copying the response straight over the shown row would erase the later edit the member made offline; keeping the optimistic copy and ignoring the response leaves guessed counts and a stale revision that the next write will be checked against. Do it in one transaction, and clear the pending marker only when no changes remain.

code

pseudocode · 13 lines
pseudocode
on_acknowledged(change, response):
  begin transaction
    delete pending_change where id = change.id
    record = load(change.record_key)
    record.confirmed = response.body
    shown = record.confirmed
    remaining = pending_changes for record.key, oldest first
    for each p in remaining:
      shown = apply(p, shown)
    record.shown = shown
    record.sync_state = if remaining is empty then "confirmed" else "pending"
    save(record)
  commit

go deeper

for a junior

Recall that the server's reply carries values the device could only guess, such as counts and revisions, so it must be written back to the local copy.

for a middle

Explain the confirmed-copy-plus-pending-changes model and the rebuild that re-applies remaining edits after each acknowledgement.

for a senior

Show why a response predates later queued edits, why a stale revision breaks the next write, and why the marker clears per change.

for a principal

Argue for one shared reconciliation routine every record type uses, so teams cannot each invent a slightly different merge that loses edits.

## The situation In a community-events app, a member edits their event offline: first they change the venue, a few minutes later the start time. Both changes are applied optimistically and queued. Back in coverage, the venue change is sent and acknowledged; the start-time change is still waiting. The server's response describes the event **as the server now holds it** — with the new venue, but with the **old** start time, because the server has not seen the second edit yet. What the client does with that response decides whether the member sees a correct event, a flicker, or a silently lost edit. ## Two wrong answers that are common - **Copy the response over the shown row.** The venue is right, but the start time jumps back to the old value. The member's second edit vanishes from the screen until its own acknowledgement arrives — or for good, if the next refresh writes the old time into the row the queue reads from. - **Keep the optimistic copy and ignore the response.** Nothing flickers, but the device keeps values it only guessed: an attendee count it incremented locally, a title the server trims or normalises, an updated-at time from the device clock, and — most damaging — the **revision** or entity tag the server assigned. The next write sent as "based on revision 6" when the server is on 7 may be refused as stale. ## Confirmed base plus pending changes The design that avoids both keeps two things per record: - the **confirmed copy** — the last version the server acknowledged or sent; - the **pending changes** — the member's unacknowledged edits, in the order they were made (the queued entries can serve as this list). What the screen shows is derived: the confirmed copy with each pending change applied on top, in order. On acknowledgement: 1. Remove the acknowledged change from the pending list. 2. Replace the confirmed copy with the server's response. 3. Rebuild the shown copy by re-applying the remaining pending changes. 4. Set the pending marker from what remains: still pending if anything is left, confirmed if nothing is. 5. Commit all of it in one transaction, so the screen updates once. In the example, the shown event has the server's venue, revision and attendee count, plus the member's new start time, still badged as waiting to sync. Storing two copies costs some space and a little code: the confirmed copy is rewritten on every acknowledgement and every refresh, and the shown copy is recomputed from it. In return, one rebuild serves every outcome — an acknowledgement, a refusal of one pending change (drop it and rebuild), or a refresh that updates the confirmed copy — so there is one code path to test instead of three hand-written patches. ## Which fields come from where | Field kind | Example | After the acknowledgement | |---|---|---| | Server-computed | revision, attendee count, created and updated times, moderation status | always the server's value | | Normalised by the server | trimmed title, canonical address | the server's value, unless a later pending change sets that field | | User-entered, no later change | the venue in the acknowledged edit | the server's value, which should match | | User-entered, later change pending | the start time edited again offline | the pending value, re-applied on top | If the store keeps only one merged row instead of two copies, the same rule applies field by field: take the server's value for every field **except** those with a pending change, and keep the record marked pending. That works for simple fields; once pending changes can depend on each other, the two-copy model is easier to get right. ## Why this outlives the session All of this state — the confirmed copy, the pending list, the marker — sits in the persistent store. The acknowledgement may arrive minutes after the edit or after a relaunch the next morning; the rebuild gives the same answer either way, because nothing it needs lived only in memory. ## What the interviewer listens for - Naming the **server-computed fields** and why guessed values must not survive. - Spotting that a response predates any later pending edit. - Clearing the marker per change, not per record. - Doing the merge in one transaction.

  • The device guessed the attendee count would rise by one; the response says it rose by three. Which wins?
    The response. The count is computed by the server from everyone's RSVPs, and other members joined while this device was offline; the device's increment was only a guess for the moment between tap and acknowledgement. Server-computed fields always come from the confirmed copy.
  • Why does keeping a stale revision after an acknowledgement cause trouble later?
    If the API checks the base revision of each write, the next edit is sent as based on the old revision and the server may refuse it as stale, although nobody else changed the event. Taking the revision from the response keeps the next write's base current.

  • Copy the whole response over the shown row, even while later edits are queued.
  • Keep the optimistic copy; the response matters only when it reports an error.
  • The attendee count the device incremented locally is accurate enough to keep.
  • Clear the record's pending marker on any acknowledgement for that record.
  • A response always includes every edit the member has made so far.