Scrivio Interview library
DebuggingIntermediate7 min read · updated 13 November 2026

A shopping cart is stored as one JSON value per user in a multi-region store that resolves conflicts by last write wins. Customers using a phone and a laptop report that removed items come back and that items added on one device vanish.

Explain both symptoms and redesign the cart so concurrent edits merge correctly.

Each device reads the whole cart, edits it and writes the whole JSON value back, and last write wins keeps only the write with the later timestamp. So when two devices write concurrently, one device's whole cart replaces the other's, which drops an item added on the other device, and a stale cart written after a removal puts the removed item back. The fix is to stop storing the cart as one value: store one row or cell per item, choose deliberately what a concurrent add and remove means, or route each cart's writes to one home region.

Why this question is asked

Both symptoms come from one design choice, and interviewers use this question to see whether we can trace a strange user report back to the conflict resolution rule underneath. The rule, last write wins, sounds harmless, but it silently throws away data whenever the unit we write is larger than the unit people edit. In this answer, we will recap how last write wins works, replay both symptoms step by step, redesign the cart so edits to different items cannot overwrite each other, decide what should happen when the same item is added and removed at once, and look at the home-region alternative and how to test the result.

1A Quick Recap of Last Write Wins

In a multi-region store, each region accepts writes locally and replicates them to the others asynchronously, which means the write is acknowledged before the other regions have it. When two regions have written the same key, the store must pick a winner. Last write wins (LWW) picks the write with the larger timestamp and discards the other one completely.

LWW is safe only when a write replaces the whole of the thing and nobody cares about the losing write. For a cart stored as one JSON document, the key is the user and the value is every item in the cart. So LWW resolves conflicts at the level of the whole cart, while people edit one item at a time. That mismatch is the bug.

2Replaying Both Symptoms

Take a cart that holds a book. On the phone the customer adds headphones, and at almost the same moment on the laptop they add a charger. Each device read the cart as [book], added its item and wrote the whole value back: the phone wrote [book, headphones] at 10:00:01.200 and the laptop wrote [book, charger] at 10:00:01.350. The laptop's timestamp is later, so every region ends with [book, charger]. The headphones vanished, even though the store did exactly what it was told.

The second symptom is the same mechanism in a different order. The customer removes the book on the laptop, which writes [charger]. The phone still has the old cart [book, charger] in memory, and when the customer changes the charger quantity there, the phone writes back [book, charger x2] with a later timestamp. The book comes back. Clock skew between devices or regions makes this worse, because the write that was really second can carry the smaller timestamp and lose.

Step throughA whole-cart write hides another device's add
PhoneStore (LWW)Laptop
read cart: [book]
read cart: [book]
write [book, headphones] at t=1.200
write [book, charger] at t=1.350
keep the later write, drop the other
cart is [book, charger]: headphones gone

6 messages in order. Play them, or step through them one at a time.

Both writes were valid; LWW kept one whole cart and discarded the other.

3Redesign 1: One Row per Item

The first and most important change is to make the unit of storage match the unit of editing. Instead of one value per user, we store one row (or one cell in a wide-column store such as Cassandra) per cart item, keyed by (cart_id, item_id), with the quantity and a timestamp on each row. Adding headphones writes the headphones row; adding a charger writes the charger row. Those are different keys, so they never conflict, and both survive.

Quantities change per item as well. A set of the quantity is still LWW, but now only on that one item, which is usually acceptable: if the customer sets the charger to 2 on one device and 3 on another at the same second, keeping one of them is a reasonable outcome. If we want increments from two devices to add up, we can store the quantity as a counter CRDT, a data type whose concurrent updates merge without conflict.

Notice that the client must also stop writing what it did not change. A device sends operations such as add charger or remove book, not a full copy of its possibly stale cart. That alone removes most of the resurrection cases.

SQL
-- before: one value per user, last write wins on the whole cart
cart(user_id PK, items_json)
-- after: one row per item, so different items never overwrite each other
cart_item(cart_id, item_id, quantity, updated_at, PRIMARY KEY (cart_id, item_id))
-- add on the phone and add on the laptop now touch different rows
INSERT ... (cart_id='c1', item_id='headphones', quantity=1) -- kept
INSERT ... (cart_id='c1', item_id='charger', quantity=1) -- kept

4Redesign 2: Choosing What a Concurrent Add and Remove Means

One conflict remains: the same item added on one device and removed on the other at the same time. Per-item rows with LWW would let the later timestamp win, which is arbitrary. We should decide the meaning on purpose.

An observed-remove set (OR-Set) gives each add a unique tag, and a remove deletes only the tags the removing device had seen. A concurrent add carries a tag the remover never saw, so it survives: add wins. For a shopping cart, add wins is usually right, because an extra item the customer can see and delete is better than one that silently disappears. Amazon's Dynamo paper made the same choice for its cart.

The other approach is to make removals record which version they removed, for example remove charger at version 7. If the item has been changed after version 7 by another device, the removal does not apply to the newer change. Both approaches need a little metadata per item, but they turn a random outcome into a rule we can explain to the business.

Cart designs compared
DesignConcurrent addsAdd vs removeCost
One JSON value, LWWone device's add is lostremoved items can returnsimple, wrong
Row per item, LWW per rowboth keptlater timestamp winssmall change, mostly right
Row per item, OR-Setboth keptadd wins, by designtags per add, tombstones
Home region per cartboth kept, orderedone writer orders themcross-region latency

5Alternative: A Home Region and Conditional Updates

Another option is to avoid concurrent writers altogether. Each cart gets a home region, and every write for that cart is routed there, so one place orders all changes and there are no conflicts to merge. The price is latency for customers far from their cart's home, perhaps 80 to 150 ms more per cart change for a traveller, and a plan for when that region is down.

Whichever design we pick, a version number on the cart or on each item lets us use conditional updates where the store supports them, such as a lightweight transaction in Cassandra or a conditional write in DynamoDB: write only if the version is still 7. A device with a stale view then gets a rejection and re-reads instead of overwriting.

Finally, we test it the way it fails. We simulate two devices against two regions, inject replication delays and clock offsets of a few hundred milliseconds, run random adds, removes and quantity changes, and check that every add that was not removed afterwards is present at the end. The original bug would fail this test within seconds.

6Follow-up questions

Why do the symptoms appear only for customers with two devices?

Because a conflict needs two writers to the same cart within the replication window. A single device makes its writes one after another and always reads its own latest cart, so LWW never has two competing values. A phone and a laptop, especially in different regions, are exactly the case of two concurrent writers.

Is per-item LWW enough on its own?

For most carts, yes, because the rare remaining conflict is a set and a remove of the very same item within the same second. If the business cares about that case, an OR-Set or versioned removals make the outcome deliberate instead of depending on clock timestamps.

What about the tombstones an OR-Set needs?

Removals leave small markers so a late replica does not bring the item back. We can purge them once every region has acknowledged them, or after a period longer than the maximum replication delay, for example a day, and carts are short lived anyway.

7Conclusion

In this answer, we traced both cart symptoms to one cause: each device writes the whole cart, and last write wins keeps one whole value and discards the other, so concurrent adds vanish and stale carts resurrect removed items. Storing one row per item, sending operations rather than full carts, choosing add-wins or versioned removals for the one remaining conflict, or giving each cart a home region fixes it, and conditional updates plus a two-device test with injected delays keep it fixed.

What to say in the interviewAbout 90 seconds aloud

Open with

Both symptoms come from storing the cart as one JSON value with last write wins. Each device reads the whole cart, changes it locally and writes the whole thing back.

Then, if they want more

If the phone and the laptop write within the replication window, the store keeps the write with the later timestamp and throws the other away entirely, so an item added on the losing device disappears. And if a device holding an old copy writes after a removal, its stale cart wins and the removed item comes back.

Clock skew makes the winner even more arbitrary. My redesign starts by making the unit of storage match the unit of editing: one row per cart item keyed by cart id and item id, and clients send operations like add or remove item rather than full carts. Then adds of different items never conflict.

For the same item added and removed concurrently, I would pick the meaning on purpose, usually add wins with an observed-remove set, or removals that record the version they removed. If the store supports it, I would add a version and use conditional writes so stale devices get rejected.

The alternative is a home region per cart, which removes conflicts at the cost of latency for travellers. I would prove it with a test of two simulated devices and injected network delays.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain both symptoms and redesign the cart so concurrent edits merge correctly.": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/8ffbbd0a0c/