Google Spanner TrueTime and Commit-Wait
Google Spanner’s TrueTime API represents the current time as an interval rather than a single timestamp, and commit-wait uses that interval to make transaction commit timestamps agree with real-time ordering across regions.
The problem: clocks disagree
A distributed database cannot assume that two machines observe the same time. Even well-synchronized clocks differ because of network delay, oscillator drift, software delays, and failures in the time-synchronization system.
Suppose a transaction commits in one region, and then a client starts another transaction in a different region. If the second region’s clock is behind, it might assign the second transaction an earlier timestamp. A database using timestamps for ordering could then produce an order that contradicts what users observed:
Transaction A finished before transaction B began, but the database ordered B before A.
Spanner calls the desired property external consistency: if one transaction completes before another starts, the commit timestamp of the first must be earlier than the commit timestamp of the second. This is stronger than merely making concurrent transactions appear in some consistent order.
Relying on an ordinary clock reading cannot provide that guarantee. Spanner instead needs to know not just its best estimate of the time, but how wrong that estimate could be.
TrueTime returns an interval
TrueTime is Spanner’s time API. Its central operation, commonly written as TT.now(), returns an interval:
[earliest, latest]
The actual current time is guaranteed to lie somewhere inside that interval. TrueTime does not claim to know the exact instant. It claims that the uncertainty is bounded.
For example:
TT.now() = [12:00:00.100, 12:00:00.104]
The machine estimates that the time is around 12:00:00.102, but it promises only that the real time is between .100 and .104. The interval’s width is the uncertainty, often represented by epsilon:
epsilon = latest - earliest
TrueTime combines several time sources, including GPS receivers and atomic clocks, and continuously accounts for their known error. The important abstraction is the guarantee, not the hardware: if t is earlier than earliest, then t definitely occurred in the past; if t is later than latest, then it definitely lies in the future.
The API therefore supports questions such as:
TT.now(): what interval contains the current time?TT.after(t): has time definitely advanced beyondt?TT.before(t): istdefinitely still in the future?
Those predicates are useful precisely because they wait for uncertainty to stop mattering. A call to TT.after(t) is true only when the lower bound of the current interval is greater than t.
What commit-wait does
When Spanner commits a read-write transaction, its coordinator chooses a commit timestamp s. The timestamp must be at least as late as the coordinator’s known upper bound at the relevant point:
s >= TT.now().latest
That makes s a time that is certainly not earlier than the transaction’s commit decision, even if the local clock estimate is wrong.
Spanner then performs commit-wait: it waits until TrueTime can prove that real time has passed s.
Conceptually, the condition is:
wait until TT.now().earliest > s
At that point, the entire current interval is later than s. The commit timestamp is no longer inside the uncertainty window. Spanner can safely make the commit visible and acknowledge it to the client.
The wait is approximately the current uncertainty bound, not an arbitrary fixed delay. If TrueTime’s uncertainty is four milliseconds, the required wait is on that order; if uncertainty grows during a time-service problem, commit-wait grows too.
Why this orders regions correctly
Imagine transaction A commits with timestamp s in region 1. Spanner does not report A as committed until the real time is definitely later than s.
Now a client starts transaction B after observing A’s successful completion, perhaps through a different region. When B obtains its start timestamp, real time is already later than s. TrueTime’s lower bound at B’s start will therefore eventually be greater than s, and B cannot receive a timestamp earlier than A’s commit timestamp.
The guarantee does not come from making every machine’s clock identical. It comes from combining two facts:
- TrueTime bounds how far a clock reading may be from reality.
- Spanner waits until the chosen commit timestamp is definitely in the past before exposing the commit.
Thus a transaction that is externally later cannot be assigned an earlier timestamp merely because it ran on a machine with a slightly different clock.
The cost of the guarantee
Commit-wait adds latency to write commits. Under normal conditions, the cost tracks TrueTime’s uncertainty bound. This is the trade-off: Spanner gets externally consistent ordering across geographically separated replicas by waiting out the period in which clock uncertainty could reverse the order.
If the time service loses its required accuracy bound, TrueTime cannot safely claim a narrow interval. Spanner must reflect that larger uncertainty in its waits, or stop making progress rather than silently violate the ordering guarantee. This is why an incident involving time synchronization can appear as increased commit latency or stalled writes.
TrueTime is therefore more than a precise clock API. It is a contract about uncertainty. Commit-wait turns that contract into a database ordering guarantee: once Spanner says a transaction committed, any transaction that starts afterward can be assigned a later timestamp, even when the two transactions run in different regions.