News for hackers. Stuff that matters.
Re:How does database sharding work? by GeneralMayhem on Friday April 07, 2023 @01:24AM (#35478332)
> I suppose the more fundamental question is: why are you not using a database that does sharding for you? Over the past few years the so-called “serverless” database has gotten a lot more traction. Starting with the infamous Spanner paper, many have been thinking about how running a distributed system should be native to the database itself, the foremost of which has been CockroachDB. You can even run cloud Spanner on GCP.

This is the most important paragraph in the article. In a world where things like Dynamo/Cassandra or Spanner/Cockroach exist, manually-sharded DB solutions are pretty much entirely obsolete. Spanner exists because Google got so sick of people building and maintaining bespoke solutions for replication and resharding, which would inevitably have their own set of quirks, bugs, consistency gaps, scaling limits, and manual operations required to reshard or rebalance from time to time. When it's part of the database itself, all those problems just... disappear. It's like a single database that just happens to spread itself across multiple machines. It's an actual distributed cloud solution.

My current employer uses sharded and replicated Postgres via RDS. Even basic things like deploying schema changes to every shard are an unbelievable pain in the ass, and changing the number of shards on a major database is a high-touch, multi-day operation. After having worked with Spanner in the past, it's like going back to the stone age, like we're only a step above babysitting individual machines in a closet. Nobody should do this.

Re:How does database sharding work? by grogers on Friday April 07, 2023 @02:34AM (#35478772)
All the "auto-sharding" DBs have their own quirks, especially around hot key throughput. You often end up having to add "sharding bits" to the beginning of your keys to get enough throughput. The size of one partition is usually tiny compared to one partition of an RDBMS too. So it ends up being not nearly the panacea that it would seem to be.
Re:How does database sharding work? by bpicolo on Friday April 07, 2023 @08:42AM (#35481004)
Software side sharding keys seem significantly simpler than managing sharding infrastructure. That's the beauty of memcached right?
Re:How does database sharding work? by varsketiz on Friday April 07, 2023 @02:49AM (#35478874)
> In a world where things like Dynamo/Cassandra or Spanner/Cockroach exist, manually-sharded DB solutions are pretty much entirely obsolete.

Far from it. Plenty of companies that are now running mysql or postgress will shard manually when they will need to scale.

Re:How does database sharding work? by dalyons on Friday April 07, 2023 @11:33AM (#35482701)
It can still be an obsolete practice even if some continue to choose to do it.
Re:How does database sharding work? by cornel_io on Friday April 07, 2023 @03:23AM (#35479059)
Solutions like Spanner are fantastic for some things (and within Google it's a no-brainer, since you're not paying an outrageous markup), but besides being expensive, they don't just let you drop them in as a scalable replacement for an existing usage pattern, and usually sharding starts first coming up when you're at a huge scale and already have a ton of app code and database design but have grown past what a single db can support (that's a massive amount of traffic these days, well over 1M DAU for most apps). See for instance https://cloud.google.com/spanner/docs/schema-design, a lot of what you have to plan for is very different than if you were designing a normal (e.g.) Rails app with typical patterns.

It's a great option, as long as you're aware of the tradeoffs and don't expect it to act (and cost) exactly like a single Postgres server.

My order of ops generally goes:

- Can you get away with pure (or mostly) key/value for your hottest table(s) and mostly avoid queries? Use Dynamo or Google Cloud Datastore, but deeply understand their quirks (e.g. never index a timestamp field on Datastore or you're worse off than a weak-ass SQL server for write-heavy loads). These can scale basically forever with zero effort if you fit their constraints, and are cheap enough

- Can you tolerate the price of Spanner and deal with it not being normal SQL? Go for it, expect some non-trivial migration cost

- If you have to shard/cluster yourself, can you make Redis work? It'll be easier operationally than managing N SQL DBs of any kind. I know, if you could you'd probably have been fine using Dynamo...

- Can you soft-shard, e.g. use different machines for different tables and spread load enough to get each table onto a single server? Do it.

- Can you minimize re-sharding ops? (If you're in a massive growth phase, the answer is no) Ok, fine, do SQL sharding, and make sure you have well-tested code to do it and you mostly use key/value access patterns.

- Consider a beefy AF bare metal SQL server, see how far that gets you.

- If none of this applies, re-evaluate the cost ($ and time) of Spanner, you're paying a big price one way or another

- Only bad ideas from this point on, just do sharding but it'll suck...

Nowhere on my list: running your own cluster for Cassandra, Mongo, or anything similar, it's such an ops nightmare and there are hosted services that are better in every way but cost.

If you do end up writing your own sharding layer, best of luck, I have yet to see any reasonably production-ready ones that work well with pretty much any web framework or ORM. And writing these is so tough, so many edge cases to consider...not something I hope to ever do again.

Re:How does database sharding work? by branko_d on Friday April 07, 2023 @04:43AM (#35479590)
> Consider a beefy AF bare metal SQL server, see how far that gets you.

Quite far, possibly to millions of users.

Case in point: StackOverflow runs on SQL Server.

Re:How does database sharding work? by alexvoda on Friday April 07, 2023 @05:37AM (#35479857)
Actually it can vary "from not that" far to "all the way" (StackOverflow). It depends very much on the schema and the type of workload. But in general people really underestimate how far an RDBMS can take you.
Re:How does database sharding work? by dalyons on Friday April 07, 2023 @07:18AM (#35480432)
Yes and no - I think people also underestimate how miserable running a single giant RDBMS can be. Everything gets hard and dangerous - backup/restores, upgrades, online migrations & schema changes. Adding the wrong index can easily brings the whole thing down. They are complicated beasts that get very fragile at the large end (size+throughput)
Re:How does database sharding work? by ComodoHacker on Friday April 07, 2023 @01:24PM (#35484260)
Speaking of backups, how do you get consistent backup of a sharded DB? Not everything is sharded, some data are replicated and should be consistent.

Everything becomes fragile at the large end.

Re:How does database sharding work? by dalyons on Saturday April 08, 2023 @12:42PM (#35495062)
well im comparing it to a natively distributed database, like for eg dynamoDB. I've worked with truly massive dynamodb "instances" and you really dont have to do much, they arent fragile, they just work. I don't necessarily know how their backup system works under the hood, but it backs up to a consistent point in time.
Re:How does database sharding work? by KptMarchewa on Friday April 07, 2023 @12:28PM (#35483496)
"millions of users" are vastly different scales with different use cases. StackOverflow is mostly serving cached content.
Re:How does database sharding work? by GeneralMayhem on Sunday April 09, 2023 @02:20PM (#35505105)
> Consider a beefy AF bare metal SQL server, see how far that gets you.

Yep, this is of course the default - although you'll at least want replication for availability and disaster recovery, but that's not too bad with PG/RDS.

> they don't just let you drop them in as a scalable replacement for an existing usage pattern, and usually sharding starts first coming up when you're at a huge scale and already have a ton of app code and database design but have grown past what a single db can support

I think once you get beyond what fits on one primary box, you're almost certainly at the point of rethinking your data access models anyway. Manual sharding is disruptive too.

Re:How does database sharding work? by dagss on Friday April 07, 2023 @03:29AM (#35479092)
Would love to be able to use Spanner..

What do you suggest one does if one has to run on Azure though?

PS Azure Cosmos is not a real product, it is a beta toy that Microsoft just has made expensive enough that people think it cannot possibly be as bad as it is..

Re:How does database sharding work? by avinassh on Friday April 07, 2023 @04:37AM (#35479563)
> PS Azure Cosmos is not a real product, it is a beta toy that Microsoft just has made expensive enough that people think it cannot possibly be as bad as it is..

what kind of problems you ran into with Cosmos?

Re:How does database sharding work? by cj on Friday April 07, 2023 @08:32AM (#35480921)
It’s supposed to be MongoDB compatible (if I’m not mistaken).

Last I checked, that “compatibility” run at least a couple major version behind the real MongoDB.

To be fair, the same is probably also true with AWS’s DocumentDB

Re:How does database sharding work? by davidjfelix on Friday April 07, 2023 @10:27AM (#35481912)
I think that AWS DocumentDB uses the last mongodb version that wasn't their new(ish) SSPL license and any compatibility beyond that was "clean room" backported. Its possible Azure is in a similar place.
Re:How does database sharding work? by CSDude on Saturday April 08, 2023 @05:12AM (#35491772)
DocumentDB is a thin layer that converts the mongo commands to Postgres, uses Aurora Postgres. If you check, the constraints are exactly same. Even list RDS CLI returns the DocumentDB clusters.

Most obvious indicator is DocumentDB is single node write whereas Mongo is really not, coming from Aurora Postgres.

Re:How does database sharding work? by yawgmoth on Friday April 07, 2023 @05:46AM (#35479897)
Recently joined an Azure shop so new to Cosmos, but it's fairly abstract as a product. It seems to be a front for different database types like Mongo or Postgres or SQL Server with some additional features such as subscriptions.

Would be interested to hear your challenges.

Re:How does database sharding work? by vinay_ys on Friday April 07, 2023 @07:02AM (#35480332)
You could consider one of these three –

1. yugabytedb (postgres compatible)

2. cockroachdb (postgres compatible)

3. tidb (mysql compatible)

You could self-host these on azure or subscribe to each of their managed service offering.

Of course, sticker price for managed will be more expensive than self-hosted enterprise product which itself will be more expensive than the self-hosted, community-supported, open-source option (which seems to be a real option with YugabyteDB).

Re:How does database sharding work? by sgtfrankieboy on Friday April 07, 2023 @07:34AM (#35480534)
It's also a real option with CockroachDB, unless you need regional sharding. Which a lot don't actually need anyways.
Re:How does database sharding work? by rad_gruchalski on Saturday April 08, 2023 @07:40AM (#35492354)
YugabyteDB tablespaces will get you there inside of their Apache 2 license.
Re:How does database sharding work? by 411111111111111 on Friday April 07, 2023 @08:11AM (#35480761)
It always triggers me to read "postgres compatible" when they've entirely skipped features such as database triggers

They've just selected a few features from postgres which they're compatible in.

Re:How does database sharding work? by alexvoda on Friday April 07, 2023 @09:13AM (#35481227)
I agree. I guess what they mean is you can use the existing adapter for your language.

The PostgreSQL organisation should create some trademarked badges to properly label the levels of compatibility other products offer.

Re:How does database sharding work? by rad_gruchalski on Saturday April 08, 2023 @07:41AM (#35492364)
YugabyteDB supports triggers. YugabyteDB is not just „postgres compatble”, it is postgres with a distributed layer.
Re:How does database sharding work? by KaiserPro on Friday April 07, 2023 @06:04AM (#35479983)
> In a world where things like Dynamo/Cassandra or Spanner/Cockroach exist, manually-sharded DB solutions are pretty much entirely obsolete.

Not really. Cassandra is a write optimised, slow, network and IO heavy beast thats a pain in the arse to administer. We replaced a 6 node m4.4xlarge with a single db.m4.2xlarge. on postgres.

You need to pick your DB to match your data, not the otherway around.

Re:How does database sharding work? by billythemaniam on Friday April 07, 2023 @07:39AM (#35480563)
Spanner and Cassandra really shouldn't be in the same sentence. They are optimized for very different use cases. The "obsolete" part of that quote does apply to Spanner, TiDB, and CockroachDB in my experience. I haven't used Yugabyte, but the other sharded databases, including Vitess (TiDB is what Vitess is trying to be), actually make life harder.
Re:How does database sharding work? by samlambert on Friday April 07, 2023 @10:02AM (#35481657)
> (TiDB is what Vitess is trying to be)

Not true. Vitess is far more proven.

Re:How does database sharding work? by billythemaniam on Friday April 07, 2023 @10:40AM (#35482032)
I'm not sure what "more proven" exactly means. If you mean, it is not as well known and used in US? Yes that's true because the core developers are Chinese. If you mean, it hasn't been used at the same scale in production? False, just completely false.

At my current job, we have reached a scale and use case that requires either manually sharding the database or using a distributed one. 20 TB including indexes, 10-20 tables with >1B rows, and spikes of up to 10K/s transactions. We put Vitess, TiDB, CockroachDB, and Spanner head-to-head. Spanner won out because it is far superior to them all for scaling geo-replication and sysadmin. But TiDB was a close second because it just works, scales, and fast. Vitess on the other hand was extremely buggy and has a very long, undocumented list of unsupported SQL features. Yes Vitess has better press and more well known, but it is an inferior technology imo. TiDB is already what Vitess claims to be.

Re:How does database sharding work? by samlambert on Friday April 07, 2023 @10:53AM (#35482168)
20TB of data and 10k/s of transactions is not a large cluster. There are Vitess cutters with petabytes of data and 100s of millions of users doing millions of QPS.

The largest Vitess cluster we know of runs on 10s of thousands of servers. So "False, just completely false" is dramatic.

Slack is powered by Vitess https://slack.engineering/scaling-datastores-at-slack-with-v... Twitter runs Vitess https://blog.twitter.com/engineering/en_us/topics/infrastruc...

I am sorry you had a buggy experience running Vitess yourself but you just wrong about the size of these production workloads.

Re:How does database sharding work? by pradeepchhetri on Friday April 07, 2023 @11:40AM (#35482813)
> There are Vitess cutters with petabytes of data.

Is it GitHub? Where can I find more details about them? It will be interesting to read more about it.

Re:How does database sharding work? by billythemaniam on Friday April 07, 2023 @11:03AM (#35482279)
I didn't claim that I am dealing with a gigantic workload (total QPS is a lot higher btw). Rather I claimed we are at the point where a distributed database is a good idea if the business wants to continue to scale without major database trouble. I gave the specs so others can understand the parameters of the evaluation. If Vitess didn't perform well at that scale, I can't imagine the pain at Slack and Twitter scale. I don't envy them at all.
Re:How does database sharding work? by samlambert on Friday April 07, 2023 @11:07AM (#35482337)
https://twitter.com/stewart/status/1252385972014088194

They are pretty happy!

Different people find different things difficult. Don't feel bad.

Re:How does database sharding work? by billythemaniam on Friday April 07, 2023 @11:18AM (#35482462)
I don't. I am very happy to not use Vitess and more than willing to recommend your competitors. Don't feel bad.
Re:How does database sharding work? by KaiserPro on Friday April 07, 2023 @04:50PM (#35486806)
you're missing the point. spanner and cassandra as for completely different ways of storing/accessing data.

For most people(ie 95% of people) After putting a caching layer, paying for a bigger RDS(or equivalent) DB service is the cheapest and fastest option.

Re:How does database sharding work? by foobiekr on Saturday April 08, 2023 @01:29PM (#35495582)
Most orgs, including mine, also have the experience of a cassandra cluster simply melting down. That was more common ten years ago, but it is pretty much chaos when it happens.

If the non-SQL distributed databases, I found FoundationDB to be by far the most robust and zero-overhead.

However, at least through version 6, it had absolutely terrible, service-destroying behavior if any of the nodes became low on storage, dropping transaction levels through the floor and since transactions are used for recovery and cluster expansion... Since it does a very nice job auto-sharding, if you're not paying attention this tends to happen to multiple nodes around the same time.

I wish it would get more attention though. It is a really amazing tool.

Re:How does database sharding work? by bsaul on Friday April 07, 2023 @09:36AM (#35481426)
How does cockroach compares in terms of performance to manual sharded databases ?

My intuition is that a properly sharded database will perform faster-or-same as a non-sharded one in all scenarios. Whereas automatically-sharded database will actually perform worst until you start reaching critical traffic that a single instance won't handle no matter what.

Am i wrong ?

Re:How does database sharding work? by jandrewrogers on Friday April 07, 2023 @10:27AM (#35481910)
Where the line gets blurry is that there can be layers of sharding inside the database even if it is never exposed to the end user. At the limit, disk pages are essentially a shard and treated similarly. Some database kernels do true sharding and resharding within a single server transparently for performance reasons, it isn't just for having data on multiple servers or cores.

There is no intrinsic performance penalty for automatic sharding, it can be as fast or faster than any other scheme. The "faster" cases are when the sharding structure must be adaptive to have uniform distribution of data and load, such as when the key distribution is intrinsically unpredictable or when you are doing multi-attribute sharding. In these cases, effective sharding can only be created by software at runtime.

Re:How does database sharding work? by hilbertseries on Friday April 07, 2023 @10:43AM (#35482063)
> My intuition is that a properly sharded database will perform faster-or-same as a non-sharded one in all scenarios.

This is workload dependent, if your queries only go to one shard, this is likely true. But if you have cross shard queries, then it’s no longer true.

Re:How does database sharding work? by crazygringo on Friday April 07, 2023 @11:00AM (#35482244)
> My intuition is that a properly sharded database will perform faster-or-same as a non-sharded one in all scenarios.

No, because as soon as you have to JOIN across shards performance plummets. I mean, you can't JOIN across shards so you have to simulate that client-side which is very slow.

The secret to performant manual sharding is to figure out a way to minimize JOINs across shards, and that the ones you have to do are very small. Or to start replicating data which quickly becomes its own nightmare of consistency.

Re:How does database sharding work? by rad_gruchalski on Saturday April 08, 2023 @07:43AM (#35492373)
You can join like that with YugabyteDB. But it would be good to get intimate with explain analyze.
Re:How does database sharding work? by arjunnarayan on Friday April 07, 2023 @11:36AM (#35482747)
[disclosure, former cockroachdb engineer]

you can get expected "single shard" performance in CockroachDB by manually splitting the shards (called "ranges" in CockroachDB) along the lines of the expected single shard queries (what you call a "properly shared database"). This is easy to do with a single SQL command. (This is what we do today; we use CockroachDB for strongly consistent metadata).

The difference between CockroachDB and a manually sharded database is that when you _do_ have to perform some cross-shard transactions (which you inevitably have to do at some point), in CockroachDB you can execute them (with a reasonable performance penalty) with strong consistency and 2PC between the shards, whereas in your manually sharded database... good luck! Hope you implement 2PC correctly.

Re:How does database sharding work? by dravita on Friday April 07, 2023 @05:31PM (#35487236)
The point about strong consistency and 2PC cross-shard is a good one. Even among other "auto-sharding" distributed relational databases, not all of them provide that.
Re:How does database sharding work? by eternalban on Friday April 07, 2023 @10:26AM (#35481901)
Because distributing a database is sensitive to cap semantics (AP or CP) and data dependencies (graph partitioning is hard *) and storage engine choices are driven by use-cases and the general technical solution is thus highly complex**. Spanner uses atomic clocks, for example. Running CockroachDB yourself is [very likely] not the same thing as using a saas varient, either. Sight unseen, it can not be 'trivial'. Same for Spanner. The general solution seems to require paying someone to provide the service. In sum, it is not a clear cut yes/no situation.

btw, [distributed] Postgres iirc was never as stellar the single node (the stuff we sing praises of) vs the distributed deployment. I'm sure it has improved significantly.

> "manual operations required to reshard or rebalance from time to time. When it's part of the database itself, all those problems just... disappear."

Not really correct.

* "Choosing the right keys can help Spanner evenly distribute data and processing to avoid hotspots"

https://cloud.google.com/spanner/docs/schema-design

https://cloud.google.com/blog/topics/developers-practitioner...

** https://static.googleusercontent.com/media/research.google.c...

[Spanner certainly did -not- start off as a distributed RDBMS. Because that project would have never been given a green light. Because it is understood just how complex that system would need to be. It started off as a distributed k/v. That's it.]

The initial focus of Spanner was on scalability and fault-tolerance, much as was the case for many systems at Google. In the last few years Spanner has increasingly become a fully-fledged database system. Production deployment with internal customers taught us a great deal about the requirements of web scale database applications, and shaped the techniques we presented in this paper. Aggressive focus on horizontal scalability enabled widespread deployment without investing too heavily in single machine performance. The latter is one of the areas where we are making continuous improvements, such as upgrading lower level storage to Ressi.

The original API of Spanner provided NoSQL methods for point lookups and range scans of individual and interleaved tables. While NoSQL methods provided a simple path to launching Spanner, and continue to be useful in simple retrieval scenarios, SQL has provided significant additional value in expressing more complex data access patterns and pushing computation to the data.

Re:How does database sharding work? by BiteCode_dev on Friday April 07, 2023 @12:16PM (#35483316)
If it's one more man-week of pain a year, but the spanner solution add a additional cost that is 10 times that, there is an incentive for the employer to keep the painful solution.

That and lock in, open source ecosystem and so on.

It's not a black and white situation.

Re:How does database sharding work? by game_the0ry on Friday April 07, 2023 @12:19PM (#35483374)
I just have to say - thanks for your response.

I am a full stack dev but I end up doing a lot of front end and rarely have to touch SQL. Posts like yours help me to realize there's still a lot I do not know while still being informative. So thanks <3

Re:How does database sharding work? by throwawaaarrgh on Saturday April 08, 2023 @09:18AM (#35492831)
distributed decentralized databases are their own special nightmare. they are all full of hidden traps, footguns, pains, limits. the more you push them the more these emerge.

in some ways the stone age was terrible, and in some ways the stone age was far superior to the current age. we've gotten soft, lazy, stupid, comfortable. we used to be feral animals, but now we're more like house plants. if we got more used to doing things like dumb animals, life would be simpler, and we could appreciate what we have more. but it's hard to resist the temptation of feeling like what we have is never enough. the grass is always greener, and how terrible that it isn't ours.