-
Notifications
You must be signed in to change notification settings - Fork 504
Sharding
tchandy edited this page Sep 14, 2010
·
10 revisions
Octopus allows you to do Database Sharding. This means you could send some queries to specific shards, or pick one shard, and use it as the default database for your application.
Sending a query to a specific shard:
#This will create a new user in "canada" shard User.using(:canada).create!(:name => 'oi')
The Api also allows dynamically change the shard:
#This will create a new user in "canada" shard User.using(:canada).using(:master).using(:canada).create!(:name => 'oi')
You could also pass a block to #using method, and #using method will send all queries inside the block to a specific shard:
#This will create a new user and a new client in "canada" shard Octopus.using(:canada) do User.create(:name => "User") Client.create(:name => "Client") end
the using method is also available for User instances:
class User < ActiveRecord::Base def awesome_queries Octopus.using(:canada) do User.create(:name => "teste") Dog.create(:name => "Xpto") end end end # This will create a new user and a new dog in the canada shard: u = User.new u.awesome_queries()
The shard could also be specify in the ActionController or Any subclass. All resquests from that controller will be executed in the selected shard.
class ApplicationController < ActionController::Base around_filter :select_shard def select_shard(&block) Octopus.using(current_user.country, &block) end end