# Logical Clock in Distributed System

Many machines collaborate in a distributed system; every machine could have a unique clock. However, these clocks might not be synchronized, meaning that no clock can be utilized to arrange occurrences in a worldwide order. A method to deal with this is to provide an appropriate date for each event using reasonable clocks. This allows events to be arranged logically and their relationship established, even if they occur on different machines. Logical clocks, in essence, provide a means of generating a virtual global wristwatch compatible with every machine in a distributed system.

By putting a set of rules in place on each machine in a distributed system, you can ensure that events are ordered consistently over an imaginary period. This is known as a logic clock. In a distributed system, a logical clock is a tool for recording causal and temporal linkages. Since a distributed system might not have a technically synchronized world clock, data from various processes in the system can be globally ordered using a logical clock.

## Implementing Scalar Time

While many ways to create mathematical clocks in distributed frameworks exist, the scalar clock is one of the simplest ways. In this approach each process maintains a local clock which is originally set to 0. To update the current clock, there are two requirements:

### Rule 1: Conduct of the Event

Add one to the global time before acting (apart from the act of getting a notification).

`Local time + 1 equals local time.`

### Rule 2: Receiving Messages

Set your clock at the highest receiving clock number and receive the local time value whenever you get an email (the message must contain the sender's global clock value). Next, add one second to your local time.

`max(local_clock, received_clock) = local_clocklocal time + 1 equals local time.`

This guarantees that all occurrences are arranged comparably, meaning that if event A causes another event, event A's timestamp will be later than event B's.

Example

For example, consider a distributed system with three processes: P1, P2, and P3. However, if an event takes place in P1 and a message is also transmitted to P2, the communication will also contain the initial wristwatch value of P1. After receiving the notification, P2 will use Rule 2 to update its initial watch and ensure that the occurrences are correct.

## Implementing Timestamps

Using dates and times is yet another method to build logical clocks. After every instance is given a timestamp, they are arranged in chronological order according to the causal relationship—that is, the "occurred before" relationship—between them. But timestamps are only useful if they follow the laws of causation.

Example

For example, imagine we have a distributed system with multiple processes, and process P1 sends a message to process P2. If, however, the date and time of a message from P1 is smaller than the time of an earlier message from P2, then the reason connection fails, and the sequence of occurrences is not more coherent.

## Distributed Systems Causality

Like a single PC system, determinism in a distributed system is still predicated on the happen-before link. To ensure cause and effect, even if event A happens in process P1 and event B happens in procedure P2, we must determine which happened first. Stated otherwise, we must ascertain the sequence of various systems' events.

## Timings and Causation

Logical timepieces, which provide a date for every occurrence, can accomplish this. If event A leads to event B, the criterion remains the same; additionally, the date and hour for A must be less than the date for B.

Example

For example, let us look at a distributed network where process P1 communicates with process P2. The timestamps for the downloading and accessing events in P1 and P2, respectively, are 1. We can conclude that there is an unproductive link between them because this shows that the transference event happened before the entry event.

Consequently, the explanation is vital in network logic clocks to preserve a sequence of occurrences and build a causal connection among them.

## Conclusion

Logical clocks provide a means of preserving the consistent event sequence of a distributed system. A straightforward approach known as "scalar time" uses two principles and local clocks to guarantee proper ordering. Other techniques, such as timestamps, are also applicable but require causation to function correctly. Activities in a distributed system can cooperate in an organized, efficient manner through the use of logic clocks.