News for hackers. Stuff that matters.
Re:GitHub's online schema migration for MySQL by perlgeek on Thursday May 03, 2018 @01:50AM (#16983620)
We evaluated gh-ost, but the killer for us is that it doesn't support any kind of foreign keys.

I understand that at GitHub's scale, foreign keys might be more of a hassle than what they are worth, but for a smallish company that values data integrity over scale and uptime, this is not an acceptable choice.

Re:GitHub's online schema migration for MySQL by shlomi-noach on Thursday May 03, 2018 @02:27AM (#16983784)
Author of gh-ost here. Actually, it should be possible to support child-side foreign keys. They would have to be named differently (the foreign key constraint has a unique name in a schema) -- but it should work. See discussion in https://github.com/github/gh-ost/issues/507

It is true that it is not on our roadmap to implement FK support for gh-ost (see https://github.com/github/gh-ost/issues/331), but if anyone wishes to contribute support for FK we're grateful. We've had more complex contributions coming from the community and we're grateful for those.

Re:GitHub's online schema migration for MySQL by killbrad on Thursday May 03, 2018 @03:37AM (#16984052)
Child-side foreign keys? Does that mean constraints in app logic instead of the database?
Re:GitHub's online schema migration for MySQL by shlomi-noach on Thursday May 03, 2018 @04:22AM (#16984224)
No, on the database itself. I used "child side" for lack of better terminology. Every foreign key has two edges: the "parent" table and the "child" table. Rows in the "child" table depend on existence of matching rows in the "parent" table.

It should be feasible to run `gh-ost` to ALTER a table that only has "child"-side constraints. It will be impossible to run `gh-ost` to ALTER a table that has "parent" side constraints.

Hope this clarifies.

Re:GitHub's online schema migration for MySQL by cup-of-tea on Thursday May 03, 2018 @02:48AM (#16983847)
I'm interested to know what companies are doing "at scale" to not need to use foreign keys. Do they just write user ids or whatnot into other tables?
Re:GitHub's online schema migration for MySQL by barrkel on Thursday May 03, 2018 @03:09AM (#16983935)
Foreign keys add locks to referenced rows during insert and update transactions. Bad news if you're locking those rows for other reasons, like a kind of distributed lock. So the problem with FKs isn't that you don't need them; it's that the extra locking limits concurrency.

FK checks also affect performance, of course. Where I work, we disable FKs on our bulk inserts but keep them enabled otherwise and also in tests; but our workload is different from the usual consumer web app, we have multi-million row inserts per user, and no more than 100 users or so per customer, who each get their own tenant DB.

Re:GitHub's online schema migration for MySQL by Chilinot on Thursday May 03, 2018 @03:07AM (#16983922)
I'm guessing they are validating the constraints in their applications instead of their databases. While this puts more requirements on your setup and developers, it offloads a lot of stress from your database.
Re:GitHub's online schema migration for MySQL by cup-of-tea on Thursday May 03, 2018 @07:54AM (#16985097)
Yeah, but from a data integrity point of view that is horrifying. I guess they must have some very comprehensive testing framework to validate the code.

Also, pardon my further ignorance, but if you're not going to use foreign key constraints, what is the point of using a relational db? Why not just a fast key-value store for each index?

Re:GitHub's online schema migration for MySQL by fipar on Thursday May 03, 2018 @09:44AM (#16985935)
You may me able to architect your application to do well-enough without Foreign Keys yet still require features that key-value stores do not provide, like SQL, triggers, etc.

In the specfic case of MySQL, while still horrifying (I agree with you! but it is one of the things you some times have to do at scale), you can create the Foreign Key constraints but then disable their verification and periodically look for violations, as described here: https://www.percona.com/blog/2011/11/18/eventual-consistency...

Re:GitHub's online schema migration for MySQL by zzzeek on Thursday May 03, 2018 @11:52AM (#16987056)
The application still has foreign key references and JOIN, which you don't get in a k/v store, you just don't have the actual CONSTRAINT. MySQL worked this way for literally decades with the MyISAM storage engine that does not have referential integrity constraints, it just allows the FOREIGN KEY syntax to pass silently.
Re:GitHub's online schema migration for MySQL by gaius on Thursday May 03, 2018 @06:19AM (#16984655)
How does the app know what is valid in a foreign key column without repeatedly retrieving the primary key column of the other table?
Re:GitHub's online schema migration for MySQL by tomnipotent on Thursday May 03, 2018 @12:25PM (#16987363)
It repeatedly retrieves the parent row. Look at the URL when replying on HN and change the id by adding a few 0's - the app prevents a comment from being added if the ID isn't found. Of course, this doesn't prevent a developer from circumventing the apps constraint logic.
Re:GitHub's online schema migration for MySQL by gaius on Thursday May 03, 2018 @12:50PM (#16987583)
This would place a far higher load on the DB (which would validate the constraint via the index if allowed to) and create more network roundtrips. It’s 10x as expensive as just using a FK, and far less reliable!
Re:GitHub's online schema migration for MySQL by tomnipotent on Thursday May 03, 2018 @08:26PM (#16991211)
>> It’s 10x as expensive as just using a FK

It's not about load, it's about locks and contention in the database caused by FK constraint enforcement. Extra read queries will barely be noticeable compared to that.

Re:GitHub's online schema migration for MySQL by gaius on Friday May 04, 2018 @01:11AM (#16992327)
it's about locks and contention in the database caused by FK constraint enforcement

What locks do you mean?

Re:GitHub's online schema migration for MySQL by sa46 on Friday May 04, 2018 @03:39AM (#16992856)
Google's Spanner doesn't support foreign keys but does support interleaved tables to cover some use cases of foreign keys. Spanner sees wide use inside Google.

The usual routes for data consistency between tables are batch clean up or in-app validation.

Re:GitHub's online schema migration for MySQL by zzzeek on Thursday May 03, 2018 @11:49AM (#16987037)
Are you referring to ON UPDATE cascades? Mutable/natural primary key values are not very common these days especially in a DB like MySQL. Incrementing integers are most common.
Re:GitHub's online schema migration for MySQL by pwnna on Thursday May 03, 2018 @01:52AM (#16983630)
This is an unfortunate by product due to the way that gh-ost is implemented. It is simply not possible to run it with a FK constraint. The reason is that since it replays the binlogs on the ghost table while the ghost table is not fully populated, the FK constraint will cause some of statements to fail. The data move from the original to the ghost table cannot be completed.
Re:GitHub's online schema migration for MySQL by codedokode on Thursday May 03, 2018 @02:11AM (#16983708)
Cannot it add FK constraints after the ghost table is fully populated?
Re:GitHub's online schema migration for MySQL by perlgeek on Thursday May 03, 2018 @02:18AM (#16983740)
The problem is that adding FK constraints is another schema change, which causes MySQL to copy the whole table, and lock the table during this time -- which is precisely what gh-ost tries to avoid.

Worse, foreign keys from other tables to the one that is being changed would need to be updated as well, blocking those tables in turn.

Re:GitHub's online schema migration for MySQL by zmoazeni on Thursday May 03, 2018 @01:33PM (#16987947)
> which causes MySQL to copy the whole table

This is wrong on a couple levels. First it doesn't copy the whole table: https://dev.mysql.com/doc/refman/5.6/en/innodb-create-index-...

However, it can take a while if MySQL is evaluating the consistency. But you can disable that with `SET FOREIGN_KEY_CHECKS = 0` which turns it into a metadata change (nearly instantaneous).

You still will need to check for violations, but you can do that in a more friendly-to-load manner, and of course will need to deal with any violations manually.

But that strategy is a good middle ground to all-or-nothing FKs.

Edit: Whoops, looks like I was wrong on the table-copy part. Per "Otherwise, only the COPY algorithm is supported." So it does copy the data when `FOREIGN_KEY_CHECKS=1` (the default)

Re:GitHub's online schema migration for MySQL by zmoazeni on Thursday May 03, 2018 @01:33PM (#16987952)
Yeah. See https://github.com/github/gh-ost/issues/507#issuecomment-338... on how to do it in a performant way.
Re:GitHub's online schema migration for MySQL by yxhuvud on Thursday May 03, 2018 @10:47AM (#16986503)
What? Why would enforced consistency be less worth at bigger scale? My guess would have been the complete opposite.
Re:GitHub's online schema migration for MySQL by sjansen on Thursday May 03, 2018 @12:44PM (#16987542)
It's not a question of worth but feasibility. Just like an ideal schema is fully normalized, but performance concerns sometimes drive denormalization. When foreign keys can't be used to enforce data integrity, the application has to be built to compensate in other ways. Sometimes that means simply accepting dirty data, and designing the application to stay robust when encountering unexpected data. Other times it means building alternate solutions to discover and repair data issues.
Re:GitHub's online schema migration for MySQL by sciurus on Thursday May 03, 2018 @01:00PM (#16987666)
shlomi-noach linked to https://github.com/github/gh-ost/issues/331 in another comment. That goes into some of the reasons to avoid foreign keys.

At a past job where we had a complex MySQL setup, I set up a slack autoresponse to post "Just say no!" anytime someone mentioned foreign keys. :-)

Re:GitHub's online schema migration for MySQL by anton_gogolev on Thursday May 03, 2018 @05:31AM (#16984495)
How could foreign keys ever be a "hassle"?
Re:GitHub's online schema migration for MySQL by shlomi-noach on Thursday May 03, 2018 @07:27AM (#16984972)
They can be a hassle when you want to shard your data, having outgrown your single-instance capacity. You will either shard functionally (extracting complete tables to other database servers), in which case FKs will completely break, or horizontally (split rows across database servers), in which case you may or may not be able to still use FKs.

They're also a performance impact on large tables since inserts/deletes must make multiple trips to the tables/indexes. That's a growing operational hassle as tables grow larger.

Re:GitHub's online schema migration for MySQL by asaph on Thursday May 03, 2018 @08:14AM (#16985229)
> You will either shard functionally (extracting complete tables to other database servers)

This isn't sharding. This is vertical partitioning. Sharding is a type of horizontal partitioning.

Reference: https://en.m.wikipedia.org/wiki/Shard_(database_architecture...