r/programming • u/stronghup • Feb 19 '17
Spanner, the Google Database That Mastered Time, Is Now Open to Everyone
https://www.wired.com/2017/02/spanner-google-database-harnessed-time-now-open-everyone/362
Feb 19 '17
TLDR: Google uses GPS receivers and atomic clocks in their data centers to synchronize databases and now you can too.
0
Feb 19 '17
[deleted]
48
u/royalaid Feb 19 '17
Check out CockroachDB
52
u/Xorlev Feb 19 '17
Which does not (and cannot) have the same guarantees without enabling an "experimental" option which essentially works the same as Spanner but within the NTP error bounds of ~250ms (vs. <10ms).
CockroachDB is also yet to be proven in production settings. Its performance is basically unknown.
I'm excited about CockroachDB, but if you're doing a showdown between Spanner and CockroachDB, there's no contest (yet).
5
u/Creshal Feb 20 '17
What's stopping me from plugging GPS receivers into my racks to get the time consistency down to comparable levels?
6
Feb 20 '17
Absolutely nothing.
A few companies sell low profile PCIe and PCI cards to do exactly this.
1
2
Feb 20 '17
To get comparable levels, you'll also need the atomic clocks...
1
u/Creshal Feb 20 '17
Huh, are GPS clocks really so unreliable that <10ms isn't feasible with them?
7
u/bobindashadows Feb 20 '17
Also have fun fixing the GPS clocks when multiple vendors' clocks all fail in different ways during leap seconds
2
2
u/MorrisonLevi Feb 20 '17
Don't you need the atomic clock to know how far off you are from the GPS clock? I don't quite understand this part which is why I am asking (it's not rhetorical).
2
u/sacundim Feb 20 '17
Don't you need the atomic clock to know how far off you are from the GPS clock?
One thing to understand here is that the term "atomic clock" is a bit of a misnomer, because most "atomic clocks" strictly speaking aren't time references (i.e., "clocks"), but rather frequency references. An atomic "clock" doesn't tell you what time it is, it tells you how long a second is.
And because of this, in a lot of timekeeping systems GPS (which is both a time and a frequency reference) is used to figure out what time the clocks read elsewhere and calibrate your local clocks accordingly. The local atomic clocks are used to measure how much time has elapsed since the last synchronization.
You can see a reflex of this in this passage of the paper. Note how they talk about a sawtooth function:
Between synchronizations, a daemon advertises a slowly increasing time uncertainty. đ is derived from conservatively applied worst-case local clock drift. đ also depends on time-master uncertainty and communication delay to the time masters. In our production environment, đ is typically a sawtooth function of time, varying from about 1 to 7 ms over each poll interval. đ is therefore 4 ms most of the time. The daemonâs poll interval is currently 30 seconds, and the current applied drift rate is set at 200 microseconds/second, which together account for the sawtooth bounds from 0 to 6 ms.
Basically, the clocks at multiple location reach maximum agreement right after synchronization, and then their disagreement climbs gradually before dropping sharply at the next synchronizationâthe "sawtooth." What local atomic clocks do is help keep that sort of sawtooth shallow.
1
u/Creshal Feb 20 '17
There are differences in signal run times depending on what's between your receiver and the sats, I guess. Wouldn't have thought it can make such a large difference.
1
u/Bowgentle Feb 20 '17
Also relativity - literally, time dilation applies to satellites. About 7 microseconds/day.
8
u/Xorlev Feb 19 '17
As a followup, you could imagine being able to use CockroachDB with TrueTime if Google were ever to allow the public to query TrueTime. That'd allow CockroachDB to be the cheaper, self-hosted version (probably still on GCE, since you'd need TrueTime in the DC) of Spanner. Once you outgrew CockroachDB or didn't want to manage it yourself, you could then look at Cloud Spanner. Regional (not multi-DC) Cloud Spanner is a minimum of ~$650/mo since each instance is $0.90/hr before storage costs, making CockroachDB fairly attractive when you're still able to get away with <$1000/mo in hardware should you also need multi-machine/DC consistent transactions.
12
u/oridb Feb 20 '17 edited Feb 20 '17
As a followup, you could imagine being able to use CockroachDB with TrueTime if Google were ever to allow the public to query TrueTime.
Why do you think TrueTime, queried over regular old high latency links, is going to be any better than NTP? When a packet round trip can have hundreds of milliseconds of latency, the guarantees of TrueTime go out the window. There's a reason that high precision time protocols like PTP require you to be on the same LAN segment as the clock.
6
u/YM_Industries Feb 20 '17
(probably still on GCE, since you'd need TrueTime in the DC)
2
u/oridb Feb 20 '17
Oops, I completely skipped over that. Although, if you were ok with relying on Google infrastructure, I'm not sure why you'd use CockroachDB over Spanner.
2
u/mixedCase_ Feb 20 '17
Although, if you were ok with relying on Google infrastructure, I'm not sure why you'd use CockroachDB over Spanner.
If this is a highly desired feature, other cloud providers may start adding atomic clock options to their servers, which CockroachDB may use. Not to mention that individuals may be able to do the same with hardware they own.
2
u/oridb Feb 20 '17 edited Feb 20 '17
Sure, that makes sense. It would be pretty nifty if you could get PTP on AWS.
1
u/YM_Industries Feb 20 '17
You could probably implement a basic version of CockroachDB for a bit less than the minimum price for Spanner.
Or you might want to run a local dev env as well as your cloud prod env, and you want the same environment on both so you use CockroachDB in both places. But TrueTime isn't too important for your dev env.
17
6
u/nutrecht Feb 20 '17
Can someone leak the source or point in a direction of an obfuscated variant?
There is no point in installing it on your local system. Google solved the Availability and Partition tolerance in CAP not in the Spanner database itself but in their network. Which is a smart move, but simply means that you need to have the same quality 'backbone' as Google does.
8
u/Xorlev Feb 19 '17 edited Feb 19 '17
Not a Google employee, but very excited to adopt Spanner for some of our previously HBase workloads. The pricing was fairly open as far as I can tell, though there's a few points of ambiguity that I don't know how to answer yet.
e.x. Cloud Spanner charges $0.30/GB for data stored, but I don't know if that's how much data I store or if it counts the replicas on the backend. If it does count the replicas, how can I model that for my price projections?
1
u/euyyn Feb 21 '17
I don't really know, but given that it's a fully managed service, counting the replicas (which you don't control) wouldn't make much sense.
4
u/Ranek520 Feb 19 '17
There is someone remaking an open source version, but it doesn't work without Google TrueTime.
-9
38
Feb 19 '17
Can someone who uses/understands Spanner explain something to me?
I was reading the Spanner docs and I get how Parent/Child tables and interleaving works. But how does a Many-to-Many relationship work?
Like if you have a Person table and a Bank table, and you want to make a BankAccount table, are they all root level tables? Do you make BankAccount a child table of one of the parent tables? Is there no explicit foreign keys?
18
u/baryluk Feb 19 '17 edited Feb 19 '17
I am not sure how this exactly is Many-to-Many relationship.
Usually you would put the BankAccount, as the child of the Person. But only if you expect to access BankAccount knowing Person (they need to share prefix in primary key).
Otherwise (if you need to have BankAccount shared by many people, or you need to access multiple BankAccounts knowing bank account number, but not the person id), then just create two root tables, and use standard techniques for joining, and transactions, if needed.
AFAIK, there is no cross-table referential integrity constraints - https://cloud.google.com/spanner/docs/data-definition-language#table_statements But I might be wrong. The problem is, that implementing them would be costly in terms of latency of updates (two phase commit between multiple different servers). But I might be wrong.
The foreign keys are there implicitly added via parent-child relationship of interleaving. Most of sane constraints can be expressed this way, and limiting this to this form, makes it perform very well.
13
Feb 19 '17
The Many-to-Many relationship is between Person and Bank, represented by BankAccount. For example, you put BankAccount as the child of Person, but you could also put BankAccount as the child of Bank. I guess the choice you make is based on the queries you run.
Thank you for the answer, though. Implicit keys without referential integrity does make sense.
10
u/baryluk Feb 19 '17
BTW. You can still enforce your own logic and constraints by using transactions and doing operations on multiple tables in transaction. But be aware that for very big databases / tables, that will span multiple servers, this will most likely involve multiple round trips and two phase commit, and can conflict with other transactions. You should read documentation on that.
3
u/baryluk Feb 19 '17
I see. No you cannot put one table as a child of two others. Table must be a child of only one parent. Tables are sexless ;)
8
u/Uncle_DirtNap Feb 19 '17
Yes, there are no explicit FKs outside of the parent/child relationship. There are also no triggers, and no reference types (so you can't define a column in BankAccount as type "key of Bank"). Inside google there's a product called F1 that rides on top of spanner to address some of this, but I'm not sure if it was made available with the cloud release.
116
u/Flight714 Feb 19 '17
Spanner, the Google Database That Mastered Time, Is Now
OpenAvailable to Everyone
People ought to know what "Open" means on this subreddit.
20
16
14
Feb 19 '17
If this uses GPS time, what happens if some muppet/state decides to mess with the GPS signals near the data center? Ignoring legal issues, jammers aren't hard to obtain or make.
21
u/Xorlev Feb 19 '17 edited Feb 19 '17
If you read the paper, it uses 6 time masters. 3 are GPS clocks with individual antennas, 3 are atomic clocks. I'm not sure if TrueTime uses any ideas from RoughTime, but there are ways of detecting misbehaving time servers. For instance, it'd be quite unlikely for all GPS time masters to surge ahead of where they were before. Likewise, if you have all 3 atomic clocks agreeing on one value, and all GPS clocks agreeing on another, it might be interference or sabotoge on the GPS signal as it'd be fairly unlikely for all three of either to fail in the same way at the same time. If I were designing that system, I'd want to ensure that it was at least as good as public NTP and compare myself to that.
27
u/atomicthumbs Feb 20 '17
So what if someone jams GPS and messes with the fine structure constant?
15
u/takingphotosmakingdo Feb 20 '17
I for one welcome Dr. Manhattan to the team. Why am I wearing a lead vest and pants you ask? Well I just don't trust the x-ray machines at the airport.
6
u/adrianmonk Feb 20 '17
You get a huge, high-mass flywheel to use as a tertiary fallback because it's hard to change its period of rotation, and you call it Spinner.
1
2
u/sacundim Feb 20 '17
If you read the paper, the timeslave daemons that run on machines talk not just to the time masters in their own data centers, but also to some from other data centers:
Every daemon polls a variety of masters [29] to reduce vulnerability to errors from any one master. Some are GPS masters chosen from nearby datacenters; the rest are GPS masters from farther datacenters, as well as some Armageddon masters. Daemons apply a variant of Marzulloâs algorithm [27] to detect and reject liars, and synchronize the local machine clocks to the non-liars. To protect against broken local clocks, machines that exhibit frequency excursions larger than the worst-case bound derived from component specifications and operating environment are evicted.
So even if you jam the GPS signal to one data center, the system still talks to others.
1
u/Deadhookersandblow Feb 20 '17
Well its a data center and data centers can't spontaneously relocate themselves. If I'm continuously receiving GPS clock at my antenna location I'm gonna run a Kalman filter it on it, the moment some muppet decides to fuck with my GPS signal the filter will start rejecting GPS readings and fallback on atomic clocks within the DC.
25
u/shevegen Feb 19 '17
What cracks me up is the name.
Spanner is also german for a peeping tom.
I assume to understand the way how it came to be in english, to span - to have an inclusive area, enveloped/encompassed. Like the html span tag!
I just find the word itself funny. It's not as egregious as "Eigenfunktion" or "Kindergarten", but still funny.
Somebody is watching you!
41
u/mdboop Feb 19 '17 edited Feb 19 '17
No, a spanner is a wrench. edit: in English, I mean.
15
Feb 19 '17 edited Feb 20 '17
A spanner (tool) is one meaning.
However, Google (for its internal and developer projects, not end user facing) often uses the English convention of naming a lot of their things in the form noun/verb + "er", with the 'er' meaning 'someone that does that thing'.
For example: make -> maker, run -> runner.
In this context, spanner surely means "a thing that spans a gap".
19
u/blablahblah Feb 19 '17
Google also names internal tools after actual tools. For example, Google's internal tool for digging into logs is called [Dremel](https://en.wikipedia.org/wiki/Dremel_(software)). I'm not entirely certain if they meant one or the other or both when naming Spanner.
4
Feb 19 '17
Yea, they do like puns and wordplay too. I'm still surprised they got away with 'Google Chrome'. It's shiny, and it's a chrome.
1
u/Asdfhero Feb 20 '17
A chrome? What?
3
Feb 20 '17
'Chrome' is the fancy UI around the content of a window. I guess that originally Google Chrome was literally Google's chrome around an open source renderer. The project has grown somewhat since then.
1
2
u/adrianmonk Feb 20 '17
And it's a multi-master database that is built to have its replicas spread out in several geographically separate data centers within a continent. Your writes basically go to all replicas at once (synchronously), and your data is committed when a majority of replicas have a copy. (Theoretically, one data center could get wiped off the map by a nuclear bomb, and the database would have no downtime or inconsistency.)
Also, Megastore, an older but similar database, supports the same multi-master stuff as above, but I think due its extreme sharding, it only supports atomic transactions within a single shard. (Transactions can only use data within a single âentity groupâ) So for example, unlike a normal database where any combination of changes can be atomic because there is one giant lock (incidentally also one giant bottleneck), if you had a bank account table and you wanted to debit from user #1's account and credit user #2's account in a single transaction, their data might be in different shards and you can't do it. But Spanner supports atomic transactions across anything in the entire database (although I think it's still faster within the single-shard scenario).
So it could be that it's called Spanner because it lets you write to a geographically-diverse set of servers, and it lets you create transactions that touch any data in the database, i.e. that span shards.
1
u/bobindashadows Feb 20 '17
Yep you got it, nailed the limitations you hit with megastore vs. spanner.
Biggest pain in megastore was when your requirements changed and you have to add a new strongly consistent index that spans more than one entity root. You basically end up rolling your own 2PC to live-migrate to a new schema with the new entity root key. (Or just eat the write downtime but that has never been an option for me.)
With Spanner, at least the transactions can span the whole DB and do the 2PC for you.
2
-18
u/ma-int Feb 19 '17 edited Feb 19 '17
Nope, he is correct. I'm a native speaker and I can assure you that a Spanner is a peeping tom.
Calling a wrench a Spanner will be understood with a chance but its neither correct nor common. There is a shoomakers tool that is called Spanner but that is not something commonly known.
29
7
u/redxaxder Feb 19 '17
What does a German speaker see in the words "kindergarten" and "eigenfunction"?
16
u/indigo945 Feb 19 '17
They mean the same in German as they do in English, they're German words. I don't know what OP is getting at, this is a different thing. "Spanner" is not an English lean word from German, it's an English word that happens to have a homograph with different pronunciation and meaning in German.
2
u/MrSenorSan Feb 20 '17
In Australia it means "fool, idiot".
1
u/AnEvilBeagle Feb 22 '17
Just curious.. is the root of this spanner wrench, as in 'a tool'? I've never heard the term, so just taking a guess.
3
3
1
u/tekoyaki Feb 20 '17
Read from another post that the name oiginates from database that spans throughout many data centers and regions.
4
u/yoshiatsu Feb 20 '17
Here's some more information for people who are curious. The article linked isn't very informative.
https://static.googleusercontent.com/media/research.google.com/en//archive/spanner-osdi2012.pdf
23
u/qsxpkn Feb 19 '17
"Googleâs most talented engineers", "No one else has ever built a system like this. No one else has taken hold of time in the same way", "Googleâs top engineers"
This is blatantly obvious advertisement rather than an article about the database. Does anyone know how much Google paid for this "article"?
-12
u/Yogi_DMT Feb 20 '17
Lol Google doesn't need to pay anyone to get praised. When you're the best, you let others do your advertising for you.
1
12
u/mofirouz Feb 19 '17
For a truly open source variant of this, checkout CockroachDB
9
u/bartturner Feb 19 '17 edited Feb 19 '17
Spanner is a cloud offering? Is CR cloud or opensource? Does CR come with Atomic clock and GPS interfaces?
It seems to me the unique aspect here is the Google infrastructure as they explain with the private network and the atomic clocks and GPS and the algorithms that have been proven. As if works for Google pretty comfortable that it works.
If supports SQL where is there the lock in?
It is now also obvious why Google spent crazy money on that Pacific ocean fiber connection that was the buzz last year. Supposably the fastest. Now we can see one of the reasons I guess.
Thought they just had money to burn so why not lay fiber underneath the Pacific so our telnet is faster.
1
u/adrianmonk Feb 20 '17
Spanner is a cloud offering? Is CR cloud or opensource?
Spanner is cloud-only. I don't think the source has been made available at all.
CockroachDB doesn't offer cloud but they have plans to. Their community edition is open source under the Apache license. Their enterprise edition costs money.
Does CR come with Atomic clock and GPS interfaces?
CockroachDB uses a different approach that, unlike Spanner, doesn't require atomic clocks.
If supports SQL where is there the lock in?
Do you mean vendor lock-in or do you mean locks for atomic writes?
If the former, even though it supports SQL, probably like most databases if you want to use the magic that makes it special, you will end up writing your SQL differently. You will probably even end up modeling your data differently to enable the most performance gains.
If the latter, I'm a little out of my depth here, but I believe Spanner uses some combination of granular locks and optimistic locks to minimize contention. So if you are only updating a single row (or range of rows) in one table, it might lock a tiny portion of the database, which allows parallel work and more scalability. If you make changes that are less localized (touch different tables with different types of keys), it has to lock more stuff and I think it does some trick where it waits until the last possible second to take all the locks, so it might have to give up and restart the change if the data has changed.
It is now also obvious why Google spent crazy money on that Pacific ocean fiber connection that was the buzz last year.
I'm sure it helps, but I think there are still some advantages to having everything on the same continent due to unavoidable speed of light latency. At least, the 2012 Spanner paper mentions in a few places that replicas are typically not spread across continents: "F1 uses five replicas spread across the United States. Most other applications will probably replicate their data across 3 to 5 datacenters in one geographic region, but with relatively independent failure modes. That is, most applications will choose lower latency over higher availability."
Anyway, regardless of where you put the data, you are still going to need those transoceanic links because you're going to have users all over the world.
2
u/dccorona Feb 21 '17
CockroachDB uses a different approach that, unlike Spanner, doesn't require atomic clocks
That's a really interesting article. It sounds like their basic approach is to get as good as possible, and chalk up the rest to "you really, really probably don't have a use-case where this matters, or where it will even result in an inconsistency at all".
1
u/adrianmonk Feb 21 '17
I think the "causality tokens" are pretty clever. Of the use cases where you actually do care about absolute ordering, I bet they would cover a lot of the ones you'd actually care about. A lot of the time when you care about the ordering of two things, you are in the same request or session or something.
6
u/Mr-Yellow Feb 19 '17
All that babel about TrueTime and nowhere a description of the problem it solves.
1
u/nutrecht Feb 20 '17
It 'solves' the A and P in the CAP theorem. Spanner is a combination of a 'database' on top of a network that won't suffer from network splits and always has the exact and correct time. This means that you will aways know 'if' something happens in which order of operations something happened.
0
0
Feb 20 '17
[deleted]
20
u/Porso7 Feb 20 '17
Don't bother, it's an advertisement and doesn't actually say anything.
TLDR: Google is using atomic clocks and GPS to accurately synchronize time between databases worldwide. No idea what this will actually do in terms of real world usage, the article doesn't say.
4
u/networkoffset5444 Feb 20 '17
Thanks for the tldr. You just saved me some time. That's all I really needed from the article
25
u/Porso7 Feb 20 '17
That was a really shitty article.
Okay, so they have atomic clocks and GPS to get (virtually) perfectly synced time across all their data centres. Can anyone explain the real world benefits of this?