The Lamport timestamp algorithm is a simple logical clock algorithm used to determine the order of events in a distributed computer system. As different nodes or processes will typically not be perfectly synchronized, this algorithm is used to provide a partial ordering of events with minimal overhead, and conceptually provide a starting point for the more advanced vector clock method. The algorithm is named after its creator, Leslie Lamport. Distributed algorithms such as resource synchronization often depend on some method of ordering events to function. For example, consider a system with two processes and a disk. The processes send messages to each other, and also send messages to the disk requesting access. The disk grants access in the order the messages were received. For example process A {\displaystyle A} sends a message to the disk requesting write access, and then sends a read instruction message to process B {\displaystyle B} . Process B {\displaystyle B} receives the message, and as a result sends its own read request message to the disk. If there is a timing delay causing the disk to receive both messages at the same time, it can determine which message happened-before the other: A {\displaystyle A} happens-before B {\displaystyle B} if one can get from A {\displaystyle A} to B {\displaystyle B} by a sequence of moves of two types: moving forward while remaining in the same process, and following a message from its sending to its reception. A logical clock algorithm provides a mechanism to determine facts about the order of such events. Note that if two events happen in different processes that do not exchange messages directly or indirectly via third-party processes, then we say that the two processes are concurrent, that is, nothing can be said about the ordering of the two events. Lamport invented a simple mechanism by which the happened-before ordering can be captured numerically. A Lamport logical clock is a numerical software counter value maintained in each process. Conceptually, this logical clock can be thought of as a clock that only has meaning in relation to messages moving between processes. When a process receives a message, it re-synchronizes its logical clock with that sender. The above-mentioned vector clock is a generalization of the idea into the context of an arbitrary number of parallel, independent processes.
Algorithm The algorithm follows some simple rules:
A process increments its counter before each local event (e.g., message sending event); When a process sends a message, it includes its counter value with the message after executing step 1; On receiving a message, the counter of the recipient is updated, if necessary, to the greater of its current counter and the timestamp in the received message. The counter is then incremented by 1 before the message is considered received. In pseudocode, the algorithm for sending is:
# event is known time = time + 1; # event happens send(message, time);
The algorithm for receiving a message is:
(message, timestamp) = receive(); time = max(timestamp, time) + 1;
Considerations For every two different events a {\displaystyle a} and b {\displaystyle b} occurring in the same process, and C ( x ) {\displaystyle C(x)} being the timestamp for a certain event x {\displaystyle x} , it is necessary that C ( a ) {\displaystyle C(a)} never equals C ( b ) {\displaystyle C(b)} . Therefore it is necessary that:
The logical clock be set so that there is a minimum of one clock "tick" (increment of the counter) between events a {\displaystyle a} and b {\displaystyle b} ; In a multi-process or multi-threaded environment, it might be necessary to attach the process ID (PID) or any other unique ID to the timestamp so that it is possible to differentiate between events a {\displaystyle a} and b {\displaystyle b} which may occur simultaneously in different processes.
Causal ordering For any two events, a {\displaystyle a} and b {\displaystyle b} , if there is any way that a {\displaystyle a} could have influenced b {\displaystyle b} , then the Lamport timestamp of a {\displaystyle a} will be less than the Lamport timestamp of b {\displaystyle b} . It’s also possible to have two events where we can’t say which came first; when that happens, it means that they couldn’t have affected each other. If a {\displaystyle a} and b {\displaystyle b} can’t have any effect on each other, then it doesn’t matter which one came first.
… excerpt ends here. Continue reading the full article.
