Module 13 / Replication

Module 13 · lesson 8 of 9 · Read 9 min

Resolving conflicts

Last write wins, merges and CRDTs.

The last two lessons ended with the same problem. In multi-leader replication, two leaders can each accept a change to the same record. In leaderless replication, two clients can write the same key and replicas receive the writes in different orders. Either way, you end up with two versions and no obvious winner.

Every replicated system that allows this needs a rule for what happens next. The rule has to be deterministic: every replica must reach the same answer on its own, or they'll disagree forever. This lesson walks through the options, from the simplest (and most dangerous) to the cleverest.

A conflict in Tiffin

Tiffin has a "group order" feature: a family shares one cart. Asha and Ravi both have it open on their phones. At the same moment, Asha adds paneer tikka and Ravi adds garlic naan. Their writes land on different replicas.

Ashacart is dal, paneer tikka
→
Replica 1
Ravicart is dal, garlic naan
→
Replica 2
Both started from a cart with just dal. Each replica now holds a different cart. Which is right?

Obviously, the right answer is "dal, paneer tikka, garlic naan". But the database doesn't know what a cart is. It sees two versions of the value for key cart:7781, and has to pick, or combine, or ask.