Live data from Hacker News

Ask HN: Has anybody shipped a web app at scale with 1 DB per account?

news.ycombinator.com

51–60 of 264 posts

Re: Ask HN: Has anybody shipped a web app at scale with 1 DB per account?

#51
Worked at a large B2B SaaS from near beginning. You want a hybrid; shared DB, but with the ability to move to a 'shard/pod' architecture where you separate out your customers/users into different dbs / apps servers as you scale.

We did it about 3 years in, when DB became a scale challenge. Eventually you'll also get to the point where you want to be able to rebalance and migrate data between each shard.

All of this is nothing you should be trying to solve too early; i struggle to think of any real benefits of single DB per user, unless you are separating out all architecture- including app servers - and that might only be relevant for large enterprise customers? Selling in that market is hard.

Re: Ask HN: Has anybody shipped a web app at scale with 1 DB per account?

#53

The inability to reuse database connections would be a huge performance hit. In a traditional webapp backend, you have a pool of connections to the database. User01 hits your service, and grabs a connection off the pool. User02 does the same, and so on. These connections get put back in the pool for reuse once a user is done with them. In your design, every time a user hits your service, a new connection, specific to…

I think (but it's not totally clear) that the proposal is about B2B services and it's one database per paying account, not one database per end user.

For instance, if you're Slack, the proposal would be to have one database per Slack workspace, not one database per person who has a login to any workspace. You absolutely need to have data relationships between multiple users in a workspace. You don't necessarily need relationships between multiple workspaces (as it happens, Slack only added this feature very recently), and having different databases means that if company 1 triggers pathological behavior on their Slack, company 2 is unaffected.

Or, if you're Wikia^WFandom, you'd have one database per site. You could certainly run one big database for the entire site, but you could also have one database for Wookieepedia, one for Memory Alpha, one for the Harry Potter Wiki, etc.

In these situations, you wouldn't have the problem about the performance hit or about making it work with frameworks - you'd run separate web application servers per workspace/site/customer, too. Some of your machines would be running Rails/Spring Boot/Django/PHP/whatever with an environment file for Wookieepedia. Some would run Memory Alpha. Ideally you'd throw this in some sort of autoscaler (anything from Apache-managed FastCGI processes to Kubernetes would work fine). But User02, when they visit Wookieepedia, would hit a Wookieepedia application server that User01 has previously used and already has a connection to the Wookieepedia DB.

Yes, you would need to deal with doing a DB migration per site/customer instead of one big migration - but on the other hand, you get to to a DB migration per customer instead of one big migration. Each one is unaffected by the others. You'd spend a bit of time automating the process of migrations and you'd hit your test sites first, and then migration is much more reliable for your customers. If you really need downtime, you can set separate downtime per customer, and if one customer has a bunch of data that takes forever to migrate, no other customer cares. It's a tradeoff, and it may be useful.

If you want to allow customers to collaborate, you need to build it as an API, as if they were two different products interacting at arms' length - but you can certainly do that.

Re: Ask HN: Has anybody shipped a web app at scale with 1 DB per account?

#54

If you are using Rails, have a look at using PostgreSQL schemas. Single "physical" database but the schemas give you distinct logical databases. Perfect for multi-tenant situations.

I think a lot of companies that provide hosting services of open source software follow this model. They'd probably be the best example for the OP to refer to in their search for relevant designs. I think ghost blog follows this model even when you self host. So a single machine can host multiple blogs and each blog creates their own random named schema.

Re: Ask HN: Has anybody shipped a web app at scale with 1 DB per account?

#55
post #29

Earlier quoted context omitted.

> In your design, every time a user hits your service, a new connection, specific to that user, will have to be made. This will incur network traffic and the overhead of logging in to the DBMS. If connecting to your DB is significantly increasing your page times, you've got seriously fast pages. Even back when I was working with a MySQL database regularly in 2010, connect + login was 5 ms at maximum (and I think it w…

> connect + login was 5 ms at maximum (and I think it was much less, I just don't remember that far). I try to get all my endpoints under 10ms! 5ms per call would be huge. (Obviously I can't succeed all the time, but 5ms is big numbers imo)

With Elixir/Phoenix I sometimes see response times measured in µs rather than ms!

Re: Ask HN: Has anybody shipped a web app at scale with 1 DB per account?

#56
post #18

There aren't a lot of benefits to doing it. If you have frequent migrations, then it probably isn't something you ever want to do. For a site I run, I have one large shared read-only database everyone can access, and then one database per user. The per-user DB isn't the most performant way of doing things, but it made it easier to: + Encrypt an entire user's data at rest using a key I can't reverse engineer. (The use…

Can you elaborate on how you achieved encryption at rest with a key you can’t access? I’m assuming the key is sent in an authorization header, then lives in memory for the duration of the session, but wondering what your tool chain looks like.

Re: Ask HN: Has anybody shipped a web app at scale with 1 DB per account?

#57

Azure has a product built specifically for this, so it must be at least vaguely common. The rationale given in docs is: "A common application pattern is to provision a single database for each customer. But different customers often have varying and unpredictable usage patterns, and it's difficult to predict the resource requirements of each individual database user." https://docs.microsoft.com/en-us/azure/sql-databa…

We used this successfully at my company many years ago when it was first released. Even automated updates through DevOps. Have there been any recent big changes that have improved it? Been looking at it again.

Re: Ask HN: Has anybody shipped a web app at scale with 1 DB per account?

#58
I have a bit of experience with this. A SaaS company I used to work with did this while I worked there, primarily due to our legacy architecture (not originally being a SaaS company)

We already had experience writing DB migrations that were reliable, and we had a pretty solid test suite of weird edge cases that caught most failures before we deployed them. Still, some problems would inevitably fall through the cracks. We had in-house tools that would take a DB snapshot before upgrading each customer, and our platform provided the functionality to leave a customer on an old version of our app while we investigated. We also had tools to do progressive rollouts if we suspected a change was risky.

Even with the best tooling in the world I would strongly advise against this approach. Cost is one huge factor - the cheapest RDS instance is about $12/month, so you have to charge more than that to break even (if you're using AWS- we weren't at the time). But the biggest problems come from keeping track of scaling for hundreds or thousands of small databases, and paying performance overhead costs thousands of times.

Re: Ask HN: Has anybody shipped a web app at scale with 1 DB per account?

#59
post #2

Seems impractical and slow at scale to manage even a few hundred separate databases. You lose all the advantages of the relational model — asking simple questions like “Which customers ordered more than $100 last month” require more application code. You might as well store the customer info in separate files on disk, each with a different possible format and version.

Those queries are definitely convenient early on but eventually you shouldn't be making those against that system and instead aggregate the data into warehouse.

Technically, there are DBs that let you do cross shard queries. See Azure Elastic DB.

Post reply on HN