skip to content

When automatic conflict resolution (like LWW or vector-clock causality checks) cannot safely pick a winner because two writes are truly concurrent and semantically different, systems like Amazon's Dynamo return both versions to the application as 'siblings.' Concretely, how does an application perform a semantic merge on such siblings, using a shopping-cart example, and what does the application have to guarantee for the merge to be safe?

level: seniorimportance: must knowfreq 65%

answer

  1. siblings returned to app
  2. union merge for cart-adds
  3. merge: commutative, associative, idempotent
  4. defers decision to domain code
  5. unresolved siblings pile up

basics

~20 s

When a database can't safely decide which of two conflicting writes is 'right,' it hands both versions to the application, whose code knows what the data means and merges them, like combining two shopping carts instead of losing one cart's items.

solid answer

~50 s

Application-level (semantic) merge is what happens when a store detects a true conflict — via vector clocks or similar — and, instead of guessing, returns all the concurrent 'sibling' versions to the caller and lets domain-aware code produce the merged result. Amazon's Dynamo paper uses the shopping cart: the application merges the divergent cart versions and writes back a version that supersedes them, so an 'add to cart' is never lost; the paper names the cost, that deleted items can resurface. A union of the items is such a merge, where LWW would have dropped one cart. For the merge to be safe, its result must not depend on who merges which versions in which order: clients can merge different subsets of siblings and their results meet again as siblings, so it needs to be commutative, associative and idempotent, and the team has to write and maintain one per data type.

go deeper

for a junior

Should understand that sometimes both conflicting writes are kept and the application's code, not the database, decides how to combine them.

for a middle

Should be able to walk through the cart-union example, explain why picking one write over the other would be wrong there, and say that the store returns every sibling and the merged write supersedes them.

for a senior

Should articulate why a merge must be commutative, associative and idempotent (clients merge different subsets in different orders) and the sibling-accumulation failure mode.

for a principal

Should be able to decide, for a given data model, whether application-level merge, LWW, or a CRDT is the right investment, weighing engineering cost against correctness needs, including what a union merge does to removed items.

## When the store gives up and asks the application Application-level merge is the resolution strategy a system falls back to precisely when automatic detection (vector clocks, version vectors) has correctly identified that two writes are concurrent but has no way to decide which one is 'right,' because rightness depends on what the data means, which only the application knows. Mechanically, the flow looks like this: 1. a client writes value V1 based on some prior version; 2. concurrently, another client writes value V2 based on the same prior version, without seeing V1, through a different replica; 3. when a later read collects versions from replicas, the vector-clock comparison shows neither V1 nor V2 dominates the other, so the store cannot discard either one without risking data loss; 4. rather than picking arbitrarily (which is what LWW would do), the store returns both V1 and V2 to the requesting client, tagged with a merged vector clock that will supersede both once the client writes back a resolved value; 5. the client (or a library embedded in it) runs a merge function specific to that data's shape and writes the merged result, which the causal history then shows as descending from both parents, closing the conflict. ## Why generic resolution cannot decide The reason this exists is that generic conflict-resolution mechanisms — LWW, arbitrary tie-breaking — are correct only when 'newer replaces older' is actually the right semantics for the field in question, and that assumption is false for a large and important class of data: anything representing an accumulation, a set of independent operations, or a structure where two changes can both be legitimate and additive. A shopping cart is the textbook case: 'add item A' and 'add item B' are not two competing readings of the same fact that need a winner, they're two independent operations that should both survive. Generic mechanisms have no way to know this without being told, so the system defers the decision to the one piece of code that does know — the application. ## The trade-off The trade-off is engineering cost and correctness burden shifted onto the application team. Writing a semantic merge function requires understanding the full space of concurrent edits that can occur for that data type and producing a result that does not depend on how the merging happened. Nobody controls that: a read returns whichever siblings the replicas it reached hold, two clients can each merge a different subset, and their merged writes are concurrent with each other, so a later reader merges merges. The merge therefore needs three properties: - **commutative** — merging V1 with V2 gives the same result as merging V2 with V1; - **associative** — merging V1 with V2 and then with V3 gives the same result as merging V1 with the merge of V2 and V3; - **idempotent** — merging a version with itself gives that same version, so, with the other two properties, a version seen twice is not counted twice. A merge with all three is a join, the property a state-based CRDT's merge is built to have; a set union has it. Getting this wrong (a merge that depends on the order it sees versions in, or that counts a version twice) reintroduces the exact class of bugs — silent, order-dependent data loss — that vector clocks were introduced to prevent, just one layer up the stack. In exchange, the payoff is that data that would otherwise be lost under LWW is genuinely preserved, and the resolution logic can encode real business rules ('union of cart items,' 'the larger of two high-water marks,' 'two logs merged by their entries' ids') rather than an arbitrary timestamp comparison. ## Failure modes 1. **Sibling accumulation and client-side complexity creep** — the characteristic failure mode. If clients don't reliably read-then-write (resolving siblings promptly), the number of concurrent sibling versions for a hot key can keep growing, since every write made without the latest read's context can add another concurrent branch, and every client that reads the key has to be prepared to handle an arbitrary number of siblings, not just two. Riak's documentation calls this **sibling explosion**, an object collecting siblings that are not reconciled, and warns that it degrades performance and can cause out-of-memory errors or, with an enormous object, crash the node when the object is read; it recommends always updating within a read/modify/write cycle. 2. **The escape hatch** — a second failure mode: developers reaching for LWW as an escape hatch once merge logic gets complicated, quietly reintroducing the exact silent-loss problem the sibling mechanism was built to avoid, just for the fields where the merge was 'too hard.' ## The canonical example The canonical real-world example remains the shopping cart in Amazon's Dynamo paper (2007): the cart requires that an 'Add to Cart' operation can never be forgotten or rejected, so when versions diverge, the client application reconciles them by merging the different versions of the cart. The paper states the result in both directions: an 'add to cart' is never lost, but deleted items can resurface. The reason is the merge's own: a merge that keeps every item either version holds brings back an item one version had removed. A union of item sets is the simplest merge with that behavior; keeping removals needs them recorded (as tombstones) so the merge can tell 'never added' from 'removed'.

  • What happens if a client only ever reads the latest single value and never handles siblings — does the merge still happen?
    No. Siblings stay in the store until a write supersedes them, and no merge happens unless client code performs one. A client that reads the siblings and writes back a single value with the read's context supersedes them all, so the other versions' changes are dropped as surely as under LWW, only in the client. A client that writes without that context adds yet another sibling. Merging is the application's job in either case.
  • Why does the merge function need to be commutative rather than just 'correct for the common case'?
    Because which client merges which siblings, and in what order, is up to timing: a read returns whichever versions the replicas it reached hold, two clients can merge different subsets, and their merged writes come back as siblings to merge again. If merging A then B differs from B then A, the final value depends on who merged first, and a change can vanish unchosen. Associativity and idempotence matter for the same reason.
  • Is there a way to get application-level-merge-style correctness without writing a custom merge function for every data type?
    Partly. CRDTs are data types built so replicas converge without custom merge code, for well-known shapes such as counters, sets and registers, not arbitrary business rules; how they guarantee it is a topic of its own.

It's like two editors independently adding different footnotes to the same paragraph while offline; instead of an editor-in-chief arbitrarily keeping only one footnote, both are handed back to a human who knows the document's intent and combines them into a single paragraph with both footnotes.

  • Thinks the store automatically merges concurrent writes for you
  • Proposes picking one sibling by timestamp as 'the merge'
  • Doesn't mention merge functions need to be commutative/order-independent
  • Unaware that unresolved siblings can accumulate if clients don't merge promptly
  • Confuses this with automatic CRDT merging

context