Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Logical Clock in Distributed System
Logical clocks in distributed systems provide a way to order events across multiple machines that do not share synchronized physical clocks. Since each machine may have its own local clock running at different rates, logical clocks assign timestamps to events based on causality rather than actual time, creating a consistent virtual global timepiece across all machines in the system.
The Need for Logical Clocks
In distributed systems, establishing the order of events is crucial for maintaining consistency. Physical clocks on different machines may drift or run at different speeds, making it impossible to rely on wall-clock time for event ordering. Logical clocks solve this by using the "happened-before" relationship to determine event causality.
Lamport's Scalar Clock Implementation
The most common implementation uses Lamport's Scalar Clock algorithm. Each process maintains a local logical clock initialized to 0, following two fundamental rules:
Rule 1: Local Event Execution
Before executing any local event (excluding message reception), increment the local clock by 1.
local_clock = local_clock + 1 execute_event()
Rule 2: Message Reception
When receiving a message containing the sender's timestamp, update the local clock to maintain causality:
local_clock = max(local_clock, received_timestamp) local_clock = local_clock + 1 process_message()
Example Execution
Consider three processes P1, P2, and P3 communicating in a distributed system:
Step-by-Step Execution
| Event | Process | Action | Clock Update | Final Clock |
|---|---|---|---|---|
| a | P1 | Local event | 0 + 1 | 1 |
| b | P2 | Local event | 0 + 1 | 1 |
| c | P1 | Send message | 1 + 1 | 2 |
| d | P2 | Receive message | max(1, 2) + 1 | 3 |
| e | P3 | Local event | 0 + 1 | 1 |
| f | P2 | Send message | 3 + 1 | 4 |
| g | P3 | Receive message | max(1, 4) + 1 | 5 |
Properties of Logical Clocks
Logical clocks ensure the clock condition: if event A happened-before event B, then the timestamp of A is less than the timestamp of B. However, the reverse is not necessarily true a smaller timestamp does not guarantee causality.
Causality preservation Events that have a causal relationship are correctly ordered
Partial ordering Only causally related events are ordered; concurrent events may have arbitrary ordering
Efficiency Requires only integer counters and simple arithmetic operations
Limitations and Extensions
While Lamport clocks provide event ordering, they cannot detect concurrent events. Vector clocks extend this concept by using arrays of logical timestamps to capture the full causality information and identify concurrent events in distributed systems.
Conclusion
Logical clocks provide an essential mechanism for maintaining event ordering in distributed systems without relying on synchronized physical clocks. Lamport's scalar clock algorithm uses simple rules to ensure causally related events are properly ordered, enabling consistent coordination across distributed processes.
