What happens when two disconnected databases both think they are in charge.
In a distributed database, a network failure can sever communication between two data centers. If both sides continue accepting writes, you have a Split-Brain. They diverge. User A updates their name on Node 1, and User A updates their age on Node 2.
When the network heals, the database must Reconcile the divergence. If it uses Last-Write-Wins (LWW), one of the updates is permanently lost (Data Loss). If it uses CRDTs (Conflict-free Replicated Data Types) or a smart merge, it can safely combine the state.
# Scenario: Network partition.
# East gets: UPDATE age = 31 (Timestamp: 10:01)
# West gets: UPDATE name = "Alicia" (Timestamp: 10:02)
# BAD: Last-Write-Wins (LWW) at the Row Level
def reconcile_lww(row_east, row_west):
if row_east.timestamp > row_west.timestamp:
return row_east
return row_west
# Result: West wins. Name is "Alicia", but age reverts to 30! (DATA LOSS)
# GOOD: Field-Level Merge / CRDTs
def reconcile_merge(row_east, row_west):
return {
"name": lww(row_east.name, row_west.name),
"age": lww(row_east.age, row_west.age)
}
# Result: Name is "Alicia", Age is 31. State safely merged!