Multiple Data Centers for your Rails App: Database Edition

So you have a Rails Application. And you decide that you don’t want to be the chum who puts all his eggs in one basket and every time your data center goes down. Luckily for you, there’s some cost-effective solutions to move you closer to your goal of 99.99% uptime.

Let’s start with your database needs.  How do I have a database in two data centers and keep it all in sync? Well to start, you’ll need a special database setup that allows for Multi-Master Replication. This means that when you write to DB Master 1, it replicates to DB Master 2.



Now, you can in theory run your application in both data centers with no problems right? Wrong. You need to ensure that your application is setup for multi-master. A good quote from the Scale Out blog on the subject:

For example, accounting systems need to generate unbroken sequences of numbers for invoices.  A common approach is to use a table that holds the next invoice number and increment it in the same transaction that creates a new invoice.  Another accounting example is reports that need to read the value of accounts consistently, for example at monthly close.  Neither example works off-the-shelf in a multi-master system with asynchronous replication, as they both require some form of synchronization to ensure global consistency across masters. “

There’s a few options out there that will give you this kind of multi-master replication. My personal favorite is Continuent Tungsten which is a clustered MySQL offering that is highly available and takes data integrity and availability very seriously. It’s also dead simple to setup and manage (Ruby based!). Their support team is unmatched, some of the most responsive and professional I’ve ever worked with.  Percona XtraDB Cluster is another option, which is another great option especially if you need to do synchronous replication in additional to asynchronous cross-data center replication.

Some new comers to the game that offer hosted, replicated MySQL include GenieDB and ClearDB.  In my experience, the cost of the hosted solutions when you have a significant amount of data can become cost-prohibitive, so if you have the man power checkout the self-hosted solutions first. However if you’re not hosting any other services, this is a great way to continue your life without sys admin duties. Beware though, GenieDB doesn’t use native MySQL so there might be some migration work required to work correctly with their system. ClearDB on the otherhand claims it’s native MySQL.

How do you get started with finding a replicated database solution? Talk to the vendors and test some out. I recommend running it in a staging-like environment for a few weeks before moving onto it so you catch any bugs related to the replicated setup.

Leave a Reply

Your email address will not be published. Required fields are marked *