Interview concept

Offline Sync And Conflict Resolution

Asked of: Software Engineer

Last updated

What's being tested

This topic tests whether you can design a client-server system that remains correct across disconnected clients, retries, concurrent writes, and partial failures. The interviewer is probing your understanding of causal ordering, idempotency, conflict semantics, durable local state, and synchronization protocols—not merely whether you can say “use last write wins.” Meta cares because products operate across unreliable mobile networks and globally distributed services, where correctness, latency, and graceful offline behavior directly affect billions of user interactions.

Core knowledge

  • Offline-first architecture treats the client as a durable replica: reads come from a local database, writes enter an outbox, and synchronization runs asynchronously. > Tip: Never rely on an in-memory queue; process crashes must not lose accepted local mutations.

  • A mutation should carry a globally unique operation ID, entity ID, author/device ID, payload, and causal metadata. The server deduplicates operation IDs, making retries safe under timeouts and at-least-once delivery.

  • State-based synchronization exchanges full or summarized state and computes a merge; it is simple but bandwidth-heavy. Operation-based synchronization exchanges mutations, reducing payload size but requiring reliable delivery, deduplication, and deterministic application.

  • Last-write-wins (LWW) is easy to implement but dangerous: wall clocks skew, simultaneous edits can silently erase data, and timestamps alone do not express causality. If used, define a deterministic tie-breaker such as (logical_time, device_id, operation_id).

  • Lamport clocks provide a scalar ordering: on local or received event, set L←max⁡(L,Lreceived)+1L \leftarrow \max(L, L_{\text{received}}) + 1. They order events consistently but cannot distinguish causally unrelated concurrent operations.

  • Vector clocks represent causality per replica. For vectors VaV_a and VbV_b, Va≤VbV_a \le V_b iff every component is no greater; if neither dominates, the versions are concurrent and require a merge policy. They become expensive as device or replica count grows.

  • CRDTs provide deterministic convergence when operations or states satisfy properties such as commutativity, associativity, and idempotence. Use an observed-remove set for membership semantics, a grow-only counter where appropriate, or sequence CRDTs such as RGA/LSEQ for collaborative text.

  • Deletions require tombstones or deletion versions. Physically removing a record can let an old offline replica resurrect it. Tombstones need garbage collection only after the system knows all relevant replicas have advanced beyond the deletion.

  • A sync endpoint should support a cursor or checkpoint, such as sync_token, and return changes after that point. Make tokens opaque, durable, and invalidatable; clients must perform a full resync when retention expires or history is compacted.

  • Conflict semantics must match the domain. A notes title may use deterministic LWW, while a set of shared members should merge additions and removals explicitly. For financial or inventory data, reject or serialize conflicting writes rather than silently merging.

  • Separate transport reliability from application correctness. HTTPS, retries, and queues do not prevent duplicate operations or stale writes. The application still needs authentication, authorization at mutation time, deduplication, validation, and version checks.

  • Local optimistic updates need a reconciliation model: retain the original operation, apply it locally, incorporate remote changes, and replay pending operations. Clearly define whether a rejected operation is rolled back, transformed, or surfaced to the user.

Worked example

Consider the representative prompt: “Design offline sync and conflict resolution for a mobile notes app.” In the first 30 seconds, clarify whether notes are single-user or shared, whether concurrent character-level editing is required, expected offline duration, acceptable data loss, and whether the server is authoritative for permissions. I would organize the answer around local durable storage and an outbox, a sync protocol with cursors and idempotency, a versioning/conflict model, and observability plus recovery. Each local edit receives an operation ID and causal version, is committed transactionally with the local note, and is uploaded repeatedly until acknowledged. The server authenticates the device, deduplicates the operation, stores an ordered change log, and returns remote changes after the client’s checkpoint. For simple note metadata, I might choose deterministic LWW with a server-assigned logical timestamp; for shared rich text, I would use a sequence CRDT or operation transformation rather than overwrite the whole document. The explicit tradeoff is implementation complexity and metadata growth versus avoiding silent data loss during concurrent edits. I would close by noting that I would add property-based convergence tests, crash/retry tests, tombstone compaction, conflict metrics, and a user-visible recovery path if time allowed.

A second angle

For an offline shared checklist, the core problem is not character ordering but membership operations such as “add item,” “check item,” and “remove item.” A remove operation must identify the version or add operation it removes; otherwise an old offline add can resurrect the item. An observed-remove set or per-item versioned state may be more appropriate than a text CRDT. The framing shifts from mergeable text sequences to explicit business invariants, such as “an item cannot be checked after permanent deletion.” This illustrates why selecting a conflict algorithm before defining domain semantics is backwards.

Common pitfalls

Pitfall: Saying “use timestamps and take the latest” without addressing clock skew, equal timestamps, causality, or silent data loss is an analytical mistake. State the exact ordering source and explain which concurrent updates may be discarded.

A common communication mistake is jumping directly into CRDT names before clarifying the product semantics. Ask whether edits are field-level, list-level, or character-level, and distinguish offline single-device edits from concurrent edits by multiple users.

Pitfall: Treating retries, authentication, and a message queue as a complete sync design misses duplicate delivery, stale authorization, tombstone retention, checkpoint invalidation, and client crash recovery. Explain the end-to-end state transitions and failure behavior.

Connections

The interviewer may pivot to replication models, event sourcing, idempotent APIs, exactly-once illusions, or distributed garbage collection. They may also ask how to test convergence, enforce authorization on replayed operations, or migrate the local schema while clients remain offline.

Further reading

Related concepts