How it works
Two clients can read one version, change it differently, and write nearly together. Last-write-wins keeps the highest timestamp; it is simple but can discard an accepted update, especially when clocks disagree. Compare-and-set, which writes only if the version is still the one you read, rejects a stale edit. Another design keeps siblings, meaning both concurrent values, for the application to reconcile. A preference, a cart, and a balance require different policies.
A vector clock detects which case you have. It holds one counter per replica, counting the writes that replica coordinated. Take replicas X and Y and a cart written through X, giving clock {X:1}. Alice reads it, adds a book through X, and produces {X:2}. Bob read the same {X:1}, adds a lamp through Y, and produces {X:1, Y:1}. Compare counter by counter: the first is ahead on X and the second is ahead on Y, so neither includes the other. They are concurrent siblings. Had Bob read {X:2} first, his write would be {X:2, Y:1}, at least as high everywhere, so it could safely replace its ancestor.
The next reader receives both carts, merges them into one with the book and the lamp, and writes back through X as {X:3, Y:1}. Amazon’s Dynamo paper describes this style of cart merge. Metadata size, tombstones, retries, and client logic add cost, and a plain union can bring back an item someone removed. A vector clock detects conflict; it cannot decide the business outcome.
Two offline devices edit an account balance. Should the store silently retain the newest wall-clock value?
No. Starting from $100, one device withdraws $30 and the other $50. Last-write-wins stores $70 or $50 and loses a withdrawal; the real result is $20. Record each change as an immutable ledger entry with a unique ID and compute the balance by summing entries, so both survive and a retry cannot count twice. If the balance must never go negative, an offline device cannot approve a withdrawal: the check has to run in one place, such as a database transaction that reads the balance and inserts the entry atomically, and offline devices can only queue requests.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Concurrent writes can both be accepted without a single correct timestamp winner.
- Causal metadata detects conflicts; domain rules resolve them.