## DEV Community

Sriram R

Posted on • Originally published at Medium

# The Notion of Logical Time

In the previous two articles, we learned about Physical Time and the nuances of time.

We concluded the thought by attempting to synchronise two clocks, but this presents a new challenge.

The very idea of synchronising time means that when a drift is found, time can jump forward or backward depending on the drift.

Consider the following scenario:
![[Screenshot 2023-02-19 at 7.47.25 PM.png]]

We wanted to calculate the time it takes to execute a function, but NTP intervenes and pushes time back.
Now, when we figure out how much time has passed, startTime is greater than endTime, so the amount of time that has passed is negative.

This is a significant issue because many critical usecases rely on calculating the time elapsed between two events.

1. Use of timeouts
2. TTL expiration
3. Retry Timers
4. Performance Evaluation, and so on.

## Logical Time

Looking at the usecase, all we need to know is how long something took. When we think about it, it has nothing to do with the exact date and time. We only need something like 'This function took 100ms to execute'.

As a result, we created Logical Time. The time measured from an arbitrary point is known as logical time. In a computer, for example, we can consider booting up to be an initial action with time 'T=0' and increment this by one every second.

This time does not indicate the date or time. It only knows that X time has passed since something happened.

This concept, it turns out, is very useful in detecting time differences between two points because there is no concept of time drift or skew here. This time can only go up and never down.

We also discussed how to order events in Message Ordering and how Physical Time is ineffective for determining the order of two messages.

Because logical time is only incremental, we can deduce that Event A occurs before Event B if Event A's logical timestamp is less than Event B's.

Logical Time can also be used to detect Causality between two messages.

There are two major algorithms of Logical Time

1. Lamport Clocks
2. Vector Clocks

Both clocks will be examined in depth in subsequent articles.