tags: Databases, Ruby on Rails, Scaling date: 2011-02-28 20:58:51.000000000Z


Seriously wtf is database sharding?

Database sharding is a method of horizontal partitioning in a database or search engine. Each individual partition is referred to as a shard or database shard.

Basically => Multiple databases in your rails application.

Why/when should i care about sharding my database?

/lost image/

  • If you have tables with massive reads/writes you should shard this table to separete database, octopus can help you
  • If you need to replicate your database, octopus can help you
  • If you need to move data along databases, octopus can help you

Setting up

Can’t be easier:

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

and call the bundler:

  bundle install

Now you need to create a config file to tell your application which shards you want, a simple yaml config/shards.yaml:

      adapter: sqlite3
      database: db/db_one.sqlite3
      pool: 5
      timeout: 5000

        adapter: postgresql
        username: postgres
        database: db_two
        encoding: unicode

Easy as pie, this is pretty simple, if you need to group or something like that, please thake a look here

##Time to code, ohay!

##Dynamic change your shard looking for something in your database

  Project.where(:name => "stjhimyblog").using(:shard_sqlite)
  Project.where(:name => "stjhimyblog").using(:shard_pgsql)

You can also use a block, just nice:

  Octopus.using(:shard_sqlite) do

This feature is nice when you want to use a specific shard in only part of the project, maybe some report, usually big queries.

##Specific model, specific database

You can also tell your model a default db to use:

  class Project < ActiveRecord::Base
    octopus_establish_connection(:adapter => "sqlite3", :database => "db_one")


using method is also available in your migrations:

  class CreateSomeRandomProjects < ActiveRecord::Migration

    def self.up
      Project.create!(:name => "stjhimyblog")

    def self.down

Last words

Octopus is a pretty solid gem, the wiki/documentation is very detailed and the features just great. If someday you need to do some database replication/sharding you should give octopus a shot.

Again, the official repository here