Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a multi-tenant Rails 3 app for deployment on Heroku

I'm building a Rails 3 app for deployment on Heroku, and I'm wondering if there are any recommendations on how to handle multi-tenancy in my models. Half a year ago, there was a related question (#3776593) posted on here, but it didn't get many answers. I've also watched Guy Naor's presentation on writing multi-tenant applications with Rails, but it seems like 2 of the 3 proposed solutions wouldn't work on Heroku. I'd link to these, but new Stackoverflow users are limited to 2 hyperlinks.

I've also come across the following tools:

  • http://samuel.kadolph.com/2010/12/simple-rails-multi-tenancy/
  • http://blog.codecrate.com/2011/03/multitenant-locking-down-your-app-and.html

Just wondering if you have experience with either the multitenant gem or the simple-rails-multi-tenancy gem. Seems like the most straightforward solution would be to simply put a belongs_to on all my models that need to be under an Account, but I'd really like to know what you're using in the real world!

like image 573
Stephen Huey Avatar asked May 06 '11 20:05

Stephen Huey


1 Answers

The approaches range from "share nothing", which usually means one database per tenant, to "share everything", which usually means each table contains rows from many tenants. The spectrum (below) can be broken down by degree of isolation, by cost (cost per tenant, that is), and by ease of disaster recovery.

  • One database per tenant; highest cost, highest isolation, easiest recovery.
  • One schema per tenant; cost between the other two, good isolation, fairly easy recovery, but recovery usually degrades performance for other tenants.
  • Share tables among tenants; lowest cost, lowest isolation (shared tables), difficult disaster recovery (recovery usually means recovering some rows for every table). Recovery usually degrades performance "a lot" for the other tenants.

All those are platform-specific to some degree. "One database per tenant" has the highest isolation when the dbms prohibits queries from accessing more than one database. But some platforms allow querying across multiple databases.

MSDN has a decent article that hits the high points: Multi-Tenant Data Architecture.

But if you're limited to Heroku, you'll have to pick an option that Heroku supports. I don't know what those options might be, but I do know that you're better off not using SQLite in development. Heroku deployments are going to run on PostgreSQL; you need to develop against PostgreSQL.

like image 146
Mike Sherrill 'Cat Recall' Avatar answered Oct 13 '22 01:10

Mike Sherrill 'Cat Recall'