In a mobile mail client, how should an incoming sync pull be merged so it never hides the user's own unsent changes?
answer
- the server has not seen it yet
- server state and pending ops apart
- replay pending operations on top
- the acknowledgement carries a revision
- discard pulled copies older than the ack
basics
~20 sKeep the server's last-confirmed state apart from pending outbox operations. A pull updates only the server layer; the screen shows it with pending operations replayed on top, so an offline flag stays set until confirmed or rejected.
solid answer
~50 sA pull reports the server's state, and the server has not seen writes still in the outbox. If the pull simply overwrites local rows, a message the user flagged or archived offline flips back, then flips again once the write lands. I store the **base** (last server-confirmed state) separately from the pending operations and derive what the screen shows as base plus pending operations replayed in order. A pull updates the base; the pending operations are then replayed. When an operation is acknowledged, its result becomes the base and the entry leaves the outbox in the same transaction. The acknowledgement should carry the message's new server revision, so a pulled copy computed before the write landed — an older revision — cannot roll it back. If someone else changed the same field meanwhile, that is a conflict for the conflict policy; the merge only guarantees nothing pending is hidden.
go deeper
Recall that the server does not know about writes still queued on the phone, so pulled state can be older than what the user just did.
Explain keeping the server-confirmed base apart from pending operations and showing the base with those operations replayed on top.
Cover the race: acknowledgements that carry revisions, discarding pulled copies older than the ack, and routing true concurrent edits to the conflict policy.
Decide where this machinery pays: a shared base-plus-pending layer for every offline-writable type, versus server-authoritative data that simply waits for the network.
## The bug A user on a plane flags three messages and archives a fourth. Each change is written locally and queued in the outbox. On landing, the client reconnects and pulls changes. The server has not received the queued writes yet, so its view says the three messages are unflagged and the fourth is in the inbox. If the pull **overwrites** the local rows, the flags disappear and the archived message pops back into the inbox. A second later the outbox sends the writes, the next pull brings them back, and the screen flickers. Worse, in the gap the user may archive the message again, or reply to the wrong state. The user expects to read their own writes. On one client, that is a merge rule, not a database setting. ## Why it happens - A pull describes the **server's** state, which by definition excludes anything still queued on the phone. - Responses can be computed before a write lands and arrive after its acknowledgement, so even "already sent" is not enough. - Applying a pulled record by id replaces every field, including the ones with pending changes. ## Options compared | Merge rule | What the user sees | Cost | |---|---|---| | Overwrite with pulled state | own changes flicker or vanish until the write lands | none, and wrong | | Skip any record with a pending write | own changes kept, but other people's changes to that record hidden while it waits | simple, lossy for days on a phone away | | Skip only fields with a pending write | own fields kept, other fields updated | needs per-field tracking; breaks for operations that touch several fields | | Base plus pending replay | own changes on top of everything new | a base layer and replayable operations | ## Base plus pending replay 1. Keep the **base**: the last state the server confirmed, for each message. 2. Keep the outbox's **pending operations** in order: "set flag on m-12", "move m-40 to Archive". 3. The screen reads the **derived state**: base with pending operations replayed in order. In practice the client stores the derived values in the rows the screen observes and recomputes affected rows whenever the base changes. 4. A **pull updates the base only**, then replays the pending operations for the records it touched. 5. When the server **acknowledges** an operation, write its result into the base and remove the outbox entry in the same transaction, so a kill cannot leave the operation both applied and still queued. 6. If the server **rejects** it for good, drop the operation and recompute; the derived state falls back to the base, and the user is told. How the screen marks pending and failed items is the branch's optimistic-state question. ## Revisions guard against late responses Ordering inside one cycle is not enough on its own: a pull request sent just before an acknowledgement can return just after it, describing the message as it was before the write. If the server assigns each record an increasing **revision**, the acknowledgement can return it ("m-12 is now revision 42"). Any pulled copy of m-12 with a lower revision predates the write and is ignored for that record. Device clocks are no substitute: they can be wrong or set by hand, and they cannot order the server's own history. ## Where this stops - **Real concurrency.** If a laptop changed the same field while the phone's write was queued, the replay puts the phone's value on top locally, but which value wins on the server is the write-conflict policy's decision, made when the write is sent. - **The outbox itself** — its durability, ordering and retries — is its own design question; this merge only reads it. - **Session-guarantee theory**, such as which named guarantee a bug violates in a replicated store, belongs to the consistency material; here it is one client's merge. ## Summary - A pull describes the server, which has not seen queued writes. - Keep a base and pending operations; show base plus replay. - Acknowledgements move results into the base atomically and carry revisions. - Pulled copies older than an acknowledged revision are discarded.
- Why is skipping every record with a pending write not good enough?It hides other people's changes to that record — a new reply in the thread, a label added on the laptop — for as long as the write is queued, which on a phone away for days can be days. Skipping per field is better; replaying pending operations over a fresh base keeps every change that does not touch the same field.
- What if a pulled response arrives after the acknowledgement but reflects a state from before it?Compare server revisions. If the acknowledgement returned revision 42 for that message, a pulled copy at revision 41 predates the write and is ignored for that record. Without a server-assigned revision the client cannot tell stale from newer, and device clocks cannot stand in for one.
- A pull should overwrite local rows, because the server is always right.
- A brief flip back is harmless, since the change returns once the outbox drains.
- Comparing device timestamps reliably tells which copy of a message is newer.
- Skipping whole records that have pending writes loses nothing.
- Once a write is acknowledged, every later pull response must be newer.