~learn.
0:00 / 1:25
W05 · Consistency and coordination

Two people like a post at once. The count goes up by one.

transcript

Two people like your post at the same moment. The count goes up by one. A like just vanished.

The counter is at 100. User A reads 100. User B reads 100 too, on another node. Both add one. Both write 101. Last write wins. Final value: 101. Expected 102.

Better timestamps? Both wrote 101. There's nothing to pick between. Keep the larger one? Still 101. The problem is one shared number.

So don't share one number. Give each node its own counter. Node A counts its like. Node B counts its like. Nobody overwrites anybody. The value is the sum. 102. Both likes survive.

Here's the twist. When replicas merge, they don't add the counts. Add them, and receiving the same state twice counts it twice. Take the max of each node's count instead. Merge twice, nothing changes.

This is a CRDT. Merge A with B, or B with A. Same result. Group the merges any way you like. Same result. Merge the same state again? Nothing changes. So every replica converges.

Concurrent increments to one number lose updates. Give every node its own count. Sum to read. Max to merge.

Full breakdown in the lecture.