octopus
octopus copied to clipboard
Default writing/reading to master and explicitly control when to delegate reading to slave
The default replication mode with octopus is to send all writes queries to master, and read queries to slaves. However, in our case, we would want to stick to the default behaviour of reading/writing to the master unless we explicitly state so.
The reason is that we don't want to go through the whole application and update each query with .using(:master) blocks. We also are unsure what side effects octopus might bring to rake and background (DelayedJob) tasks.
Can this be somehow achieved?
Hi, I recently discovered this gem but after few tests, i think you can use it this way by NOT setting replicated and fully_replicated in the config file. See the wiki here: https://github.com/tchandy/octopus/wiki/Config-File Have a good day, PB
Hey @pbechu,
Thanks a lot for the tip! Considering Rails reuses connections inside the same process (see http://stackoverflow.com/a/32303585/377920), what would happen after the instance that used Octopus.using(:slave) would get another request? Is there any possibility that the same instance, AR or Octopus would re-use the slave connection, even though we don't explicitly call Octopus.using(:slave) on that request?
No answer to the last question yet? It's a use case we care too...
@thiagopradi so, is not using "replicated: true" the right way for this use case? If it is, I can update the docs and create a pull request.
Thx
It's not at all obvious how to do this (and it doesn't appear to be documented anywhere), but the way how to do this is to set replicated to true, fully_replicated to false, and then simply toggle on replication mode dynamically in your code wherever you want to explicitly use it, like so:
Example Config File:
octopus:
replicated: true
fully_replicated: false
verify_connection: true
environments:
- production
- development
production:
read_replica:
adapter: mysql2
database: db_name
username: db_username
password: db_password
host: db_hostname
development:
read_replica:
adapter: mysql2
database: db_name
username: db_username
password: db_password
host: db_hostname
Example Ruby code:
Octopus.fully_replicated do
# Code that reads from the replica, but writes to the master
end