Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Database Sharding for ActiveRecord

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


Octopus - Easy Database Sharding for ActiveRecord

Octopus is a better way to do Database Sharding in ActiveRecord. Sharding allows multiple databases in the same rails application. While there are several projects that implement Sharding (e.g. DbCharmer, DataFabric, MultiDb), each project has its own limitations. The main goal of octopus project is to provide a nice and clean way of doing Database Sharding.

Feature list:

The design of the api is made to be simple as possible. Octopus is focusing in the end user, giving the power of multiple databases, but with reliable code and flexibility. Octopus is focused on Rails 3, but is compatible with Rails 2.x.

Octopus supports:

  • Sharding (with multiple shards, and grouped shards).
  • Replication (Master/slave support, with multiple slaves).
  • Moving data between shards with migrations.
  • Tools to manage database configurations. (soon)


When using replication, all writes queries will be sent to master, and read queries to slaves. More info could be found at: Wiki


When using sharding, you need to specify what shard the query will be sent. Octopus support selecting the shard inside a controller, or manually in each object. More could be found at Wiki

Replication + Sharding

Replication + Sharding isn't supported yet. This is in our todo, and will be done ASAP. if you need, feel free to fork and implement it.


Rails 2.x

Install the octopus gem:

 sudo gem install ar-octopus 

Add this line to enviroment.rb:

config.gem 'ar-octopus', :lib => "octopus"

Rails 3.x

Add this line to Gemfile:

gem 'ar-octopus', :require => "octopus"

Runs a bundle install:

bundle install

How to use Octopus?

First, you need to create a config file, shards.yml, inside your config/ directory. to see the syntax and how this file should look, please checkout this page on wiki.


Octopus adds a method to each AR Class and object. the using method is used to select the shard, like this:

User.where(:name => "Thiago").limit(3).using(:slave_one) 

Octopus also supports queries inside block. When you pass a block to the using method, all queries inside the block will be sent to the specified shard.

Octopus.using(:slave_two) do 
  User.create(:name => "Mike")

Each object knows where is your shard, so you could to thinks like this:

# This will find the user in the shard1
@user = User.using(:shard1).find_by_name("Joao")

# This will find the user in the master database
@user2 = User.find_by_name("Jose")

#Sets the name = "Mike"

# Save the user in the correct shard, shard1.


In migrations, you also have access to the using method. The syntax is basically the same. This migration will run in brazil and canada shards.

  class CreateUsersOnBothShards  "Both")

    def self.down

You also could send a migration to a group of shards. This migration will be sent to all shards that belongs to history_shards group, specified in shards.yml:

  class CreateUsersOnMultiplesGroups  "MultipleGroup")

    def self.down

Rails Controllers

If you want to send a specified action, or all actions from a controller, to a specific shard, use this syntax:

  class ApplicationController 

To see the complete list of features and syntax, please check out our Wiki Wanna see sample rails applications using octopus features? please check it out: Sharding Example and Replication Example. Also, we have a example that shows how to use Octopus without Rails: Octopus + Sinatra Example.

Contributing with Octopus

Contributors are welcome! To run the test suite, you need mysql, postgresql and sqlite3 installed. In order to install necessary gems, this is what you need to setup your Octopus development environment:

  git clone
  cd octopus
  bundle install
  rake db:prepare
  rake spec

To run our integrations tests inside sample_app, you need to following commands:

  cd sample_app
  bundle install

If you are having issues with octopus spec suite, check all databases password inside the config files, and make sure that your passwords/permissions are right.


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


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

Something went wrong with that request. Please try again.