Skip to content
xxx edited this page Jan 30, 2012 · 13 revisions

If you have replicated database configured in shards.yml (see Config File for more info), Octopus will send all writes to master, and all reads to slaves. You should specify if you application is fully replicated, or just some models are replicated. For default, Octopus will assume that your application is fully replicated. Here is how your config file should look:

octopus:
  replicated: true
  environments:
    - staging
    - production

  staging:
    slave1:
      database: octopus_shard2
      <<: *mysql
    slave2:
      database: octopus_shard3
      <<: *mysql

  production:
    slave3:
      database: octopus_shard4
      <<: *mysql
    slave4:
      database: octopus_shard5
      <<: *mysql

Partial Replication

If the application isn’t fully replicated, you need to specify on your config file, and also in your models what will be replicated:

Config File

octopus:
  replicated: true
  fully_replicated: false
  environments:
    - staging
    - production

  staging:
    slave1:
      database: octopus_shard2
      <<: *mysql
    slave2:
      database: octopus_shard3
      <<: *mysql

  production:
    slave3:
      database: octopus_shard4
      <<: *mysql
    slave4:
      database: octopus_shard5
      <<: *mysql

Class Syntax

#This class is replicated, writes to master and reads to slave.
class Cat < ActiveRecord::Base
  replicated_model()
end

Multiples Slaves

Octopus supports multiples slaves. Octopus will pick one shard using a round robin algorithm.

Queries to specific shards

Also, if you want to send some queries to a specific slave or to a master, you could use normal sharding feature, examples:


#sends the query to master
User.using(:master).count

#Sends the query to specific slave
User.using(:slave_1).count

Octopus.using(:slave_1) do
User.count
end