Hotfix release available: 2025-05-14b "Librarian". upgrade now! [56.2] (what's this?)
Hotfix release available: 2025-05-14a "Librarian". upgrade now! [56.1] (what's this?)
New release available: 2025-05-14 "Librarian". upgrade now! [56] (what's this?)
Hotfix release available: 2024-02-06b "Kaos". upgrade now! [55.2] (what's this?)
Hotfix release available: 2024-02-06a "Kaos". upgrade now! [55.1] (what's this?)
New release available: 2024-02-06 "Kaos". upgrade now! [55] (what's this?)
Hotfix release available: 2023-04-04b "Jack Jackrum". upgrade now! [54.2] (what's this?)
Hotfix release available: 2023-04-04a "Jack Jackrum". upgrade now! [54.1] (what's this?)
New release available: 2023-04-04 "Jack Jackrum". upgrade now! [54] (what's this?)
Hotfix release available: 2022-07-31b "Igor". upgrade now! [53.1] (what's this?)
Hotfix release available: 2022-07-31a "Igor". upgrade now! [53] (what's this?)
New release available: 2022-07-31 "Igor". upgrade now! [52.2] (what's this?)
New release candidate 2 available: rc2022-06-26 "Igor". upgrade now! [52.1] (what's this?)
New release candidate available: 2022-06-26 "Igor". upgrade now! [52] (what's this?)
Hotfix release available: 2020-07-29a "Hogfather". upgrade now! [51.4] (what's this?)
New release available: 2020-07-29 "Hogfather". upgrade now! [51.3] (what's this?)
New release candidate 3 available: 2020-06-09 "Hogfather". upgrade now! [51.2] (what's this?)
New release candidate 2 available: 2020-06-01 "Hogfather". upgrade now! [51.1] (what's this?)
New release candidate available: 2020-06-01 "Hogfather". upgrade now! [51] (what's this?)
Hotfix release available: 2018-04-22c "Greebo". upgrade now! [50.3] (what's this?)
Hotfix release available: 2018-04-22b "Greebo". upgrade now! [50.2] (what's this?)
master_slave_db_support

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.

<environment>: #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: <username>
        password: <pwd>
        database: <master_db_name>
      - role: slave
        host: localhost
        pool: 5
        username: <username>
        password: <pwd>
        database: <slave_db_name>

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.

master_slave_db_support.txt · Last modified: 2018/08/31 16:16 (external edit)