Skip to content

Time and Consistency in Distributed Systems

This article is a brief introduction to two important topics in distributed systems: time and consistency.

First, we will look at why time is tricky in distributed systems. Since machines do not share a single global clock, it is not always obvious which event happened before another. This leads us to logical clocks, which give us a way to reason about the ordering of events without relying on perfectly synchronized physical time.

Then, we will discuss how we define and verify the correctness of distributed systems through consistency models like linearizability and sequential consistency, and connect this discussion to the CAP theorem.


When reasoning about distributed systems, it is helpful to know the order of events.

Let’s say Process A executed W and X, and Process B executed Y and Z. We want to know the total order of these events, a single-file sequence such as W → X → Y → Z. (Here, “processes” don’t necessarily share memory or run on the same machine. Think of them as nodes in a distributed system.)

Finding the order of events within a single process or a node is usually trivial as it follows some local timeline. On the other hand, finding the order of events across different processes is not so straightforward. If we have an event from Process A and an event from Process B, how can we tell if one preceded the other?

If information traveled at the speed of light without network delays, or if the processes had access to a universal clock as a reference, this wouldn’t be a problem. But in reality, it is quite hard to get all processes to agree on the timing of an event.

So, we decide to ignore physical timestamps and use a notion of “logical” time that is easier to work with. In Lamport’s logical clocks, only two pieces of information are preserved.

  1. Local Order: If X preceded Y within the same process, X must also precede Y in the total order.
  2. Causality: If X from Process A caused Y from Process B, the causality must be preserved. Causality is established by Process A sending a message to Process B. If X and Y from different processes are not causally linked, they are considered concurrent and can be ordered in any way.

Lamport’s clock guarantees that if event A caused event B, then timestamp(A) < timestamp(B). However, the reverse is not guaranteed. If timestamp(A) < timestamp(B), it does not necessarily mean that A caused B. This means that just by looking at Lamport timestamps, we cannot always tell whether two events are causally related or concurrent.

Vector clocks extend Lamport’s clock by keeping a separate logical timestamp for each process. This allows the system to distinguish between events that are causally related and events that happened concurrently.


Linearizability: A Correctness Condition for Distributed Systems

Section titled “Linearizability: A Correctness Condition for Distributed Systems”

In distributed systems, correctness is often discussed in terms of consistency.

Let’s say we write x=5 to a system. In a perfect world, immediately after we finish the write, any subsequent read should return 5. Linearizability is the strongest consistency condition that effectively guarantees this behavior. It ensures that once an operation completes and returns, its results are visible to everyone else. In weaker consistency models, it might be considered acceptable for a subsequent read to return a stale value for some time.


Let’s say we have an abstract data type object, a stack for example, shared by multiple processes. A stack supports two operations: a push and a pop. We model the system’s execution as a history of events (H), where each event is an interval with an invocation and a response.

This means operations aren’t atomic; when a push/pop is invoked, it takes time before the operation finishes and returns. But when we order these events, we want to collapse each interval to a single point. We say that any point within the interval can be the linearization point. This point is the exact moment we say the operation “took effect”. As we will see, being able to choose an arbitrary point within the interval gives us the freedom to build a sensible order of events.

Let’s say we observed the following H.

1. Process A invokes push(3)
2. Process A invokes push(4)
3. Process A push(3) returns
4. Process B invokes pop()
5. Process B pop() returns 3

As we can see, H might have pending invocations, which are requests that have not yet returned a response. To reason about these, the next step is to obtain complete(H').

Here, H' is an extension of H where we can decide the fate of the pending operations:

  • Extend: We can add matching responses to pending invocations if we believe the operation actually took effect.
  • Drop: The complete function then drops any remaining pending invocations that we chose not to extend.

In other words, this allows us to choose whether a pending operation “happened” or not. If we don’t have a reason to believe the operation took effect, we can just drop it. In our example, it doesn’t really matter either way, so let’s extend Process A’s push(4) with a matching response.

1. Process A invokes push(3)
2. Process A invokes push(4)
3. Process A's push(3) returns
4. Process B invokes pop()
5. Process B's pop() returns 3
6. Process A's push(4) returns

Now, we can finally evaluate whether complete(H') is linearizable. Based on our earlier discussion about linearization points, we can construct a timeline like the one below.

1. Process A invokes push(3)
2. Process B invokes pop() and receives 3
3. Process A invokes push(4)

Notice that we are allowed to say that push(4) took effect after pop, even if push(4) was invoked first. This history is linearizable because we were able to construct a timeline that satisfies two conditions:

  1. Legality (L1): The sequence follows the LIFO rules of a stack.
  2. Real-Time Order (L2): If one operation finishes before another operation begins, that order must be preserved in our total order; because push(3) returned before pop() was invoked, push(3) correctly appears first in our total order.

If we are forced to break the rules of the stack (L1) or ignore the real-time order of events (L2) to build a sequence that “makes sense,” the history is not linearizable. We say that a system is linearizable if every history it produces is linearizable.


Initially, I was not really sure what to make of this textbook definition of linearizability.

A helpful clarification is that everything we have discussed so far, including the seemingly arbitrary ordering of events to create a “correct” story, is from the client-side perspective. The history given above is an external observation of client requests and responses. It follows the real-time order in which operations were invoked and returned, but the system itself may not have access to this full global history.

Linearizability is a way to reason about correctness from the outside. We are not saying that the system literally receives a full history and moves operations around until it finds a valid ordering. Internally, the system could be using a task queue, locking, replication protocols, consensus, or something else entirely. Linearizability only asks whether the behavior we observe can be explained as if each operation took effect at some single point between its invocation and response.

The practical takeaway is that linearizability is the strongest consistency model because it does not allow stale reads once an operation has completed. After an operation takes effect at its linearization point, all later operations must reflect that change.


Now, let’s take a look at an example of a weaker consistency model: sequential consistency. Consider the following history:

1. Process A invokes push(3)
2. Process A's push(3) returns
3. Process A invokes push(4)
4. Process A's push(4) returns
5. Process B invokes pop()
6. Process B's pop() returns 3

In this history, push(4) strictly preceded pop. Because a stack should return the most recently pushed value, a linearizable system would require B’s pop to return 4 or break the real-time order. Therefore, this history is not linearizable.

However, in a sequential consistency model, we only require that the order of events within the same process be preserved. This gives us the freedom to reorder events across different processes to create a sensible story. We can generate a timeline like this.

1. Process A invokes push(3)
2. Process B invokes pop() and receives 3
3. Process A invokes push(4)

While this sequence ignores the fact that push(4) strictly preceded pop, it is valid under sequential consistency because Process A’s internal order of events is still preserved.


The CAP Theorem states that a distributed system cannot simultaneously achieve Consistency (Linearizability), Availability, and Partition Tolerance. Informally, this means that only two of these properties can be achieved at the same time.

  • Availability: Every request to a non-failing node must eventually result in a response.
  • Partition Tolerance: The system must continue to operate even when the network is partitioned and processes cannot communicate.

Theoretically, we could choose Linearizability and Availability, and sacrifice Partition Tolerance. However, such a system assumes a perfect network and simply cannot operate correctly when a network failure occurs. Network failures are an inevitable fact of life, which often leaves us to sacrifice either Linearizability or Availability.

Consider a partition-tolerant system with two servers, S1 and S2. Due to a network partition, they can no longer talk to each other. Let’s say:

  1. A sends a request to S1 to write x=5. S1 completes the write.
  2. B sends a request to S2 to read x.

Because of the partition, S2 has no way of knowing that S1 just updated x. It faces a choice:

  • Sacrifice Linearizability: S2 can respond immediately with its local (outdated) value.
  • Sacrifice Availability: S2 can refuse to respond or wait indefinitely for the partition to heal, fearing its local value is outdated.

The CAP Theorem has forced real-world distributed systems to make significant architectural trade-offs. IIn practice, many large-scale systems prioritizes Availability over Linearizability. For many global-scale services, it is more valuable for the system to remain “up” and responsive, even if it occasionally returns a stale value. This reality has led to the emergence of weaker consistency models like eventual consistency.