📝 tldr;
Google uses a highly available and globally distributed clock called “TrueTime”.
TrueTime doesn’t tell you the exact time, but gives you a range which is guaranteed to contain the absolute time.
TrueTime is the key enabler for Google Spanner - the first ever database to provide consistent SQL reads at the global scale of NoSQL databases!
This is a follow up article after introducing Google’s spanner database
🙋♀️ Credit to our Contributing Author
Bhavana wrote this amazing article to share with our 19,000+ readers! If you’re also interested in being a byte-sized design writer, consider applying here!
Bhavana Hindupur is a Principal Software Engineer at Microsoft. She brings experience from her tenure at tech giants Google and Amazon, where she designed and implemented several large-scale solutions in the cloud.
Read more from her at thepeoplessoftwareengineer
📚 Introduction to TrueTime
In distributed systems, telling the exact time is tricky.
Every computer has its own clock, but differences in the underlying hardware and external factors like temperature cause each of these clocks to report a slightly different time.
TrueTime is backed by Google’s robust hardware - multiple modern clock references (GPS and atomic clocks).
But TrueTime doesn’t pretend to know the exact time - it intentionally exposes clock uncertainty.
🤨 What does that mean?
Normally when you ask a clock what time it is, it will tell you the exact time like:
“The time is exactly 3.45PM”.
But when you ask TrueTime the time using its TT.now() API, it will tell you time interval like:
“The time is between 3.44PM and 3.46PM.”
This time interval is guaranteed to contain the absolute time at which TT.now() was executed.
(In reality, the bounds of this interval are around 8-10 milliseconds.)
The two ends of the time range returned by true time are represented by TT.now().earliest and TT.now().latest.
⏰ TrueTime powers Spanner
Spanner uses TrueTime to “correctly” order its write transactions. That means if transaction T1 commits before transaction T2 starts, Spanner guarantees that T1 has an earlier timestamp than T2.
Vocab: This is also known as “global wall clock ordering”.
This allows Spanner to support consistent reads across an entire database (across 🌎 globally distributed shards) without blocking writes.
All by simply pulling the read from a timestamped-snapshot.
Keep reading with a 7-day free trial
Subscribe to Byte-Sized Design to keep reading this post and get 7 days of free access to the full post archives.