Skip to content

Conflict resolution

What a replicated store does when two replicas accept writes to the same item at once: pick a winner by timestamp, keep both versions for the application to merge, use a mergeable data type, or avoid conflicts by routing writes.

On this page

What it is

A write conflict is two changes to the same item made on different replicas, neither of which saw the other. It can arise wherever more than one node accepts writes: a table replicated across regions with writers in each, a leaderless store that takes a write on any replica, or a client that edits offline and syncs later. The classic framing asks two questions. When to resolve: at write time, which may mean refusing a write while a majority of replicas is unreachable, or at read time, so writes are accepted and divergent versions are reconciled later. And who resolves: the store, which is limited to simple rules such as last write wins, or the application, which knows what the data means and can merge it.

Last writer wins (LWW). Each write carries a timestamp and the highest one survives; the other concurrent write is discarded from the stored item. DynamoDB global tables in their default multi-Region eventual consistency mode resolve a conflicting item by its latest internal timestamp, and all replicas converge to that version. Cassandra 5.0 timestamps each mutation, deletes included, using the client's clock or, if none is supplied, the coordinator's, and its documentation warns that correctness depends on those clocks being kept in sync with something like NTP. It also resolves per column rather than per row, so two regions updating different columns of the same row both keep their change. Azure Cosmos DB with multiple write regions uses LWW by default on its _ts property, lets the NoSQL API substitute a numeric property of your choosing, and resolves a delete against a concurrent insert or replace in favor of the delete regardless of that value.

Keep all versions and let the application merge. Instead of guessing, the store keeps the parallel versions and hands them to a reader. In the Dynamo design each version carries a vector clock, a list of (node, counter) pairs. If one clock's counters are all less than or equal to the other's, the first version is an ancestor and the store drops it on its own. If neither dominates, the versions are concurrent: a read returns all of them with a context, the client merges them and writes back with that context, and the branches collapse into one new version. The shopping cart was the motivating case: a merge that unions the carts keeps each add, but an item removed on one branch can reappear. CouchDB 3.5 does the same with a revision tree. After replication both revisions exist, a plain read shows a winner picked by a deterministic rule so all peers show the same one, conflicts=true lists the others, and the application resolves by writing a merged document and deleting the losing leaves.

Data types that merge themselves. A conflict-free replicated data type (CRDT) defines how two versions combine, so replicas converge without the application choosing a winner; what survives a concurrent update depends on the rule the type defines. Cassandra's documentation describes each row as a last-write-wins element set, itself a CRDT, and states that its map and set collection types resolve concurrent updates conflict-free. A related option is a merge you write yourself: a Cosmos DB container with the custom policy runs a registered merge stored procedure when it detects a conflict, and if the procedure is missing or throws, the conflicting versions are written to a conflicts feed for the application to resolve by hand.

When to use it

Plan for conflicts whenever your design lets more than one place accept writes to the same key: an active-active deployment where each region writes locally, a leaderless store that accepts a write at any replica and repairs later, or clients that work offline and sync. Then say which rule applies to which data. Our advice: LWW for fields where the newest value is the whole truth, such as a display name or a sensor reading, once you've decided that dropping an older concurrent edit is acceptable there. Siblings or a CRDT for anything built up by many small changes, such as a cart, a tag set or a counter, because LWW on the whole value would throw away one side's changes. A custom merge when a business rule names the winner, such as the higher bid or the latest approved status.

When not to

With a single leader per key there is no write conflict to resolve: one node orders all writes, and two clients racing on the same row is a concurrency-control problem handled with a version check or a lock. CouchDB on one node shows the shape: a PUT must carry the _rev it read, and if that revision has been superseded the update is refused with 409 Conflict for the client to retry. DynamoDB global tables offer the same trade in their multi-Region strong consistency mode: a write to an item being modified in another Region fails with ReplicatedWriteConflictException and can be retried, at the price of cross-Region round trips on each write and strongly consistent read.

Don't reach for siblings when there is no sensible merge: two people claiming the same username, or two edits to the same free-text paragraph, have no union, and showing both versions to a user is a product decision rather than a storage one. And be wary of LWW on a plain value that is updated by increments. If two regions each add one to a counter stored as a single value, the surviving write holds one increment and the other is gone, so take that trade on purpose or store the counter as a mergeable type.

Compared

ApproachHow the winner is chosenWhat can be lostPick it when

Last writer wins

Highest timestamp per item, or per column

The other concurrent write is discarded from the item; skewed clocks can keep the older one

Each write carries the whole value (a name, a reading) and dropping a concurrent edit is acceptable

Siblings to the application

The store keeps all branches; the app merges on read and writes back

Nothing, if the merge keeps all; a removed element can resurface

The domain has a natural merge (cart, tag set) and you can afford merge code on the read path

Mergeable data type (CRDT)

The type's own merge rule, defined so that replicas converge

Depends on the type: an LWW element still drops a concurrent write, and a set's add/remove policy picks a side

Updates are increments or set adds and no human should be in the loop

Custom merge procedure

Your code, run by the store when it detects the conflict

Whatever your code drops; failures land in a conflicts feed

A business rule names the winner and the store supports server-side merge

Avoid the conflict

One leader per key, which refuses a write carrying a stale version

Write availability: a write can be refused or wait while its leader is unreachable, and a stale write is refused and must retry

Correctness beats write availability (balances, inventory, unique names)

In a picture

replicatereplicateLWWsiblingsWriter in region AReplica A: v2aWriter in region BReplica B: v2bSame item, v2aand v2bKeep highesttimestampReturn both tothe appApp writesmerged v3
Two replicas accept writes to the same item; when replication brings the versions together, the store either keeps the highest timestamp or hands both to the application, which writes back a merged version.

How to say it in an interview

The common mistake

Quiz

Two regions each add a different item to the same cart, stored as one value under last writer wins, within the same second. What does the cart hold once replication settles?
A read returns two sibling versions of an item. Who combines them?
For which data is last writer wins a reasonable choice?
Which design removes the conflict instead of resolving it after the fact?

Questions that use this

Related

Sources

  1. All Things Distributed: Dynamo: Amazon's Highly Available Key-value Store
  2. Amazon DynamoDB Developer Guide: How DynamoDB global tables work
  3. Apache Cassandra 5.0 documentation: Dynamo (architecture)
  4. Apache CouchDB 3.5 documentation: Replication and conflict model
  5. Azure Cosmos DB: Conflict types and resolution policies when using multiple write regions
Sign in to report a mistake