TrueTime API in Spanner

Learn about the TrueTime API methods and how it ensures external consistency.

TrueTime is a highly available distributed clock service. With TrueTime, applications can produce timestamps that increase monotonically. For example, if the generation of the timestamp TT ends before that of the timestamp TT' begins, then the resultant timestamp TT'will be bigger than the previous timestamp TT (with in an error bound). This is guaranteed for all servers and at any given time.

Spanner assigns timestamps to each transaction using the TrueTime API. The timestamp is the exact time Spanner considered the transaction to commit. Spanner's multi-version concurrency control guarantees the ordering of transactions on timestamps. It allows clients to execute consistent reads over the database, even when spread across different cloud regions, without blocking writes.

Let's dive into the details of the TrueTime API.

TrueTime API methods

As per Spanner's published paperCorbett, James C., Jeffrey Dean, Michael Epstein, Andrew Fikes, Christopher Frost, Jeffrey John Furman, Sanjay Ghemawat et al. "Spanner: Google’s globally distributed database." ACM Transactions on Computer Systems (TOCS) 31, no. 3 (2013): 1-22., the methods that the TrueTime API allows are as follows:

TrueTime API Methods

Method

Returns

TT.now()

TTinterval: [earliest, latest]

TT.after(t)

True, if t has definitely passed

TT.before(t)

True, if t has definitely not arrived

The argument t is of the TTstamp type. TrueTime reports an interval of time, TTinterval, instead of an exact timestamp. The interval with bounded time uncertainty is represented directly in TrueTime. All endpoints of TTinterval must be of the TTstamp type. Upon calling TT.now(), we'll get back a TTinterval (earliest and latest values) that includes the precise moment that we want. The values are the earliest possible and latest possible timestamps. As per Spanner's published paperCorbett, James C., Jeffrey Dean, Michael Epstein, Andrew Fikes, Christopher Frost, Jeffrey John Furman, Sanjay Ghemawat et al. "Spanner: Google’s globally distributed database." ACM Transactions on Computer Systems (TOCS) 31, no. 3 (2013): 1-22., the time epoch is analogous to the UNIX time epoch with leap-second smearing. ϵ\epsilon represents instantaneous error bound, and ...

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.