Rails Database Optimizations You Can Do Now


Ruby on Rails depends on its database for just about everything it does. Characteristically the database performs well but it is also one of the first things that slow down a site, no matter what type of Rails hosting it is on. There are a few methods you can use to optimize your database:

Separate your data to multiple databases

Based on your site and the date it stores, it might make sense to separate your data into a second (or third) database. This can be the same database type as your main application, like MySQL, or something different like MongoDB. Common things to separate are logging or historic data. For example, with an e-commerce site, it might be useful to have all of your product prices in the database but you can keep your historic prices in a separate database since they aren’t used as often. Ruby on Rails has support for connecting to multiple databases so it is not too difficult to set up.

Move your database to a separate server

Moving your database to a separate server will let you add resources to the database itself without having to fight Rails for those resources. This may be performed with nearly all Rails hosting providers and is an excellent first step if you are trying to level up your website.

Setup a database cluster

At a certain point, you will need to set-up a database cluster. A database cluster is just a group of servers that all have a copy of your database running. Your website will then connect to a random server in the cluster to get its data. So if you have a database cluster with 3 servers, each server will handle 1/3 of the database traffic. The most common database cluster setup is master/slave, which means that there is one of your server set-ups as the master and the rest are considered slaves. The master server is the one who holds the original data and the slaves have copies of that data that they refresh every few minutes.

Turn off unnecessary database features

Most databases are configured with a bunch of extra features by default. Turning off these features can save you some resources on the server. Some common things to turn off are remote connections, unused database engines (MyISAM vs InnoDB), or optional authentication settings like LDAP.