0

I'm looking at a way of achieving fail over and additionally load balancing over three databases in my Rails application.

The code that follows currently works with no issues, however if db_1 is down then my application dies.

I would like to use db_1 as the primary and the other two db_2 and db_3 as a fail over.

Additionally I would like to load balance them but that is a less important requirement.

config/database.yml

db_1:
  adapter: mysql2
  reconnect: false
  pool: 5
  username: <username>
  password: <password>
  database: database_test
  host: 101.101.101.1

db_2:
  adapter: mysql2
  reconnect: false
  pool: 5
  username: <username>
  password: <password>
  database: database_test
  host: 101.101.101.2

db_3:
  adapter: mysql2
  reconnect: false
  pool: 5
  username: <username>
  password: <password>
  database: database_test
  host: 101.101.101.3

app/models/ext_databases.rb

class ExtDatabases < ActiveRecord::Base
        self.abstract_class = true
        establish_connection :db_1
end

app/models/users.rb

class Users < ExtDatabases
        Users.table_name = "ext_users"
end

2 Answers 2

1

That should be handled in MySQL side. You have a few options with MySQL like active-passive (replication), master-slave, cluster or sharding. Use those keywords on google to find the official MySQL documentation. Each strategies have their own advantages and disadvantages, and you need to understand them before a decision.

Sign up to request clarification or add additional context in comments.

Comments

1

MySQL already has a tool to do automatic failover, and it's aptly named mysqlfailover

This utility permits users to perform replication health monitoring and automatic failover on a replication topology consisting of a master and its slaves. The utility is designed to run interactively or continuously refreshing the health information at periodic intervals. Its primary mission is to monitor the master for failure and when a failure occurs, execute failover to the best slave available. The utility accepts a list of slaves to be considered the candidate slave.

Of course that means you do need to setup mysql replication but judging by the tone of your question you seem to have done so already.

mysqlfailover isn't the only option. Percona, a database that's fully compatible with mysql but independently developed has their own thing:

https://www.percona.com/doc/percona-xtradb-cluster/5.5/manual/failover.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.