Replication

Thiago Pradi edited this page Oct 29, 2017 · 20 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 your 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:
      host: 194.32.45.61
      adapter: mysql
      database: octopus_shard2
    slave2:
      host: 194.32.45.63
      adapter: mysql
      database: octopus_shard3
     
  production:
    slave3:
      host: 88.48.81.71
      adapter: mysql
      database: octopus_shard4
    slave4:
      host: 88.48.81.72
      adapter: mysql
      database: octopus_shard5

Partial Replication

If the application isn’t fully replicated, you will need to specify fully_replicated as false in your config file and then call replicated_model() inside of the models you want replicated:

Config File

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

  staging:
    slave1:
      host: 194.32.45.61
      adapter: mysql
      database: octopus_shard2
    slave2:
      host: 194.32.45.63
      adapter: mysql
      database: octopus_shard3

  production:
    slave3:
      host: 88.48.81.71
      adapter: mysql
      database: octopus_shard4
    slave4:
      host: 88.48.81.71
      adapter: mysql
      database: octopus_shard5

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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.