Lamport Clocks

Lamport Clocks

Assuming that we cannot achieve accurate clock synchronization - or starting with the goal that our system should not be sensitive to issues with time synchronization, how can we order things?

Lamport clocks and vector clocks are replacements for physical clocks which rely on counters and communication to determine the order of events across a distributed system. These clocks provide a counter that is comparable across different nodes.




A Lamport clock?is simple. Each process maintains a counter using the following rules:

  • Whenever a process does work, increment the counter
  • Whenever a process sends a message, include the counter
  • When a message is received, set the counter to?max(local_counter, received_counter) + 1

Expressed as code:

function LamportClock() {
  this.value = 1;
}

LamportClock.prototype.get = function() {
  return this.value;
}

LamportClock.prototype.increment = function() {
  this.value++;
}

LamportClock.prototype.merge = function(other) {
  this.value = Math.max(this.value, other.value) + 1;
}        

A?Lamport clock?allows counters to be compared across systems, with a caveat: Lamport clocks define a partial order. If?timestamp(a) < timestamp(b):

  • a?may have happened before?b?or
  • a?may be incomparable with?b

This is known as clock consistency condition: if one event comes before another, then that event's logical clock comes before the others. If?a?and?b?are from the same causal history, e.g. either both timestamp values were produced on the same process; or?b?is a response to the message sent in?a?then we know that?a?happened before?b.

Intuitively, this is because a Lamport clock can only carry information about one timeline / history; hence, comparing Lamport timestamps from systems that never communicate with each other may cause concurrent events to appear to be ordered when they are not.

Imagine a system that after an initial period divides into two independent subsystems which never communicate with each other.

For all events in each independent system, if a happened before b, then?ts(a) < ts(b); but if you take two events from the different independent systems (e.g. events that are not causally related) then you cannot say anything meaningful about their relative order. While each part of the system has assigned timestamps to events, those timestamps have no relation to each other. Two events may appear to be ordered even though they are unrelated.

However - and this is still a useful property - from the perspective of a single machine, any message sent with?ts(a)?will receive a response with?ts(b)?which is?> ts(a).




I hope you found the article useful.

Happy Coding :)

要查看或添加评论,请登录

Umang Agarwal的更多文章

  • Push and Pull Configuration Management Tools

    Push and Pull Configuration Management Tools

    Push and pull configuration management tools are software solutions that facilitate the management and distribution of…

    10 条评论
  • What is Configuration Management in DevOps?

    What is Configuration Management in DevOps?

    Configuration management in DevOps refers to the process of managing and controlling the configuration of software…

    3 条评论
  • Principles of Web Distributed Systems Design

    Principles of Web Distributed Systems Design

    Like most things in life, taking the time to plan ahead when building a web service can help in the long run;…

  • What is non-monotonicity good for?

    What is non-monotonicity good for?

    The difference between monotonicity and non-monotonicity is interesting. For example, adding two numbers is monotonic…

  • Does time progress at the same rate everywhere?

    Does time progress at the same rate everywhere?

    We all have an intuitive concept of time based on our own experience as individuals. Unfortunately, that intuitive…

  • Two Phase Commit (2PC)

    Two Phase Commit (2PC)

    Two phase commit (2PC) is a protocol used in many classic relational databases. For example, MySQL Cluster (not to be…

  • Partition and Replication

    Partition and Replication

    The manner in which a data set is distributed between multiple nodes is very important. In order for any computation to…

  • Forward Proxy vs Reverse Proxy Servers

    Forward Proxy vs Reverse Proxy Servers

    Forward Proxy A forward proxy is an intermediary that sits between one or more user devices and the internet. Instead…

  • Remote Procedure Calls (RPC)

    Remote Procedure Calls (RPC)

    In distributed computing, a remote procedure call (RPC) is when a computer program causes a procedure (subroutine) to…

  • Types of NoSQL databases

    Types of NoSQL databases

    NoSQL is a collection of data items represented in a key-value store, document store, wide column store, or a graph…

社区洞察

其他会员也浏览了