Treat the app as : every device is a local . Study actions are written to a plus an append-only (outbox); on reconnect the device and since its last sync. Conflicts are resolved by one global rule but by the .
Treat the app as : every device is a local . Study actions are written to a plus an append-only (outbox); on reconnect the device and since its last sync. Conflicts are resolved by one global rule but by the .
Mental model: think of it like git — each device "commits" locally while offline, and sync is a merge, not an overwrite.
Device (offline) Server = source of truth
┌──────────────────┐ on reconnect ┌───────────────────────────┐
│ Local DB (SQLite)│ push ops (delta) │ Sync API /sync │
│ + Op log (outbox)│ ─────────────────▶ │ • idempotent by op.id │
│ cursor = lastSeen│ ◀───────────────── │ • merge by semantics │
└──────────────────┘ pull changes>cursor└───────────────────────────┘
Progress DB (Postgres)
Phone clocks are unreliable and two devices can edit the same record offline, so "last write wins" (LWW) silently loses data. Resolve by semantics:
// op has an id -> apply idempotently (retries don't double-count). clientTs is NOT trusted.
function mergeProgress(server, op) {
return {
// progress only increases -> max: a late old op never drags it backward
progress: Math.max(server.progress ?? 0, op.progress),
// studiedMs is an additive counter -> add the delta, don't overwrite
studiedMs: (server.studiedMs ?? 0) + op.studiedMs,
completedAt: server.completedAt ?? (op.progress >= 1 ? op.clientTs : null),
};
}
Here, plain LWW would let a stale device syncing late with progress: 0.3 overwrite the server's 0.9 → lost progress. Taking max and summing studiedMs makes sync order-independent — safe when millions of devices sync with skewed clocks.
The interviewer is probing whether you know client clocks can't be trusted (so not pure timestamp LWW), whether you make sync idempotent (op ids), and whether you merge by data meaning. A weak answer: "use timestamps, last write wins." A strong answer: replica + op log + idempotency + per-field merge + server as source of truth — and naming LWW's data-loss failure explicitly.
LWW is cheap but lossy; semantic merge needs per-field thought but loses nothing. Reach for full CRDT/OT only when you need true collaborative editing (notes, documents) — it is costly, so don't use it for everything.
A library of IT interview questions with detailed answers — from Junior to Senior.
Donate