Reputation: 48450
I have a very straight forward master/slave replication setup with MySQL. ActiveRecord is configured to hit the master hostname as would be expected in database.yml. My question is, is it currently possible to set an option for ActiveRecord to do all reads from a slave and leave writes to the master? I'm using Ruby on Rails/ActiveRecord 3.1.3. I haven't found any options to perform the following, just a single host entry field in database.yml. Is this currently possible?
Upvotes: 3
Views: 7010
Reputation: 540
We also had the same requirement. Currently, this is not possible in ActiveRecord.
We spiked out 2 gems, Octopus and Makara. I have written a blog comparing these 2 gems : https://ypoonawala.wordpress.com/2015/11/15/octopus-vs-makara-read-write-adapters-for-activerecord-2/
In my opinion, Makara works well and makes up for the issues with Octopus.
Upvotes: 0
Reputation: 11942
There is the makara gem. It sends all SELECT
statements to slaves and write operations to master.
Add this like to your Gemfile
:
gem 'makara'
And then configure your database.ml like this:
production:
adapter: 'mysql2_makara'
database: 'MyAppProduction'
# any other standard AR configurations
# add a makara subconfig
makara:
# the following are default values
blacklist_duration: 5
master_ttl: 5
sticky: true
# list your connections with the override values (they're merged into the top-level config)
# be sure to provide the role if master, role is assumed to be a slave if not provided
connections:
- role: master
host: master.sql.host
- role: slave
host: slave1.sql.host
- role: slave
host: slave2.sql.host
Upvotes: 2
Reputation: 19485
Yes this is possible, but you need to use a gem for it; it's not built into Rails itself.
I use seamless database pool currently and it works great. Octopus is another alternative.
Upvotes: 4