Happened-before explained

In computer science, the happened-before relation (denoted:

\to

) is a relation between the result of two events, such that if one event should happen before another event, the result must reflect that, even if those events are in reality executed out of order (usually to optimize program flow). This involves ordering events based on the potential causal relationship of pairs of events in a concurrent system, especially asynchronous distributed systems. It was formulated by Leslie Lamport.[1]

The happened-before relation is formally defined as the least strict partial order on events such that:

a

and

b

occur on the same process,

a\tob

if the occurrence of event

a

preceded the occurrence of event

b

.

a

is the sending of a message and event

b

is the reception of the message sent in event

a

,

a\tob

.

If two events happen in different isolated processes (that do not exchange messages directly or indirectly via third-party processes), then the two processes are said to be concurrent, that is neither

a\tob

nor

b\toa

is true.[2]

If there are other causal relationships between events in a given system, such as between the creation of a process and its first event, these relationships are also added to the definition.For example, in some programming languages such as Java, C, C++ or Rust, a happens-before edge exists if memory written to by statement A is visible to statement B, that is, if statement A completes its write before statement B starts its read.

Like all strict partial orders, the happened-before relation is transitive, irreflexive (and vacuously, asymmetric), i.e.:

\foralla,b,c

, if

a\tob

and

b\toc

, then

a\toc

(transitivity). This means that for any three events

a,b,c

, if

a

happened before

b

, and

b

happened before

c

, then

a

must have happened before

c

.

\foralla,a\nrightarrowa

(irreflexivity). This means that no event can happen before itself.

\foralla,b,

if

a\tob

then

b\nrightarrowa

(asymmetry). This means that for any two events

a,b

, if

a

happened before

b

then

b

cannot have happened before

a

.

Let us observe that the asymmetry property directly follows from the previous properties: by contradiction, let us suppose that

\foralla,b,

we have

a\tob

and

b\toa

. Then by transitivity we have

a\toa,

which contradicts irreflexivity.

The processes that make up a distributed system have no knowledge of the happened-before relation unless they use a logical clock, like a Lamport clock or a vector clock. This allows one to design algorithms for mutual exclusion, and tasks like debugging or optimising distributed systems.

See also

References

Notes and References

  1. Lamport, Leslie (1978). "Time, Clocks and the Ordering of Events in a Distributed System", Communications of the ACM, 21(7), 558-565.
  2. Web site: Distributed Systems 3rd edition (2017). 2021-03-20. DISTRIBUTED-SYSTEMS.NET. en-US.