
A Vector Clock (VC) is a mechanism used in distributed systems to maintain the causal ordering of events. Unlike a Lamport Timestamp, which provides only a partial order of events, vector clocks provide a full causal relationship between events.
Vector clocks are widely used in distributed databases (e.g., Amazon DynamoDB, Apache Cassandra), event logging systems, and version control mechanisms to track changes across multiple nodes while resolving conflicts.
In distributed systems:
Vector clocks help:
A vector clock consists of:
For example, a vector clock for three processes P1, P2, and P3 might look like:
P1: [2, 1, 0]
P2: [1, 3, 1]
P3: [0, 2, 4]
Each index represents a process, and the corresponding number is the event count.
There are three main rules for updating vector clocks:
[0, 0, 0].P1: [1, 0, 0] # P1 performs an event P2: [0, 1, 0] # P2 performs an eventP1 sends a message to P2 P1 updates: [2, 0, 0] P2 receives: [2, 1, 0] # Max(P1, P2) + Increment P2To compare two vector clocks VC1 and VC2, we check each corresponding value:
VC1 = [2, 1, 0]
VC2 = [1, 3, 1]
Since VC1[0] > VC2[0], VC1[1] < VC2[1], and VC1[2] < VC2[2], these events are concurrent.
class VectorClock:
def __init__(self, process_id, num_processes):
self.process_id = process_id
self.clock = [0] * num_processes # Initialize vector clock
def increment(self):
"""Increment the process's own clock."""
self.clock[self.process_id] += 1
def update(self, received_clock):
"""Merge with another vector clock using element-wise maximum."""
for i in range(len(self.clock)):
self.clock[i] = max(self.clock[i], received_clock[i])
def send_message(self):
"""Simulate sending a message."""
self.increment()
return self.clock.copy()
def receive_message(self, received_clock):
"""Simulate receiving a message and updating the vector clock."""
self.increment()
self.update(received_clock)
def __str__(self):
return str(self.clock)
# Example Usage
p1 = VectorClock(0, 3)
p2 = VectorClock(1, 3)
p1.increment() # P1 does an event
print("P1 after event:", p1)
msg = p1.send_message() # P1 sends a message to P2
p2.receive_message(msg) # P2 receives the message
print("P2 after receiving message:", p2)
Output:
P1 after event: [1, 0, 0]
P2 after receiving message: [1, 1, 0]
import java.util.Arrays;
class VectorClock {
private int[] clock;
private int processId;
public VectorClock(int processId, int numProcesses) {
this.processId = processId;
this.clock = new int[numProcesses]; // Initialize vector clock
}
public void increment() {
clock[processId]++; // Increment own process counter
}
public void update(int[] receivedClock) {
for (int i = 0; i < clock.length; i++) {
clock[i] = Math.max(clock[i], receivedClock[i]); // Element-wise maximum
}
}
public int[] sendMessage() {
increment();
return clock.clone();
}
public void receiveMessage(int[] receivedClock) {
increment();
update(receivedClock);
}
@Override
public String toString() {
return Arrays.toString(clock);
}
public static void main(String[] args) {
VectorClock p1 = new VectorClock(0, 3);
VectorClock p2 = new VectorClock(1, 3);
p1.increment();
System.out.println("P1 after event: " + p1);
int[] msg = p1.sendMessage();
p2.receiveMessage(msg);
System.out.println("P2 after receiving message: " + p2);
}
}
| Method | Tracks Causality | Ordering | Complexity |
|---|---|---|---|
| Lamport Timestamps | ❌ No | Partial | O(1) |
| Vector Clocks | ✅ Yes | Total | O(N) |
| Hybrid Logical Clocks | ✅ Yes | Total | O(1) |
O(N), where N is the number of processes).Although vector clocks have higher memory overhead, they remain one of the best tools for managing distributed event ordering. 🚀
When analyzing a stock, one of the first financial indicators you’ll encounter is EPS, or Earnings Per Share. It’s one… Read More
When you look at a stock’s profile on a financial website, one of the first things you’ll see is its… Read More
In the world of open-source software, simplicity and flexibility are often just as important as legal protection. That’s why the… Read More
If you want your software to be open source, but still compatible with commercial use—and not as restrictive as the… Read More
When it comes to open-source software, developers and businesses alike need licenses that balance freedom, legal clarity, and long-term security.… Read More
If you’re working on open-source projects or choosing third-party libraries for your software, understanding software licenses is essential. Among the… Read More