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.
From Physical to Logical Time
Section titled âFrom Physical to Logical Timeâ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.
- Local Order: If X preceded Y within the same process, X must also precede Y in the total order.
- 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) returns4. Process B invokes pop()5. Process B pop() returns 3As 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
completefunction 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) returns4. Process B invokes pop()5. Process B's pop() returns 36. Process A's push(4) returnsNow, 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 33. 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:
- Legality (L1): The sequence follows the LIFO rules of a stack.
- 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 beforepop()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.
Sequential Consistency
Section titled âSequential Consistencyâ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) returns3. Process A invokes push(4)4. Process A's push(4) returns5. Process B invokes pop()6. Process B's pop() returns 3In 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 33. 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
Section titled âThe CAP Theoremâ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:
- A sends a request to S1 to write
x=5. S1 completes the write. - 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.