===== Master/Salve DB connection Support ===== Should be able to support master-salve DB setup (replication) for active record 4 (rails 4). All reads should go to slave and writes to master. ===== Options ==== We were using multi DB gem with rails 2 which is no more supported in rails 4 ( https://github.com/schoefmax/multi_db ) The following gems are evaluated. 1. https://github.com/taskrabbit/makara - Good. Automatically switches reads and writes and can be overridden easily. 2. https://github.com/tchandy/octopus - (Seems good but does not seem to automatically switching reads and writes well. Needs code changes but simple) 3. https://github.com/wbharding/seamless_database_pool - (Not rails 4 compatible) Chose makara gem as it seems to exactly does what we need and is being maintained actively. Octopus would be preferred over this incase we decide to go with sharding. ===== Implementation ====== makara gem is installed and code is committed. For now, there is no change in the database.yml and no additional proxy is used. database.yml can be modified as below to enable master-salve proxy. ==== Enabling multiple DB support on any environment ===== Change the database.yml like below. No code change is needed. : #development, staging, production adapter: makara_mysql2 encoding: utf8 reconnect: false socket: /var/lib/mysql/mysql.sock makara: blacklist_duration: 3 master_ttl: 5 sticky: true connections: - role: master host: localhost pool: 5 username: password: database: - role: slave host: localhost pool: 5 username: password: database: ===== Notes ===== 1. In case any situation where a read has to explicitly go to master, we can either use stick_to_master! method on the model connection OR override the default adapter with a new one. (Ex - http://www.blrice.net/blog/2014/12/06/scaling-rails-with-distributed-db-reads/) ** One common scenario where we may need to read from master is immediately after a record's creation. This is just incase replication does not work. So, reload method can be proxied to read from master. But as of now, this can be handled by using sticky option in the config(sticky: true). Did not find the need to adress this during the testing. So, left it unchanged.