Search code examples
ruby-on-railsrubypostgresqlreplicationoctopus

Rails Postgresql replication via Octopus gem when in Development env


Apparently when using the Octopus gem to do Postgres replication everything should be plug and play. However I can't seem to find what I'm doing wrong.

This is my config/shards.yml

octopus:
  environments:
    - development
  replicated: true
  development:
    slave1:
      adapter: postgresql
      host: localhost
      database: slaveapp_development
      username: pguser
      password: pgpass

The AR model Provider(I create the exact same tables in each app via Rake tasks) I'd like to sync/replicate to my slave:

class Provider < ActiveRecord::Base
  has_many :products
  replicated_model()
end

I boot both apps via Rails server and enter Masterapp's console and from there:

> Provider.using(:slave1).create({provider_params...})  
#=> works! I get a new record in slave1's DB.
> Provider.using(:master).create({provider_params...})  
#=> works partly. Creates record only in master's DB.

The problem is that when calling Provider.using(:master)... I'm expecting:

1 - Create record at master's DB.

2 - Replicate same record at slave1's DB. <--- This is NOT happening.


Solution

  • That is not the purpose of the Octopus gem.

    It redirects the database requests, depending on whether it is a read or write operation. That way you can use your Rails models without thinking about the current database connection and if it fits the intended operation.

    It does not copy the data to the slaves.

    To perform the actual replication, i.e. the data transfer from the master to the slaves, you have to set it up yourself.

    There are several options with PostgreSQL. If you are using a newer version (9.1+) you can use the integrated streaming replication in "hot standby" mode. There are tutorials on how to set it up, e.g.

    If you are stuck with an older version of PostgreSQL have a look at the alternatives.