Founder @SoluteLabs, Traveller, Photographer, Passionate about new Startups and Tech
With the launch of Rails 6, one of the new features that were announced was of multiple database support.
This breather came to me when, in one of our applications, I was struggling to manage separate DBs efficiently. There are several reasons why one would want such support, such as:
When faced with using multiple Databases, usually, one serves as master and other servers as a slave. Because I was managing this before the release of rails 6, I was using a not-so-optimal (read patched/hacky) way to get this done. I created a separate YAML file for the second database to store the connection information. Here is a sample file:
second_database.yaml adapter: db_adapter encoding: db_encoding pool: db_pool username: db_username password: db_password database: db_name host: db_host
To access multiple databases, every time I would:
config = YAML.load_file(second_database.yaml') connection = ActiveRecord::Base.establish_connection( adapter: config['adapter'], host: config['host'], database: config['database'], username: config['username'], password: config['password'] )
employees = connection.execute("SELECT * from employees")
Such a headache!
But the release of the new Rails 6, you probably won’t have to go through the pain (and squirm at your own code) to manage multiple Databases.
As soon as Rails 6 was launched, I upgraded my application to leverage the benefits of multiple DB support. Let’s go step by step to understand the upgrade and set up for multiple databases.
Bingo, now you run on Rails 6!
Now let’s move further for multiple DB setup with rails 6.
With rails 6 multiple DB support, you can have more than one database with a replica (read-only copy) of each database.
Now, consider your rails application with a single primary database, and now after upgrading to Rails 6, you need to add a new database for some of the new tables. The current version of your database.yml looks like:
With rails 6; you can add a replica for your primary database and also can add a new database by updating your database.yml like this:
Consider a few points which you need to take care when using replica for your database:
to the database settings.
setting in the database.yml file as shown above. Though we have a replica database, we haven’t set a migration path for the same.
After this step, you will have a new database added; now, let’s set up a model for it. To use a new database, you need to add one abstract model class to your app; as shown below:
Now you can access the new database; now let’s briefly discuss multiple database features:
1. The automatic connection switching between primary and replica database:
To use a read-only database to the application, you need to configure middleware for automatic connection switching.
Automatic connection switching allows your application to switch between primary and replica databases based on HTTP request methods.
For example, if your application receives POST, PUT, DELETE, or PATCH request, the app automatically writes to the primary database. And, for the requests such as GET or HEAD, the application reads from the replica one.
To configure middleware for automatic connection switching; uncomment or add the following line to application config.
Rails only send GET or HEAD request to primary only if the read request is within what we configured above; by default, it will be set to 2 seconds. You have the liberty to change it as per your database infrastructure.
2. The manual connection switching between primary and replica database:
To connect to the replica or primary database manually; rails provide
Sometimes, your application needs to connect to primary or replica irrespective of the request type. In such cases, you can utilize the
method provided by ActiveRecord.
By using the above code, you can force your application to connect to the blogs’ database irrespective of the request type.
Now, this command uses the connection to read replicas of the blogs’ database and use it.
Here are they,
We can expect these features to come up soon and help us configure, use, and manage multiple databases with Rails 6 super effectively.
Create your free account to unlock your custom reading experience.