Physical and Logical Time: Closing Thoughts

Let's conclude the time and order chapters with a clear understanding of the difference between physical and logical time and what each one offers.

Hopefully, the two previous chapters helped us understand the difference between the concepts of physical and logical time. At the same time, some parts went into detail to explain the inner workings of some techniques and their benefits and pitfalls. This might have left us with more questions, so this lesson will contain an overview of what we have seen and some additional observations.

Let’s start by reviewing the basic difference between physical and logical time.

Difference between physical and logical time

Physical time

Physical time is measured based on some physical phenomena. Depending on the phenomenon that is observed, the granularity of physical time can differ from days to seconds or nanoseconds.

In all cases, the time flows continually between discrete values.

Time can be considered discrete in the context of hardware or software systems. Whether this is true in other contexts (i.e., physics) is a much bigger question and is out of topic for this discussion.

  • In a non-distributed system, a computing node can use these measurements to associate occurring events and compare them with each other to determine which happened first.

  • In a distributed system, the various nodes have separate clocks that might not be in sync, so they will have to do additional work to ensure the values from their clocks can be compared safely. This additional work involves exchanging messages between nodes that contain the values of their clocks so that nodes can adjust their clocks accordingly to synchronize them.

Logical time

Logical time is not measured based on physical phenomena, but instead uses local events to measure logical time.

For instance, the node can maintain a counter that is incremented every time something happens. In this way, every event can still be associated with a discrete “moment in time” but the value of this time will be most relative and not absolute.

Furthermore, logical time will not flow continuously, but will only flow when events happen at a node. The node will still be able to compare local events and determine which one happened first.

As mentioned before, these instances of time do not have any absolute semantics. As a result, in a distributed system, the various nodes will have to exchange their perception of logical time so that they can compare the values of their logical clocks and be able to order events across different nodes (and clocks).

Points to be noted

After reading the difference between physical and logical time, did you notice anything interesting?

In the beginning, the concepts of physical and logical time do seem really different. However, a closer examination reveals they are quite similar and actually share some basic properties.

In both cases, time flows in discrete increments every time something happens.

  • In the case of physical time, what happens is the underlying physical phenomenon.
  • In the case of logical time it’s the actual logical event that happens in a node.

In both cases, communication is required between nodes to synchronize their clocks.

  • In the case of physical time, this communication is performed in the background continuously.
  • In the case of logical time, this communication is performed on-demand when messages are sent between nodes.

Identifying that these two notions make use of these common, basic principles but in a slightly different way is useful to understand the advantages and disadvantages of each concept and where each one can be useful.

Advantages and disadvantages of physical time

By its nature, physical time attempts to perform all the necessary coordination and communication in the background so that the various nodes can establish order without additional coordination when needed.

In most cases, physical clocks are used in order to provide the illusion of a total order, which is not realistic in a distributed system.

Furthermore, physical clocks need to remain properly synchronized, which might not be temporarily possible under network partitions. If the clock drift between nodes is larger than the acceptable error, it might mean the correctness of the application is compromised.

Advantages and disadvantages of logical time

Logical time operates under the assumption that network partitions are given, and a partial order satisfying causality is the best kind of ordering one can achieve in a distributed system. During partitions, different nodes can keep operating while essentially leaving their clock drift.

However, when partitions are healed, or nodes communicate with each other, they can eventually detect things that happened concurrently and perform the necessary reconciliation.

Logical clocks can also be adapted so that only necessary causal relationships are captured (instead of everything), thus achieving both good performance and safety.

However, as explained previously logical time does not flow on its own as physical time does; it only flows when events happen. Consequently, logical time cannot be used for tasks that require a notion of wall clock time that flows even when no events happen. For instance, to identify whether another node is slow during communication or has potentially crashed, a node needs to use timeouts and physical time.

Consider a distributed e-commerce platform with servers located in different regions. Users interact with the platform to browse products, add items to their carts, and make purchases. The system aims to maintain accurate timestamps for user activities.

Describe how the platform can face challenges related to physical and logical time in this distributed environment. Provide specific examples of scenarios where physical time synchronization and logical time concepts, such as vector clocks or Lamport clocks, play crucial roles. Explain how these timekeeping mechanisms can address the challenges and ensure consistency in recording user interactions across the distributed servers.

Address time challenges in a distributed environment, both physically and logically.

Get hands-on with 1400+ tech skills courses.