Quick revision
The short answer
Track changes per field, not per document, and stamp each change with a unique id, the device that made it and a hybrid logical clock or version vector, so the server can tell concurrent edits from sequential ones. Then merge by field type: scalars by an explicit rule (last writer wins per field, or flag the conflict), the checklist as an observed-remove set with per-item state, and notes as an append-only list. Status transitions such as closed or invoiced stay with the server, which validates them and rejects a conflicting offline change with a clear message.
What to remember
In this answer, we designed offline sync around per-field changes, each with a unique id, a device id, an HLC timestamp and the version it was based on. Scalars merge by an explicit rule or get flagged, the checklist is an observed-remove set and notes are append-only, while status transitions are validated by the server, which rejects conflicting ones with a clear message. The protocol pushes changes idempotently, pulls others' changes since a token and rebases pending edits, and compaction plus a full resync after the retention window keep metadata bounded.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Track changes per field, not per document, and stamp each change with a unique id, the device that made it and a hybrid logical clock or version vector, so the server can tell concurrent edits from sequential ones. Then merge by field type: scalars by an explicit rule (last writer wins per field, or flag the conflict), the checklist as an observed-remove set with per-item state, and notes as an append-only list. Status transitions such as closed or invoiced stay with the server, which validates them and rejects a conflicting offline change with a clear message.
Why this question is asked
Offline-first apps are where conflict handling stops being theory: a technician in a basement with no signal edits a work order for three hours, while the office edits the same order. Interviewers ask this design question to see whether we can choose merge rules per kind of data and still recognise the few decisions that must stay central. In this answer, we will set out the requirements, design the metadata kept with every change, go through how each field type merges, decide where the server must still say no, describe the sync protocol, and plan for the growth of metadata and devices that stay offline too long.
1Requirements and the Shape of the Data
A work order has scalar fields (assigned technician, priority, scheduled time, address), a checklist of maybe 20 items that technicians tick, notes that people write during the job, and a status that moves through states such as open, in progress, completed, closed and invoiced. Technicians must edit all of it offline for hours, and several people, including the office, may edit the same order.
Two goals pull against each other. We want offline edits to survive, because losing a technician's notes after a long job is unacceptable. And we want business rules to hold, because a work order that was invoiced must not be reopened by a phone that was offline. The design keeps as much as possible mergeable and puts only the rule-bound parts behind the server.
2What Metadata Each Change Carries
The client does not sync documents; it syncs changes. Each change records the work order id, the field or item it touches, the new value, a globally unique change id (a UUID made on the device), the device id, and a timestamp from a hybrid logical clock (HLC). An HLC combines the wall clock with a counter, so its timestamps stay close to real time but always increase and respect cause and effect between devices that have synced.
Recording changes per field is the key choice. If the technician edits the scheduled time and the office edits the priority, those are different fields, and both survive without any conflict at all. A whole-document version would have reported a conflict, or worse, silently overwritten one of them.
To tell a concurrent edit from a sequential one, each change also records the version it was based on: the server sync token or the version vector the device had seen for that field. If the office changed the priority after the technician's device had already seen the technician's previous change, the edits are sequential and the later one simply applies. If neither had seen the other, they are concurrent, and the field's merge rule decides.
3How Each Field Type Merges
Scalar fields take an explicit rule. For low-stakes fields such as the contact phone number, last writer wins per field by HLC is fine. For fields where a silent choice could hurt, such as the scheduled time or the assigned technician, we keep both values and flag the conflict for a person in the office to resolve, showing who set what and when.
The checklist is a set of items, each with its own state. We model it as an observed-remove set, a set type where an add carries a unique tag and a remove only cancels the tags the remover had seen, plus per-item state for done or not done. Two technicians ticking different items merge trivially. If one adds an item while another removes it, the add wins, which is safer for a checklist.
Notes are where people lose the most work, so we never overwrite them. The simplest design is an append-only list of entries, each with author, HLC timestamp and text, sorted by timestamp for display; concurrent notes simply both appear. If the product truly needs collaborative editing of one text, a sequence CRDT such as the ones in Automerge or Yjs merges character insertions, but that is more machinery than most work orders need.
| Field | Merge rule | Why |
|---|---|---|
| Contact phone, description | last writer wins per field (HLC) | low stakes, rarely edited together |
| Scheduled time, assignee | keep both, flag for the office | a wrong silent choice sends a person |
| Checklist | observed-remove set + per-item state | different items never conflict |
| Notes | append-only list of entries | nobody's text is ever lost |
| Status | server validates every transition | invoicing and closing are business rules |
4Where the Server Must Still Decide
Some changes cannot be merged, because they depend on rules that involve other parts of the business. Status is the clearest case. Once the office has closed and invoiced a work order, a technician's offline change from completed back to in progress must not reopen it, because an invoice has been sent.
So status transitions are requests, not facts. The device records that the technician tried to move the order to completed. On sync, the server checks the transition against the current state and the allowed state machine. If it is valid, it applies; if not, the server rejects it and returns a clear message, such as 'This work order was closed by the office at 14:05; your change to completed was not applied.' The technician's notes and checklist from the same session are still accepted, because they merge.
The same applies to anything with money or inventory, such as parts used from stock: the device records the intent, and the server, which sees all requests, decides.
5The Sync Protocol
Sync is a simple exchange that is safe to repeat. The client sends its unsynced changes together with the last sync token it received. The server applies each change idempotently, keyed by its change id, so a retry after a dropped connection does not apply anything twice. It then returns its verdict for each change and every change made by others since the client's sync token, plus a new token.
The client applies the incoming changes, then rebases its own still-pending edits on top, the same way git rebases local commits on a new upstream: the pending edits are replayed on the fresh state, and any that the server rejected are shown to the user rather than silently dropped.
6 messages in order. Play them, or step through them one at a time.
6Metadata Growth and Long-Offline Devices
Merge metadata grows. Removed checklist items leave tombstones, small markers that say this tag was removed, so that an old device cannot bring the item back. Version information accumulates per device. We compact them once every device that holds the work order has synced past them; the server knows each device's last sync token, so it can tell when that is.
We cannot wait forever for a device that never returns, so we set a retention window, for example 30 days. A device that comes back after that has missed compaction, so instead of an incremental sync it gets a full resync: the server sends current state, and the device's pending edits are replayed as new changes against it, with conflicts surfaced to the user. That keeps metadata bounded without risking resurrection.
7Follow-up questions
How would you test conflict handling before release?
I would write the merge rules as pure functions and property-test them: for random sets of concurrent changes, applying them in any order on any number of replicas must produce the same state, and no note or added item may be lost. Then I would run simulated devices against a real server with injected disconnects, clock offsets and duplicated requests, and finally a field trial with technicians in places with poor signal, checking server logs for rejected changes.
Why a hybrid logical clock instead of the device's wall clock?
Phone clocks can be minutes off. An HLC stays close to wall time but never goes backwards and always moves past any timestamp the device has received, so a change made after seeing another change always sorts after it, even with a bad clock.
Why not just lock the work order while a technician has it?
Because the technician is offline and cannot release or renew the lock, and the office would be blocked for hours. Per-field merging gives most of the safety of a lock without stopping anyone from working.
8Conclusion
In this answer, we designed offline sync around per-field changes, each with a unique id, a device id, an HLC timestamp and the version it was based on. Scalars merge by an explicit rule or get flagged, the checklist is an observed-remove set and notes are append-only, while status transitions are validated by the server, which rejects conflicting ones with a clear message. The protocol pushes changes idempotently, pulls others' changes since a token and rebases pending edits, and compaction plus a full resync after the retention window keep metadata bounded.
Open with
I would design it around per-field changes rather than whole documents. Every edit on the device becomes a change record with a UUID, the device id, the field it touches, a hybrid logical clock timestamp and the sync version it was based on, so the server can tell whether two edits were concurrent or one had seen the other.
Then, if they want more
Then each field type gets its own merge rule. Ordinary scalars use last writer wins per field, and risky ones like the scheduled time or the assignee keep both values and get flagged for the office. The checklist is an observed-remove set with per-item state, so ticking different items never conflicts and a concurrent add survives a remove.
Notes are an append-only list of entries, so nobody's text is ever overwritten. Status is different: transitions like closed or invoiced are business rules, so the device only requests them and the server validates each one, rejecting a stale offline change with a clear message while still accepting that session's notes and checklist.
Sync itself pushes pending changes with the last token, the server applies them idempotently by change id and returns changes since that token, and the client rebases its pending edits. Finally I compact tombstones once every device has synced past them and force a full resync for devices offline beyond the retention window.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design the sync and conflict handling: what metadata is kept, how each field type...": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/7731589a76/