Logical Clock in Distributed System


In a distributed system, multiple machines are working together, and each machine may have its own clock. still, these clocks may not be accompanied with each other, and there's no single clock that can be used to order events globally. Logical clocks give a way to handle this by assigning each event a logical timestamp, which can be used to order events and establish reason between them, indeed if they do on different machines. In substance, logical clocks give a way to produce a virtual global timepiece that's consistent across all machines in a distributed system.

Scalar Time Implementation

There are different methods for implementing logical clocks in a distributed system, but one of the simplest is Scalar Time. In this implementation, each process keeps a local clock that is initially set to 0. There are two rules for updating the local clock −

Rule 1: Event Execution

Before executing an event (excluding the event of receiving a message), increment the local clock by 1.

local_clock = local_clock + 1

Rule 2: Message Reception

When receiving a message (the message must include the sender's local clock value), set your local clock to the maximum of the received clock value and the local clock value. After this, increment your local clock by 1.

local_clock = max(local_clock, received_clock)
local_clock = local_clock + 1

This ensures that events are ordered similarly so that if event A contributes to event B being, the timestamp of event A is lower than the timestamp of event B.

Example

For illustration, there are 3 processes in a distributed system P1, P2, and P3. However, the message will include the original timepiece value of P1, If an event occurs in P1 and also a message is transferred to P2. When P2 receives the message, it'll modernize its own original timepiece using Rule 2, icing that events are ordered rightly.

Timestamp Implementation

Another method for implementing logical clocks is using timestamps. Each event is assigned a timestamp, and events are ordered based on the causality relationship (i.e., the "happened before" relationship) between the events. However, timestamps only work as long as they obey causality.

Example

For illustration, suppose we have a distributed system with multiple processes, and process P1 sends a communication to process P2. However, the reason relationship is violated and the ordering of events is no longer consistent, If the timestamp of the communication from P1 is lesser than the timestamp of an earlier communication from P2.

Causality in Distributed Systems

In a distributed system, causality is still based on the happen-before relationship, just like in a single PC system. If event A occurs in process P1 and event B occurs in process P2, we still need to establish which event occurred first to maintain causality. In other words, we need to determine the order of events across different processes.

Timestamps and Causality

To do this, we can use logical clocks, which assign a timestamp to each event. The rule is still the same if event A causes event B, also the timestamp of A should be lower than the timestamp of B.

Example

For illustration, consider a distributed system where process P1 sends a communication to process P2. The transferring event in P1 has a timestamp of 1, and the entering event in P2 has a timestamp of 2. This indicates that the transferring event passed before the entering event, and we can establish an unproductive relationship between them.

thus, the reason is important in logical timepieces in distributed systems to maintain the order of events and establish a cause-and-effect relationship between them.

Conclusion

logical clocks provide a way to maintain the consistent ordering of events in a distributed system. Scalar Time is a simple implementation that uses local clocks and two rules to ensure correct order. Other methods, such as using timestamps, can also be used but must obey causality to work properly. By using logical clocks, processes in a distributed system can work together in an organized and efficient way.

Updated on: 03-May-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements