DEV Community

Cover image for Two devices edited the same record offline. Which edit wins?
Stacklyn Labs
Stacklyn Labs

Posted on

Two devices edited the same record offline. Which edit wins?

A user is on a flight with no Wi-Fi. They open your Flutter app and edit a document. On the ground, a colleague opens the web dashboard and edits the same document. The plane lands, the phone reconnects, and your sync service now holds two versions of the same record.

If the app simply pushes its copy and overwrites the server, the colleague's work is gone. Nobody gets an error. The data just quietly disappears.

Offline-first apps need a conflict rule decided up front, not discovered in production. Here are three approaches, from simplest to most careful, and when each one fits.

First, know what is waiting to sync

Conflict resolution only works if the app knows which local records are unsynced. The usual pattern is a queue in your local store: every offline create or edit is saved to a Hive box with a pendingSync flag.

@HiveType(typeId: 1)
class Note extends HiveObject {
  @HiveField(0) String id;
  @HiveField(1) String title;
  @HiveField(2) String body;
  @HiveField(3) DateTime updatedAt;
  @HiveField(4) bool pendingSync;

  Note(this.id, this.title, this.body, this.updatedAt, {this.pendingSync = true});
}

Iterable<Note> pending(Box<Note> box) =>
    box.values.where((n) => n.pendingSync);
Enter fullscreen mode Exit fullscreen mode

That flag separates "already matches the cloud" from "waiting to go out". When connectivity_plus reports the network is back, the sync service walks this list. If the server is down or rate limiting, retry with exponential backoff (wait 2 seconds, then 4, then 8) instead of dropping the item after one failure.

Now, the collision.

Strategy 1: Last-Write-Wins

Stamp every record, or better every field, with a timestamp when it changes. On conflict, the server keeps the newer one and discards the older.

Note resolveLww(Note local, Note remote) =>
    local.updatedAt.isAfter(remote.updatedAt) ? local : remote;
Enter fullscreen mode Exit fullscreen mode

It is deterministic and easy to reason about. It fits apps where one user syncs their own data across their own devices: a phone and a tablet are unlikely to be edited at the same moment, and "the latest edit wins" matches what the user expects.

The cost: in the flight example, one of the two people loses their whole edit, even if they touched different parts of the document.

Strategy 2: Field-level merge

Instead of comparing whole documents, compare field by field. If one person changed the title and the other changed the description, nothing actually overlaps, so you can keep both.

Map<String, Object?> mergeFields({
  required Map<String, Object?> base,   // last synced version
  required Map<String, Object?> local,
  required Map<String, Object?> remote,
}) {
  final merged = Map<String, Object?>.from(base);
  for (final key in base.keys) {
    final localChanged = local[key] != base[key];
    final remoteChanged = remote[key] != base[key];
    if (localChanged && !remoteChanged) merged[key] = local[key];
    if (remoteChanged && !localChanged) merged[key] = remote[key];
    if (localChanged && remoteChanged) {
      merged[key] = remote[key]; // same field: fall back to a rule, e.g. LWW
    }
  }
  return merged;
}
Enter fullscreen mode Exit fullscreen mode

You need the last synced version (base) to know who changed what, so keep it next to the local copy. When both sides change the same field, you still need a fallback rule, but you lose much less work than with whole-record Last-Write-Wins.

Use this when several people can edit the same record, or when Last-Write-Wins would throw away too much.

Strategy 3: Delta sync

Delta sync isn't a conflict rule. It decides how much data moves, and it makes the other two cheaper. Instead of downloading everything on reconnect, the client sends the time of its last successful sync, and the server returns only the records changed since then.

Future<List<Note>> pullChanges(Dio dio, DateTime lastSync) async {
  final res = await dio.get(
    '/notes/changes',
    queryParameters: {'since': lastSync.toUtc().toIso8601String()},
  );
  return (res.data as List).map((j) => Note.fromJson(j)).toList();
}
Enter fullscreen mode Exit fullscreen mode

Small payloads mean a reconnect sync feels instant.

The short version

  • Queue offline edits locally with a pendingSync flag, and retry with exponential backoff.
  • Last-Write-Wins (per field if you can) is enough when one user syncs their own devices.
  • Field-level merge keeps both edits when two people changed different fields.
  • Delta sync keeps every reconnect small, whichever rule you pick.

Further reading: Handling Data Synchronization and Conflict Resolution in Offline Apps.

Which rule does your offline app use today, and has it ever lost someone's edit?

Top comments (0)