-
Notifications
You must be signed in to change notification settings - Fork 505
Replication
Thiago Pradi edited this page Oct 29, 2017
·
23 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
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:
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
#This class is replicated, writes to master and reads to slave.
class Cat < ActiveRecord::Base
replicated_model()
end
```ruby
<h2> Multiples Slaves </h2>
Octopus supports multiples slaves. Octopus will pick one shard using a round robin algorithm.
<h2>Queries to specific shards </h2>
Also, if you want to send some queries to a specific slave or to a master, you could use normal sharding feature, examples:
```ruby
#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