Vector Clock is an algorithm that generates partial ordering of events and detects causality violations in a distributed system. These clocks expand on Scalar time to facilitate a casually consistent view of the distributed system, they detect whether a contributed event has caused another event in the distributed system. It essentially captures all the casual relationships. This algorithm helps us label every process with a vector(a list of integers) with an integer for each local clock of every process within the system. So for N given processes, there will be vector/ array of size N.
How does the vector clock algorithm work :
- Initially, all the clocks are set to zero.
- Every time, an Internal event occurs in a process, the value of the processes’s logical clock in the vector is incremented by 1
- Also, every time a process sends a message, the value of the processes’s logical clock in the vector is incremented by 1.
Every time, a process receives a message, the value of the processes’s logical clock in the vector is incremented by 1, and moreover, each element is updated by taking the maximum of the value in its own vector clock and the value in the vector in the received message (for every element).
Consider a process (P) with a vector size N for each process: the above set of rules mentioned are to be executed by the vector clock:
The above example depicts the vector clocks mechanism in which the vector clocks are updated after execution of internal events, the arrows indicate how the values of vectors are sent in between the processes (P1, P2, P3).
To sum up, Vector clocks algorithms are used in distributed systems to provide a casually consistent ordering of events but the entire Vector is sent to each process for every message sent, in order to keep the vector clocks in sync.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.