vector clock example

Vector Clock Tutorial: Mastering Time Synchronization

By David King
AD

A vector clock is a data structure used in distributed computing to represent a point in time. It was introduced by Ken Thompson in his 1965 paper "The Tun текстok with Time Sharing". Vector clocks play a crucial role in keeping the global state consistent across various nodes in a distributed system. Let's dive into the concept, its functioning, and examples to illustrate how vector clocks help in maintaining time consistency.

Imagine a distributed system with multiple nodes working simultaneously. Each node needs to know when certain events occurred to maintain data coherence. Vector clocks provide a mechanism for nodes to record events and deduce causality, answering questions like, "Did event B at node B happen before event A at node A?"

Understanding Vector Clocks

A vector clock is essentially an array of timestamps, where each entry corresponds to a specific node in the distributed system. When an event occurs, the node updates its corresponding timestamp in the vector.

AD
a black and white clock with numbers on the face, which is half past five
a black and white clock with numbers on the face, which is half past five
Clock Time Icon, Clock Icons, Time Icons, Clock Clipart PNG and Vector with Transparent Background for Free Download
Clock Time Icon, Clock Icons, Time Icons, Clock Clipart PNG and Vector with Transparent Background for Free Download
a black and white clock face with numbers on each side, indicating the time is 11 00
a black and white clock face with numbers on each side, indicating the time is 11 00

The beauty of vector clocks lies in their ability to capture event causality. If every node has a vector clock, and every event update includes the sender's vector clock, receiving nodes can compare and determine the chronological order of events.

Vector Clock Structure

A vector clock VC for node N in a distributed system with 'n' nodes is represented as an 'n'-element array VC = , where ti is the number of events observed by node Ni.

RELOJ GATO
RELOJ GATO
Vector Clock Icon Vector, Clock, Time, Timer PNG and Vector with Transparent Background for Free Download
Vector Clock Icon Vector, Clock, Time, Timer PNG and Vector with Transparent Background for Free Download
a clock with red hands and numbers on the face, which is showing eleven o'clock
a clock with red hands and numbers on the face, which is showing eleven o'clock

Initially, all timstamps ti are set to zero, i.e., VC = <0, 0, ..., 0>. Each time a node generates a new event, it increments its corresponding timestamp in the vector clock.

Vector Clock Operations

When a node sends a message to another node, it includes its current vector clock. Upon receiving the message, the receiving node updates its vector clock using the following rules:

AD
  • If ti in the received vector clock is greater than ti in the receiver's vector clock, set ti to the value received.
  • For all j ≠ i, set tj to max(tj, tj received).

The first rule ensures that the receiver knows about all events that the sender knows about. The second rule makes sure that the receiver does not lose any information about events it already knew about before receiving the message.

Mostrador De Relógio - Ilustração Vetorial Ilustração do Vetor - Ilustração de cara, minuto: 78841336
Mostrador De Relógio - Ilustração Vetorial Ilustração do Vetor - Ilustração de cara, minuto: 78841336
a black and white clock face with numbers on each side, showing the time in twelve o'clock
a black and white clock face with numbers on each side, showing the time in twelve o'clock
1n
1n
Clock Face. Blank Hour Dial. Wedges Mark Minutes and Hours Stock Vector - Illustration of business, round: 122589678
Clock Face. Blank Hour Dial. Wedges Mark Minutes and Hours Stock Vector - Illustration of business, round: 122589678
a black and white clock icon with no numbers on the face, hd png
a black and white clock icon with no numbers on the face, hd png
Clock icon vector illustration office clock on white background | Premium Vector
Clock icon vector illustration office clock on white background | Premium Vector

Vector Clock Examples

Let's consider a simple distributed system with three nodes, A, B, and C. Initially, all vector clocks are VC = <0, 0, 0>.

Example 1: Single-message Exchange

Node A sends a message to Node B with its vector clock VC = <1, 0, 0>. After receiving, Node B updates its vector clock to VC = <1, 1, 0>, indicating it's now aware of an event at Node A.

AD

If Node B then sends a message to Node C with its updated vector clock, Node C updates its vector clock as follows: VC = <1, 1, 0> ≥ <0, 0, 0> → VC = <1, 1, 1>. This demonstrates how vector clocks help propagate and capture event causality.

Node C can now determine that an event occurred at both Node A and B before any event at Node C.

Example 2: Multiple-message Exchange

Suppose Node A sends a message to Node B, then Node B sends a message to Node C, and finally Node C sends a message back to Node A. Let's see how the vector clocks evolve:

AD

After each message exchange, the vector clocks will look like this: Starting vector clocks: VC = <0, 0, 0> - After A → B: VC_B = <1, 0, 0> - After B → C: VC_C = <1, 1, 0> - After C → A: VC_A = <1, 1, 1> - After A → B: VC_B = <1, 1, 1>

By comparing these final vector clocks, we can determine the causal relationships: both A and B had events before C, and all nodes had events before the final message exchange.

Vector clocks empower distributed systems to maintain a consistent global state by providing a holistic view of time and causality. Despite their simplicity, they play a vital role in building resilient and robust large-scale applications. To learn more about implementing vector clocks, consider exploring practical tutorials and case studies.

AD