octopus icon indicating copy to clipboard operation
octopus copied to clipboard

Default writing/reading to master and explicitly control when to delegate reading to slave

Open alexhanh opened this issue 10 years ago • 5 comments

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?

alexhanh avatar Aug 18 '15 13:08 alexhanh

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

pbechu avatar Sep 02 '15 07:09 pbechu

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?

alexhanh avatar Sep 04 '15 13:09 alexhanh

No answer to the last question yet? It's a use case we care too...

knightq avatar May 27 '16 08:05 knightq

@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

danmaz74 avatar Nov 04 '17 11:11 danmaz74

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

tomchapin avatar Oct 20 '20 23:10 tomchapin