Read my latest article: Was away on vacation (posted Sun, 11 May 2008 22:33:00 GMT)

Master/Slave Databases with Ruby on Rails 5

Posted by Robby Russell Thu, 15 Nov 2007 22:02:00 GMT

Not terribly long ago, I announced Active Delegate, which was a really lightweight plugin that I developed to allow models to talk to multiple databases for specific methods. The plugin worked great for really simple situations, like individual models.. but when it came time to test with associations it fell apart. I haven’t had a chance to work on any updates and knew that it was going to take more work to get it going.

Earlier this week, we helped one of our bigger clients launch their new web site1. For the deployment, we needed to send all writes to a master database and a reads to slaves (initial deployment is talking to almost 10 slaves spread around the globe!). We needed something to get integrated quickly and decided to ditch Active Delegate for the time being and began looking at the following options.

I spoke with Rick Olson2 and he pointed me to a new plugin that he hasn’t really released yet. So, I’m going to do him a favor and announce it for him. Of course… I got his permission first… ;-)

Announcing Masochism!

Masochism3 is a new plugin for Ruby on Rails that allows you to delegate all writes to a master database and reads to a slave database. The configuration process is just a few lines in your environment file and the plugin takes care of the rest.

Installing Masochism

With piston, you can import Masochism with:


  $ cd vendor/plugins
  $ piston import http://ar-code.svn.engineyard.com/plugins/masochism/

You can also install it with the old-fashioned way:


  $ ./script/plugin install -x http://ar-code.svn.engineyard.com/plugins/masochism/

Configuring Masochism

The first thing that you’ll need to do is add another database connection in config/database.yml for master_database. By default, Masochism expects you to have a production database, which will be the read-only/slave database. The master_database will be the connection details for your (you guessed it…) master database.


# config/database.yml  
production:
  database: masochism_slave_database
  adapter: postgresql
  host: slavedb1.hostname.tld
  ...

master_database:
  database: masochism_master_database
  adapter: postgresql
  host: masterdb.hostname.tld
  ...

The idea here is that replication will be handled elsewhere and your application can reap the benefits of talking to the slave database for all of it’s read-only operations and let the master database(s) spend their time writing data.

The next step is to set this up in your environment file. In our scenario, this was config/environments/production.rb.


# Add this to config/environments/production.rb
config.after_initialize do 
  ActiveReload::ConnectionProxy.setup!    
end


Voila, you should be good to go now. As I mentioned, we’ve only been using this for this past week and we’ve had to address a few problems that the initial version of the plugin didn’t address. One of our developers, Andy Delcambre, just posted an article to show how we had a problem with using ActiveRecord observers and masochism, which we’re sending over a patch for now.

As we continue to monitor how this solution works, we’ll report any findings on our blog. In the meantime, I’d be interested in knowing what you’re using to solve this problem. :-)

1 Contiki, a cool travel company we’re working with

2 Rick just moved to Portland… welcome to stump town!

3 The Masochism plugin README

Starting MySQL after upgrading to OS X Leopard 18

Posted by Robby Russell Sat, 27 Oct 2007 11:13:00 GMT

If you upgraded to OS X Leopard and are running MySQL from the MySQL.com installer1, you might be having some problems with starting it from the GUI interface. There isn’t a fix from MySQL yet, so to get around that… you can run it from the command-line.

Start MySQL from the command line

cd /usr/local/mysql; ./bin/mysqld_safe &

This should get MySQL up and running for you. If someone wants to share a tip on how to get this to start automatically on reboot, please post a comment and I’ll help get the word out.

1 I didn’t have this problem as I installed MySQL via MacPorts... but this came up for a few members of PLANET ARGON after they upgraded to Leopard.

One of those mornings 1

Posted by Robby Russell Tue, 17 Apr 2007 16:09:00 GMT

Well, I made it about eight months since I got my new hard drive... and today… I had to run…

sudo port -v install mysql5 +server

Why? I have to migrate some data from MySQL to PostgreSQL.

Just the beginning... 3

Posted by Robby Russell Mon, 13 Feb 2006 22:21:00 GMT

This week… PLANET ARGON will be making a series of announcements. The last time that we did this we found ourselves with a few new employees. What will be happening this week?

...the rumors in #caboose... are likely to be true.

In other news… Jeremy had to install MySQL on his PowerBook last week...

...the things you do for clients. ;-)

Have a great week!

(more) Migrating from MySQL to PostgreSQL

Posted by Robby Russell Fri, 02 Dec 2005 02:59:00 GMT

Christopher Kings-Lynne is getting ready to release 1.0 of his cool project on PgFoundry called, MySQL Compatibility Functions.

What does this provide?

“A reimplemenation of as many MySQL functions as possible in PostgreSQL, as an aid to porting.”

Basically… a bunch of pg/sql functions… to account for all those mysql functions that you all love so much. :-)

For those who feel like they are stuck with MySQL… check this out.

...it’s not too late!

Next Week… I’ll discuss my progress with PostGIS and Ruby on Rails.

Interviewed by CRN regarding Oracle, MySQL, and PostgreSQL

Posted by Robby Russell Sun, 13 Nov 2005 03:49:00 GMT

I was interviewed by CRN regarding my personal thoughts on how the release of Oracle Express might compete with MySQL and PostgreSQL.

Read the short article…

Oracle Express… {yawn}

Older posts: 1 2