Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Tomasz Giereś

Multi-tenancy in Rails
with PostgreSQL
Schemas
Me
●
●
●
●
●

Tomasz Giereś
tomaszgieres (github/twitter)
tomasz.gieres@gmail.com
almost 2 years at Lunar Logic
mainly dev...
This talk and presentation
●
●
●
●
●
●

50 lines of source code,
1 meme,
45 slides,
30-40 minutes,
ask questions during th...
Meme
schema != schema.rb
The problem

1. You have a blog.
2. Your friend wants a blog too.
Multiple instances
Multiple instances
1. Rent an another server / create new heroku
application / whatever,
2. Buy a domain,
3. Configure eve...
Multiple instances
● Very easy at the beginning,
● fixed price per instance* (server / hardware /
database / licences / ex...
Multiple instances
Multitenancy
Multitenancy
● One application instance serves all the
blogs,
● One database for all the blogs,
● No need to pay for the e...
Multitenancy
Multitenancy
1. Add root element to data structures:
● User has a blog_id
● Post has a blog_id
● etc.
2. At storage and co...
PostgreSQL schemas
PostgreSQL schemas
● a namespace
● a group of tables
● an extra layer between a database and a
table
● just an extra dot :...
PostgreSQL schemas

● every tenant uses its own schema,
● tables with common data are not in schemas
( in fact they are in...
PostgreSQL schemas
PostgreSQL schemas
PostgreSQL schemas
another_database=# CREATE DATABASE blogs OWNER postgres;
CREATE DATABASE
blogs=# CREATE TABLE administr...
PostgreSQL schemas
PostgreSQL schemas
blogs=# CREATE SCHEMA rails_tips;
CREATE SCHEMA
blogs=# CREATE TABLE rails_tips.posts ( title varchar(4...
PostgreSQL schemas
PostgreSQL schemas
blogs=# CREATE SCHEMA angularjs_tips;
CREATE SCHEMA
blogs=# CREATE TABLE angularjs_tips.posts ( title v...
PostgreSQL schemas
blogs=# SELECT * FROM administrators;
name
-----John
(1 row)
PostgreSQL schemas
blogs=# SELECT * FROM angularjs_tips.posts;
title | content
---------------+----------Scopes basics | b...
PostgreSQL schemas

Repeating schema name
all the time is annyoing.
PostgreSQL schemas
united_states=# SELECT * FROM posts;
ERROR: relation "posts" does not exist
LINE 1: SELECT * FROM posts...
PostgreSQL schemas
blogs=# SHOW search_path;
search_path
---------------"$user",public
(1 row)
PostgreSQL schemas
blogs=# SET search_path TO rails_tips, public;
SET
blogs=# SELECT * FROM posts;
title
| content
-------...
PostgreSQL schemas
Practice
Practice
● usually used with subdomains,
● a client organization / a workspace / any
isolated unit = tenant ?
● simpler ap...
Practice
Wrocław

Rails
Rails
Active Record + pg gem:
● supports schemas (create, drop, list),
● can manipulate search path,
● people prefer to ex...
Rails
From Railscast:
#389 Multitenancy with PostgreSQL
with some modifications
Rails
Rails
Rails
It’s common to set search path
and current tenant
in rack middleware
Rails
Migrations
Rails
Useful information
●
●
●
●
●

influitive / apartment (gem)
pow server
classes and methods to work in console
don’t forget ...
Resources
●
●
●
●
●
●

http://www.postgresql.org/docs/9.3/static/ddl-schemas.html
http://timnew.github.io/blog/2012/07/17/...
Upcoming SlideShare
Loading in …5
×

Krug 02 2014

1,577 views

Published on

  • Be the first to comment

Krug 02 2014

  1. 1. Tomasz Giereś Multi-tenancy in Rails with PostgreSQL Schemas
  2. 2. Me ● ● ● ● ● Tomasz Giereś tomaszgieres (github/twitter) tomasz.gieres@gmail.com almost 2 years at Lunar Logic mainly developing Ruby on Rails applications ● also AngularJS, Backbone, Chef, bash, anything needed ● 10+ projects ● work with clients
  3. 3. This talk and presentation ● ● ● ● ● ● 50 lines of source code, 1 meme, 45 slides, 30-40 minutes, ask questions during the presentation, feedback welcome
  4. 4. Meme
  5. 5. schema != schema.rb
  6. 6. The problem 1. You have a blog. 2. Your friend wants a blog too.
  7. 7. Multiple instances
  8. 8. Multiple instances 1. Rent an another server / create new heroku application / whatever, 2. Buy a domain, 3. Configure everything, 4. Deploy a copy of the blog engine there.
  9. 9. Multiple instances ● Very easy at the beginning, ● fixed price per instance* (server / hardware / database / licences / external services ), ● $100 to host a blog with 50 000 UU for you, ● $100 to host a blog with 5 UU for your friend, ● $100 dollars for every new instance, ● deploy, administrate, backup separately, ● separate admin panels etc., no easy way to i.e. collect data from several applications * Instance = application process(es) + database + (...)
  10. 10. Multiple instances
  11. 11. Multitenancy
  12. 12. Multitenancy ● One application instance serves all the blogs, ● One database for all the blogs, ● No need to pay for the external services separately?, ● Easier to scale: ○ count traffic for all the blogs, ○ just add more heroku dynos or application processes when needed
  13. 13. Multitenancy
  14. 14. Multitenancy 1. Add root element to data structures: ● User has a blog_id ● Post has a blog_id ● etc. 2. At storage and configuration level ● keep the data for every tenant in a separate storage ● dynamically switch the storage before accessing the data
  15. 15. PostgreSQL schemas
  16. 16. PostgreSQL schemas ● a namespace ● a group of tables ● an extra layer between a database and a table ● just an extra dot :) ○ <table_name>.<column_name> ○ <schema_name>.<table_name>.<column_name>
  17. 17. PostgreSQL schemas ● every tenant uses its own schema, ● tables with common data are not in schemas ( in fact they are in ‘public’ schema )
  18. 18. PostgreSQL schemas
  19. 19. PostgreSQL schemas
  20. 20. PostgreSQL schemas another_database=# CREATE DATABASE blogs OWNER postgres; CREATE DATABASE blogs=# CREATE TABLE administrators (name varchar(40)); CREATE TABLE blogs=# INSERT INTO administrators (name) VALUES ('John'); INSERT 0 1
  21. 21. PostgreSQL schemas
  22. 22. PostgreSQL schemas blogs=# CREATE SCHEMA rails_tips; CREATE SCHEMA blogs=# CREATE TABLE rails_tips.posts ( title varchar(40), content text ); CREATE TABLE blogs=# INSERT INTO rails_tips.posts (title, content) VALUES ('Active Record basics', 'blah blah'); INSERT 0 1
  23. 23. PostgreSQL schemas
  24. 24. PostgreSQL schemas blogs=# CREATE SCHEMA angularjs_tips; CREATE SCHEMA blogs=# CREATE TABLE angularjs_tips.posts ( title varchar(40), content text ); CREATE TABLE blogs=# INSERT INTO angularjs_tips.posts (title, content) VALUES ('Active Record basics', 'blah blah'); INSERT 0 1
  25. 25. PostgreSQL schemas blogs=# SELECT * FROM administrators; name -----John (1 row)
  26. 26. PostgreSQL schemas blogs=# SELECT * FROM angularjs_tips.posts; title | content ---------------+----------Scopes basics | blah blah (1 row)
  27. 27. PostgreSQL schemas Repeating schema name all the time is annyoing.
  28. 28. PostgreSQL schemas united_states=# SELECT * FROM posts; ERROR: relation "posts" does not exist LINE 1: SELECT * FROM posts; ^
  29. 29. PostgreSQL schemas blogs=# SHOW search_path; search_path ---------------"$user",public (1 row)
  30. 30. PostgreSQL schemas blogs=# SET search_path TO rails_tips, public; SET blogs=# SELECT * FROM posts; title | content ----------------------+----------Active Record basics | blah blah (1 row)
  31. 31. PostgreSQL schemas
  32. 32. Practice
  33. 33. Practice ● usually used with subdomains, ● a client organization / a workspace / any isolated unit = tenant ? ● simpler application design and architecture, ● less chance for the data leak
  34. 34. Practice
  35. 35. Wrocław Rails
  36. 36. Rails Active Record + pg gem: ● supports schemas (create, drop, list), ● can manipulate search path, ● people prefer to execute SQL statements anyway, ● supports the same operations on the tables within schema as on the public ones
  37. 37. Rails From Railscast: #389 Multitenancy with PostgreSQL with some modifications
  38. 38. Rails
  39. 39. Rails
  40. 40. Rails It’s common to set search path and current tenant in rack middleware
  41. 41. Rails Migrations
  42. 42. Rails
  43. 43. Useful information ● ● ● ● ● influitive / apartment (gem) pow server classes and methods to work in console don’t forget about security subdomains and schemas in testing environment (lots of helpers needed), ● rescue, sidekiq
  44. 44. Resources ● ● ● ● ● ● http://www.postgresql.org/docs/9.3/static/ddl-schemas.html http://timnew.github.io/blog/2012/07/17/use-postgres-multiple-schemadatabase-in-rails/ http://blog.jerodsanto.net/2011/07/building-multi-tenant-rails-apps-withpostgresql-schemas/ http://railscasts.com/episodes/123-subdomains-revised http://railscasts.com/episodes/388-multitenancy-with-scopes http://railscasts.com/episodes/389-multitenancy-with-postgresql

×