ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/projects/mini-dynamo/tasks/dynamo-t3-s1-conflict-detection
DS

Detect Conflicts with Vector Clock Comparison

Mini-Dynamo / Vector Clocks
intermediate

Concept

Causal Ordering with Vector Clocks

The power of vector clocks is that they allow precise causal reasoning. Given two versions of a value — say from two replicas — you can determine exactly one of four relationships.

The Comparison Algorithm

def compare(a, b):
    a_le_b = all(a[n] <= b[n] for n in nodes)
    b_le_a = all(b[n] <= a[n] for n in nodes)

    if a_le_b and b_le_a:
        return "EQUAL"
    elif a_le_b:
        return "BEFORE"
    elif b_le_a:
        return "AFTER"
    else:
        return "CONCURRENT"

Interpreting Results in Dynamo

ResultMeaningAction
BEFOREA is an older version of BDiscard A, keep B
AFTERA is a newer version of BDiscard B, keep A
CONCURRENTGenuine conflictStore both; surface to client
EQUALDuplicate writeKeep either

Concurrent Writes in Dynamo

Concurrent writes happen when two clients write to the same key at roughly the same time via different coordinators. Neither write was aware of the other. Dynamo stores both versions and returns them both to the next reader. The application (or a merge function) then resolves the conflict.

Amazon's shopping cart, for example, merges conflicting carts by taking the union of items — you might see duplicate items, but you will never lose an item due to a conflict.

main.py
python

Sign in to run and submit code.