Multitenant
SaaS Apps in
Rails
By Iqbal Hasnan
Me
● 2nd time attending Ruby Tuesday
● work for Booster for 2.5 years
● my blog https://u.osu.edu/hasnan.1
Multitenant
Meaning
Multi-tenancy is an architecture in which a
single instance of a software application
serves multiple customers. Each customer is
called a tenant.
Examples
● github.com
● basecamp.com
SaaS
Meaning
Software as a service (SaaS) is a software
delivery method that provides access to
software and its functions remotely as a
web-based service in which software is
licensed on a subscription basis.
Multi-tenancy options for Rails
● Postgresql schema (apartment gem)
● Column scoping
PostgreSQL
Schema
Column Scoping
Schema Column Scope
● Migration running time O(n+1)
● Only works with PostgreSQL
● Creates new schema every time
● Apartment gem
https://github.com/influitive/apartment
● Migration running time O(1)
● Work with any database.
● Add 1 row record
● Classic way of doing multi-tenancy
before PostgreSQL schema
Schema Scope https://github.com/influitive/apart
ment#usage
Column Scope
1 ) Controller Level scoping
2 ) Model level scoping
● default_scope
● unscoped
Controller level scoping
app/controllers/application_controller.rb
Model level scoping
app/models/tenant.rb app/models/post.rb
Apps URL
1 ) Sub-domain
2 ) Non sub-domain
Slack Github
https://booster.slack.com https://github.com/orgs/booster/dashboard
● Requires wildcard SSL
● Enable NginX wildcard config
● Works with normal SSL (free with
letsencrypt.org)
Sub-domain Approach
● Make use of Rails Routing Constraints
● http://guides.rubyonrails.org/routing.html#request-based-constraints
Sub-domain Approach
config/routes.rb
Sub-domain Approach
app/services/subdomain_required.rb
app/services/domain_required.rb
Charging
Customer
1 ) Subscription
2 ) Rake task & Cron Job
Subscription & Billing Implementation
● DEMO

Multitenant SaaS Apps In Rails By Iqbal Hasnan