DEV Community

Magiquip
Magiquip

Posted on

Offline Sync Conflicts: Make Every Resolution Explainable

Offline sync conflicts: make every resolution explainable

An offline edit can be correct and still be stale. Two people update the same customer record on different devices: one changes the phone number, the other changes the billing address. When the devices reconnect, neither edit is inherently wrong—but a naïve sync can still make one disappear.

The design question isn’t just which value wins. It’s how the system detects concurrent work, which changes are safe to combine, and what the user sees when the answer depends on context only they can provide.

Conflict handling is part of the product, not just an error handler.

Start with a shared baseline

To detect concurrent edits, the client needs to know what the user started with. Think of the record in three versions:

  • Base: the version the device had when editing began.
  • Local: the user’s edited version.
  • Remote: the current server version when the device reconnects.

For example:

Field Base Local Remote
Phone 555-0100 555-0199 555-0100
Address 14 Oak St 14 Oak St 22 Pine St

The phone changed locally; the address changed remotely. Those edits can usually be combined without asking the user.

A version token lets the server detect stale writes. The client sends the revision it edited, and the server accepts the update only if that revision is still current. The token might be a revision number, an entity tag, or another server-managed value. A timestamp alone is a weaker choice: device clocks can disagree, and updates can share a timestamp.

The client also needs enough information to compare Base, Local, and Remote. Keeping a full base snapshot is straightforward. For larger records, a base revision plus a carefully defined set of field-level changes may be more efficient. Either way, a patch without its editing context isn’t enough to resolve a conflict safely.

Merge what’s independent; ask about what isn’t

For a simple scalar field, a three-way merge can use these rules:

  1. If Local equals Base, only the remote side changed it. Keep Remote.
  2. If Remote equals Base, only the local side changed it. Keep Local.
  3. If Local equals Remote, both sides arrived at the same value. Keep that value.
  4. Otherwise, both sides changed the field differently. Mark it as a conflict.

In the example, the phone follows rule two and the address follows rule one. The result keeps both changes.

These rules are useful only when the field’s meaning supports them. For example:

  • Independent profile fields can often be merged separately.
  • A set of uniquely identified tags may be merged by combining additions, if removals have defined semantics.
  • An append-only event log can accept distinct events rather than replacing the whole list.

Other values need domain-specific care. A list’s order may matter. Text edits may be mergeable when their ranges don’t overlap, but formatting and structural changes can complicate the result. Payment status or inventory counts may depend on other fields or server-side transactions. Treating these as ordinary independent values can produce a record that is technically merged but invalid for the product.

“Last write wins” is a policy, not a neutral default. If the product uses it, define how the winner is determined and make the data-loss trade-off explicit. Silently replacing an important user-authored change is often the wrong experience.

Give the user a decision they can understand

When both sides changed the same field differently, show the competing values and describe the choices:

Phone number changed on both devices

This device: 555-0199

Current server version: 555-0175

Keep this device’s number, use the server’s number, or enter a different one.

“Keep mine” shouldn’t mean “overwrite the whole server record with an old copy.” It should mean applying the local choice to the current server record while preserving other changes that can be merged. Likewise, “use the server version” should discard only the conflicting local change—not unrelated local work that remains safe to apply.

A useful conflict screen answers three questions:

  • What changed? Show the relevant field or content, not an opaque revision ID.
  • Which version is which? Label them clearly, such as “This device” and “Current server version.” Don’t rely on device timestamps as proof of ordering.
  • What will this choice do? Explain whether non-conflicting edits will still be kept.

For complex records, a field-by-field comparison may be easier to review than an all-or-nothing choice. Rich text may call for a side-by-side view or a focused editor rather than an automatic merge.

Keep the local and remote versions available until the user resolves the conflict. If they dismiss the screen, communicate that action is still needed; don’t present the record as fully synced. “Saved on this device” and “synced with the server” are different states.

A conflict is not a network retry

A temporary connection failure and a stale revision need different handling.

For a transient failure, retry the same mutation with the same identity and payload. A stable mutation ID, deduplicated by the server, helps prevent a timed-out request that actually succeeded from being applied twice. Use bounded backoff rather than repeatedly sending requests at a fixed, aggressive rate.

For a version conflict, don’t keep resending the stale write until it happens to succeed. Fetch the current record, compare it with the original base and local intent, then merge or ask the user. If the user chooses a value, submit a new mutation against the latest known revision. If the record changes again before that mutation is accepted, compare again. A resolution screen can become stale too.

Keep the outcomes distinct:

  • Network error or timeout: retry the same operation safely.
  • Revision mismatch: reconcile against the newer version.
  • Validation failure: explain which rule the proposed result violates; don’t treat it as a transient sync problem.

The server remains responsible for the final concurrency check. A client-side merge is a proposal, not a guarantee that the record hasn’t changed since it was fetched.

Make the flow testable

Conflict behavior is easier to verify when the merge policy is a deterministic function of Base, Local, and Remote. It should return either a merged candidate or unresolved fields, without depending on UI state, network timing, or the device clock.

Tests should cover at least:

Scenario Expected result
Only the local side changes a field Keep the local value
Only the remote side changes a field Keep the remote value
Both sides make the same change Keep that value without prompting
Both sides make different changes Surface a conflict
Different independent fields change Merge both changes
A retry repeats an already accepted mutation Don’t apply it twice
The record changes while the user is resolving Reconcile against the newer version
One side deletes a record while the other edits it Apply an explicit delete-versus-edit policy
A proposed merge violates a domain rule Reject it with a useful explanation

Also test the user-facing states: a change pending sync, a conflict awaiting a decision, a resolution submitted but not yet confirmed, and a record fully synced. They’re distinct outcomes, even if the underlying data model is small.

The best conflict flow isn’t the one that eliminates every prompt. It combines genuinely independent work, asks for help when intent is ambiguous, and makes each outcome clear enough to trust. When a conflict reaches the user, they should be choosing between understandable versions—not guessing what “retry” might overwrite.

Top comments (0)