Skip to content

thiagopradi/octopus_sharding_example

Repository files navigation

Octopus Sharding Example

This is a sample rails application using sharding with Octopus. This application have the User information on master database, and all informations after login are stored in the shard, based on `country` attribute of the current_user. Available shards are: brazil, mexico and canada.

How-to

Octopus is simple to use, just follow the steps:

- Add the gem to your Gemfile:
gem 'ar-octopus', '0.0.12', :require => "octopus"
  • Create a shards.yml file inside config directory, that looks like this:
development:
  shards:
    canada:
      host: localhost
      adapter: mysql
      database: canada_shard
    brazil:
      host: localhost
      adapter: mysql
      database: brazil_shard
    mexico:
      host: localhost
      adapter: mysql
      database: mexico_shard

After this, you need to select what shard to use. This could be done with around_filter in controller, or setting the shard manually. This code in application_controller.rb that selects the shard:

class ApplicationController < ActionController::Base
  around_filter :select_shard


  def select_shard()
    if user_signed_in?
      using(current_user.country.to_sym) { yield }
    else
      yield     
    end
  end
end

If the user isn't logged in, the shard will be the master. after this, it will pick the country attribute, passes to Octopus, and all queries will be sent to the selected shard. If you want to specify manually what shard to use, this is the syntax:

 User.using(:brazil).all 

Important!

Each ActiveRecord object knows where is the source shard. if you want to move objects between shards, you need to specify what shard to sent, like this:

 
# This will save the user in the brazil shard
@user = User.using(:brazil).first
@user.name = "New Name"
@user.save()

# This will move the data to another shard:
new_user = User.new
new_user.attributes = @user.attributes
new_user.save()

More info could be found at Octopus Wiki

Thanks

This project is sponsored by the Ruby Summer of Code, and my mentors Mike Perham and Amit Agarwal.

Copyright

Copyright (c) 2010 Thiago Pradi, released under the MIT license.

About

Sample Rails app using Octopus Library to Enable database Sharding on ActiveRecord

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published