SlideShare una empresa de Scribd logo
1 de 44
Descargar para leer sin conexión
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 developing Ruby on Rails
applications
● also AngularJS, Backbone, Chef, bash,
anything needed
● 10+ projects
● work with clients
This talk and presentation
●
●
●
●
●
●

50 lines of source code,
1 meme,
45 slides,
30-40 minutes,
ask questions during the presentation,
feedback welcome
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 everything,
4. Deploy a copy of the blog engine there.
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 + (...)
Multiple instances
Multitenancy
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
Multitenancy
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
PostgreSQL schemas
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>
PostgreSQL schemas

● every tenant uses its own schema,
● tables with common data are not in schemas
( in fact they are in ‘public’ schema )
PostgreSQL schemas
PostgreSQL schemas
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
PostgreSQL schemas
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
PostgreSQL schemas
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
PostgreSQL schemas
blogs=# SELECT * FROM administrators;
name
-----John
(1 row)
PostgreSQL schemas
blogs=# SELECT * FROM angularjs_tips.posts;
title | content
---------------+----------Scopes basics | blah blah
(1 row)
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
----------------------+----------Active Record basics | blah blah
(1 row)
PostgreSQL schemas
Practice
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
Practice
Wrocław

Rails
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
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 about security
subdomains and schemas in testing
environment (lots of helpers needed),
● rescue, sidekiq
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (8)

Day 2 - Intro to Rails
Day 2 - Intro to RailsDay 2 - Intro to Rails
Day 2 - Intro to Rails
 
Scrapy
ScrapyScrapy
Scrapy
 
CouchApp - Build scalable web applications and relax
CouchApp - Build scalable web applications and relaxCouchApp - Build scalable web applications and relax
CouchApp - Build scalable web applications and relax
 
Breaking The Cross Domain Barrier
Breaking The Cross Domain BarrierBreaking The Cross Domain Barrier
Breaking The Cross Domain Barrier
 
Day 8 - jRuby
Day 8 - jRubyDay 8 - jRuby
Day 8 - jRuby
 
node.js - Fast event based web application development
node.js - Fast event based web application developmentnode.js - Fast event based web application development
node.js - Fast event based web application development
 
Django and Mongoengine
Django and MongoengineDjango and Mongoengine
Django and Mongoengine
 
Django rest framework
Django rest frameworkDjango rest framework
Django rest framework
 

Similar a Krug 02 2014

Beginning WordPress Workshop
Beginning WordPress WorkshopBeginning WordPress Workshop
Beginning WordPress WorkshopThe Toolbox, Inc.
 
Introduction to rails
Introduction to railsIntroduction to rails
Introduction to railsGo Asgard
 
Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016Mukesh Tilokani
 
SHOW202: How to customize Lotus Quickr Templates Using HTML, Javascript and C...
SHOW202: How to customize Lotus Quickr Templates Using HTML, Javascript and C...SHOW202: How to customize Lotus Quickr Templates Using HTML, Javascript and C...
SHOW202: How to customize Lotus Quickr Templates Using HTML, Javascript and C...Brian O'Gorman
 
My first powershell script
My first powershell scriptMy first powershell script
My first powershell scriptDavid Cobb
 
Architektura html, css i javascript - Jan Kraus
Architektura html, css i javascript - Jan KrausArchitektura html, css i javascript - Jan Kraus
Architektura html, css i javascript - Jan KrausWomen in Technology Poland
 
How to build a PostgreSQL-backed website quickly
How to build a PostgreSQL-backed website quicklyHow to build a PostgreSQL-backed website quickly
How to build a PostgreSQL-backed website quicklyJohn Ashmead
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme EnlightenmentAmanda Giles
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017Amanda Giles
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBMongoDB
 
Building your first app with mongo db
Building your first app with mongo dbBuilding your first app with mongo db
Building your first app with mongo dbMongoDB
 
Bye bye $GLOBALS['TYPO3_DB']
Bye bye $GLOBALS['TYPO3_DB']Bye bye $GLOBALS['TYPO3_DB']
Bye bye $GLOBALS['TYPO3_DB']Jan Helke
 
Jbake workshop (Greach 2019)
Jbake workshop (Greach 2019)Jbake workshop (Greach 2019)
Jbake workshop (Greach 2019)Mario García
 
How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2Federico Razzoli
 
2015 WordCamp Maine Keynote
2015 WordCamp Maine Keynote2015 WordCamp Maine Keynote
2015 WordCamp Maine KeynoteScott Taylor
 
Web Development with Python and Django
Web Development with Python and DjangoWeb Development with Python and Django
Web Development with Python and DjangoMichael Pirnat
 

Similar a Krug 02 2014 (20)

Beginning WordPress Workshop
Beginning WordPress WorkshopBeginning WordPress Workshop
Beginning WordPress Workshop
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
 
Introduction to rails
Introduction to railsIntroduction to rails
Introduction to rails
 
Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016
 
SHOW202: How to customize Lotus Quickr Templates Using HTML, Javascript and C...
SHOW202: How to customize Lotus Quickr Templates Using HTML, Javascript and C...SHOW202: How to customize Lotus Quickr Templates Using HTML, Javascript and C...
SHOW202: How to customize Lotus Quickr Templates Using HTML, Javascript and C...
 
My first powershell script
My first powershell scriptMy first powershell script
My first powershell script
 
Architektura html, css i javascript - Jan Kraus
Architektura html, css i javascript - Jan KrausArchitektura html, css i javascript - Jan Kraus
Architektura html, css i javascript - Jan Kraus
 
How to build a PostgreSQL-backed website quickly
How to build a PostgreSQL-backed website quicklyHow to build a PostgreSQL-backed website quickly
How to build a PostgreSQL-backed website quickly
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 
The emerging world of mongo db csp
The emerging world of mongo db   cspThe emerging world of mongo db   csp
The emerging world of mongo db csp
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
 
Building your first app with mongo db
Building your first app with mongo dbBuilding your first app with mongo db
Building your first app with mongo db
 
Bye bye $GLOBALS['TYPO3_DB']
Bye bye $GLOBALS['TYPO3_DB']Bye bye $GLOBALS['TYPO3_DB']
Bye bye $GLOBALS['TYPO3_DB']
 
Jbake workshop (Greach 2019)
Jbake workshop (Greach 2019)Jbake workshop (Greach 2019)
Jbake workshop (Greach 2019)
 
How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2
 
Week3 adb
Week3 adbWeek3 adb
Week3 adb
 
&lt;?php + WordPress
&lt;?php + WordPress&lt;?php + WordPress
&lt;?php + WordPress
 
2015 WordCamp Maine Keynote
2015 WordCamp Maine Keynote2015 WordCamp Maine Keynote
2015 WordCamp Maine Keynote
 
Web Development with Python and Django
Web Development with Python and DjangoWeb Development with Python and Django
Web Development with Python and Django
 

Krug 02 2014