简体   繁体   中英

How can I have multiple schemas and multiple subdomains when hosting on Heroku?

I am planning on using Devise and Apartment in my upcoming application to create subdomains for each organization that creates an account. I would like to host my application on Heroku, but ran across the following quote:

The most common use case for using multiple schemas in a database is building a software-as-a-service application wherein each customer has their own schema. While this technique seems compelling, we strongly recommend against it as it has caused numerous cases of operational problems. For instance, even a moderate number of schemas (> 50) can severely impact the performance of Heroku's database snapshots tool, PG Backups.

What technique would work well with Heroku to host basecamp-style subdomains in rails 4 where many users can log in to the subdomain which they are part of?

If Heroku does not work, what other PaaS options are there that would do this well?

Domain

Firstly, you need to be sure that you're using your own custom domain for the subdomains.

Heroku's standard xxx.herokuapp.com won't be able to handle another subdomain on top of that - so you'll basically need to use your custom domain from the get-go

It will be good to reference this documentation for more information!


Multi Tenancy

Although I don't have experience with PGSQL's schemas , I do have some with multi tenancy as a whole.

There are a number of great resources here:

Essentially, multi-tenancy is just a way to scope the data so that it's only the tenant's that you see / interact with. In the sense of the DB, the two ways to achieve this are either to use different DB's (as you would with MYSQL), or use a schema (like with PGSQL)

Whilst I can't give you a direct fix for your issue, I can help you with some ideas:


Models

One way to achieve multi-tenancy, especially with the likes of MYSQL, is to do it through the model:

How do i work with two different databases in rails with active records?

#lib/admin.rb
class Admin < ActiveRecord::Base
  self.abstract_class = true 
  establish_connection "#{Rails.env}_admin"
end

#app/models/option.rb
Class Option < Admin
  # do stuff
end

This works very well for us, although we have not got it working for scoped accounts yet. We've been thinking of setting a @@class_variable for the Account or something, but haven't been working on that right now.

This works very well for MYSQL - powered databases, but also means you'll have to create db's for every account, which will not work with PGSQL (as far as I'm aware)


PGSQL Schemas

I feel this is kind of a cheat way to do this, as all the data is still stored in 1 database - it's basically just scoped around different types of data.

The problem here is that real multi tenancy should be where you completely separate the user's data, so you could cut it out of the app completely if they wanted. From a security & access perspective, it's the most flexible & modular way.

The problem for Heroku is they can only use one database (they give everyone access to their AWS database instances), meaning they can't allow you to create 50+ free databases (it just won't work very well).

You can, of course, use your own stack to create the databases you require, but in terms of PGSQL, it's just about creating the schemas for your data & then using something like - Apartment to make it happen:

PostgreSQL works slightly differently than other databases when creating a new tenant. If you are using PostgreSQL, Apartment by default will set up a new schema and migrate into there. This provides better performance, and allows Apartment to work on systems like Heroku, which would not allow a full new database to be created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM