alphagov / trade-tariff-backend

Enabling the population and distribution via API of UK Customs tariffs and duties
MIT License
7 stars 6 forks source link

Use slave db for reads #114

Closed saulius closed 10 years ago

saulius commented 10 years ago

This is regarding https://www.pivotaltracker.com/story/show/58300476

We cannot use https://github.com/bdurand/seamless_database_pool for master/slave database support, but Sequel has this functionality baked in. See http://sequel.rubyforge.org/rdoc/files/doc/sharding_rdoc.html#label-Master+and+Slave+Database+Configurations

So in order to make this work all we need to do is extend database.yml like so:

# config/database.yml

development:
  adapter: mysql2
  encoding: utf8
  reconnect: true
  database: tariff_development
  username: tariff
  password: tariff
  servers:
    :read_only:
      :host: 'slave_host'

Mind the servers hash, this is new. Then all reads will go through slave_host:

[1] pry(main)> Commodity.first
Sequel::DatabaseConnectionError: Mysql2::Error: Unknown MySQL server host 'slave_host' (25)

If there are more slave hosts we can do something similar to what Sequel guide suggests regarding DB.get_slave_host.

Ping @jabley @matthewford

jabley commented 10 years ago

Looks good. I've made a pull request in the deployment configuration.

matthewford commented 10 years ago

merged