A clock that says "the time is between here and here", and a database that waits out the doubt.
TrueTime is Google's clock API for its global database Spanner. Instead of saying "it is 12:00:00.005", it says "the real time is somewhere between 12:00:00.001 and 12:00:00.009". It is honest about the doubt.
No clock is exact, so most systems ignore the error and hope. TrueTime does the opposite: it measures the error and returns it. The call TT.now() returns an interval [earliest, latest] that is guaranteed to contain the real time. Two helper calls are TT.after(t) (is t surely in the past?) and TT.before(t) (is t surely in the future?).
To keep the interval small, Google puts GPS receivers and atomic clocks in every data centre, and each machine syncs from several time masters every 30 seconds. Between syncs the uncertainty grows slowly (assuming up to 200 microseconds of drift per second), so Ξ΅ is a sawtooth, usually 1 to 7 ms.
A transaction needs a commit timestamp s. Spanner picks s = TT.now().latest, which is a time no earlier than real now. Then it waits until TT.after(s) is true, which means s is certainly in the past. Only then does it release the locks and tell the client "committed".
| Call | Returns | Meaning |
|---|---|---|
| TT.now() | [earliest, latest] | The real time is definitely inside this interval |
| TT.after(t) | true or false | True if t has definitely passed (t < earliest) |
| TT.before(t) | true or false | True if t has definitely not arrived yet (t > latest) |
| Ξ΅ (epsilon) | (latest β earliest) / 2 | The uncertainty. Typically 1 to 7 ms |
| Approach | How order is decided | Cost |
|---|---|---|
| Plain wall clocks | Each server stamps its own clock | Can be wrong by the clock error |
| Hybrid logical clock | Wall time plus a counter, carried in messages | Causal order only, not real-time order |
| TrueTime | Latest of the interval, then wait until it is past | Special hardware and a few ms wait per write |
| Central timestamp server | One machine hands out numbers | A bottleneck and a single point of failure |
A simplified model of a Spanner write. Real Spanner picks the timestamp at the leader when the commit starts, and overlaps the wait with the replication it needs anyway. Google reports an average Ξ΅ of a few milliseconds (GPS and atomic clocks in each data centre).
| Term | Meaning in easy words |
|---|---|
| TrueTime | Google's clock API that returns a time interval |
| TT.now() | Returns [earliest, latest], which contains the real time |
| TT.after(t) | True if time t has definitely passed |
| Epsilon (Ξ΅) | Half the width of the interval: the clock uncertainty |
| Commit wait | Waiting until the chosen commit timestamp is surely in the past |
| External consistency | If T1 finishes before T2 starts, T1 has the smaller timestamp, everywhere |
| Strict serializability | Transactions behave as if run one by one, in real-time order |
| Time master | A machine with GPS or an atomic clock that serves time to others |
| Snapshot read | A lock-free read of the data as of a chosen timestamp |
| Paxos group | The replicas of one Spanner shard, which agree on each write |
| Clock error bound | A promised limit on how wrong a clock can be |
| Choice | We gain | We lose |
|---|---|---|
| TrueTime | External consistency, global reads | Hardware cost and commit wait |
| Hybrid logical clock | No special hardware, no wait | Only causal order |
| Smaller Ξ΅ | Shorter commit wait, faster writes | More GPS and atomic clock gear and sync traffic |
| Larger Ξ΅ (cheap clocks) | Cheap and easy | Slow writes, long waits |
| Read at a past timestamp | Never blocks on writes | Data a few seconds old |
| Strong reads | Always the newest data | May wait for the latest commits |