[LF] Causality
This is a series of notes on topics related to Local-First software.
Problem: Ordering events in a distributed system is challenging due to potential message reordering in the network. This can lead to confusing situations where a reply is received before the original message.
Example: User C receives reply m2
before the initial message m1
, making it seem like B predicted A’s statement. This can cause issues, especially with ordered operations like database updates.
Naive Approach (Timestamps): Attaching timestamps from local clocks to messages seems like a solution, but clock synchronization (even with NTP) has limitations and residual skew. This can lead to incorrect ordering based on timestamps.
Happens-Before Relation: A formal way to define the “correct” order of events, capturing causality. Assumes each node has a single thread of execution with a strict local order.
Definition of Happens-Before (a → b):
- Local Order: If events
a
andb
occur at the same node anda
happens beforeb
in that node’s execution. - Message Passing: If event
a
is sending a messagem
, and eventb
is the receipt of the same messagem
(assuming unique messages). - Transitivity: If there exists an event
c
such thata → c
andc → b
.
Partial Order: The happens-before relation is a partial order, meaning that for some events a
and b
, neither a → b
nor b → a
might be true.
Concurrency (a || b): If neither a → b
nor b → a
, then events a
and b
are considered concurrent, implying no causal dependency through message exchange.
Causality:
- Borrowed from physics (relativity).
- If
a → b
, thena
might have causedb
(potential causality). - If
a || b
, thena
cannot have causedb
. - The happens-before relation encodes potential causality.
Causal Order: A strict total order ≺
on events is a causal order if (a → b) ⇒ (a ≺ b)
.