Loudon & Company

If you like this, sign up for my mailing list (1~2 a month)

Better Drupal MySQL Slave Usage via Autoslave

Sunday, March 10th, 2013

This short article is essential reading for sites using MySQL replication. It covers the background problem, potential solutions, and risks of higher slave utilization

    The major takeaways in these threads are:
  1. slave usage is barely perceptible
  2. slaves are only used when contrib and core have explicitly set an option in the db_query function
  3. there is a partial solution that can force a minority of queries to use a slave
  4. this is a design choice because slave database usage affects a minority of Drupal sites and a perfect solution is complicated and will incur an unnecessary performance overhead on every query (eg, regex on "^SELECT")

All of that is to say, if you have a highly trafficked site, MySQL replication isn't doing nearly as much as it could for you.

Fortunately, there's an amazing little module that solves everything for you with a few more lines in your settings.php. Autoslave defines a new database connection type (driver) and takes the stance that a regex and read-only slave DBs may not be the perfect solution, but they are a pragmatic one*.

The project page has sample config and the maintainer is VERY responsive and helpful. As far as configuration, there are only a couple of points worth noting: 1, you need a workaround for drush and 2, you need a workaround for update.php.

As far as risks go, one of my clients' websites wouldn't run without autoslave — the authenticated traffic load was crushing the master database. It should also be noted that there is no risk of data corruption, if your slaves are read-only; you could lose a write, but this would also trigger and log an error. Besides, a bit of testing is normal due diligence and should give you plenty of peace of mind.

* Also worth noting, this approach of inspecting the query is exactly the same used by the PHP PECL mysqlnd extension — http://www.php.net/manual/en/mysqlnd-ms.architecture.php.