Lurker News
A common way of deploying a web application database at scale is to setup a MySQL or Postgres server, create one table for all customers, and have an account_id or owner_if field and let the application code handle security. This makes it easier to run database migrations and upgrade code per customer all at once.

I’m curious if anybody has taken the approach of provisioning one database per account? This means you’d have to run migrations per account and keep track of all the migration versions and statuses somewhere. Additionally, if an application has custom fields or columns, the differences would have to be tracked somehow and name space collisions managed.
Has anybody done this? Particularly with Rails? What kinda of tools or processes did you learn when you did it? Would you do it again? What are some interesting trade offs between the two approaches?

Comments (258) 117 threads

Earlier comments
hootbootscoot#
This sounds like a job for CouchDB with PouchDB in-client. You'll need to configure replication per-db, but this can be scripted via cURL or wget, as CouchDB has a REST API.

That's precisely what this system was designed for.

CodeWriter23#
A client of mine used some SaaS from a vendor. The vendor sold you an “Instance“ that basically is an EC2 instance. Each instance has a self-contained app and self-contained database. Migration and app update are handled a single client/single instance at a time. Standard backup and restore tools can be used. It’s a more expensive approach but the software was specialized and expensive too. An upside of this model, it lends itself well to clients who require on-premise deployment. And it seems for heavy use clients, you can scale up their instance and database as needed.
lemiffe1 reply#
We use a database per account, it is necessary for some ISO (and other) certifications to have single-tenant DBs.

Of course this requires a bunch of extra tooling, like upgrade scripts that don't ALTER tables directly but rather lock-copy-delete-rename, etc.
There are many tools out there which help out with this, and whatever we couldn't find we built ourselves. Tools like JOOQ can update code entities based on the database, so a database-first approach is what we used, but you can go either way.
The benefit of this approach is ultimately security and less multi-tenant catastrophes leaking data from customers, etc.

chrisacky1 reply#
Can you explain why the ALTER approach isn't feasible? If you are locking anyway, is it not the same thing?
lemiffe#
My mistake, I didn't check before posting.

The use case was: Keep the table online and don't bring down the Galera cluster (which happened when running an ALTER on a table with millions of rows).
We went for pt-online-schema-change (from Percona) which copies, alters the new table, keeps them in sync, and then replaces it. All automated which is pretty sweet.
One of the answers on here has more info:
https://stackoverflow.com/questions/463677/alter-table-witho...

tobyhede1 reply#
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.
nstart#
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.

Load more comments