Scaling WordPress with MySQL Replicas and HyperDB Plugin

Advantages of MySQL Replication

Typical WordPress sites are limited to the capacity of a single database to serve read and write requests. As a result, high traffic sites can experience latency as requests are fulfilled. MySQL replication rapidly copies content from the "master" database to one or more "replica" databases. This allows you to spread requests across multiple databases to improve site performance and load times.

About HyperDB

The HyperDB plugin replaces the standard wpdb class so that WordPress is able to write and read from additional database servers. The drop-in plugin supports database replication, failover, load balancing, and partitioning — all tools for scaling WordPress.

Keep in mind, HyperDB is a powerful tool with several tuning options based on database architecture and network topology. Before you implement a complex configuration, it’s best to see if a simpler configuration suits your needs.

Install and Configure HyperDB

Download the archive of HyperDB from the WordPress plugin repository and move the db.php file into the /wp-content directory. This is a drop-in plugin and does not require activation at any time.

Next, configure the master/replica databases within db-config.php. This file should be stored within the same directory as the site's wp-config.php file.

When the db.php database drop-in is deployed to production, WordPress will begin allocating MySQL database reads and writes based on the configuration details you’ve provided in db-config.php.

The following sample configurations can be used in place of the dp-config.php file provided within the plugin archive. These examples require no additional edits for sites running on Pantheon. For more advanced options, refer to the db-config.php file provided in the HyperDB plugin archive.

Split Reads Between Master and Replica

Split reads between the master and the replica, to simply distribute the load between two servers.

<?php
/**
 * Register the master server to HyperDB
 */
$wpdb->add_database( array(
        'host'     => DB_HOST,
        'user'     => DB_USER,
        'password' => DB_PASSWORD,
        'name'     => DB_NAME,
        'write'    => 1, // master server takes write queries
        'read'     => 1, // ... and read queries
) );
/**
 * Register replica database server if it's available in this environment
 */
if ( ! empty( $_ENV['REPLICA_DB_HOST'] ) ) {
        $wpdb->add_database(array(
                'host'     => $_ENV['REPLICA_DB_HOST'] . ':' . $_ENV['REPLICA_DB_PORT'],
                'user'     => $_ENV['REPLICA_DB_USER'],
                'password' => $_ENV['REPLICA_DB_PASSWORD'],
                'name'     => $_ENV['REPLICA_DB_NAME'],
                'write'    => 0, // replica doesn't take write queries
                'read'     => 1, // ... but it does take read queries
        ));
}
// That's it!

Pass Frontend Read Queries to Replica, WordPress Dashboard Reads and Writes to Master

Pass all frontend database read queries to the replica, leaving the master dedicated to WordPress dashboard reads and writes. This can better ensure WordPress dashboard availability during high frontend load.

<?php
/**
 * Use HyperDB to just use the replica for frontend reads.
 * Register the master server to HyperDB
 */
$wpdb->add_database( array(
        'host'     => DB_HOST,
        'user'     => DB_USER,
        'password' => DB_PASSWORD,
        'name'     => DB_NAME,
        'write'    => 1, // master server takes write queries
        'read'     => is_admin() || empty( $_ENV['REPLICA_DB_HOST'] ) ? 1 : 0, // ... but only takes read queries in the admin if the replica is available
) );
/**
 * Register replica database server if it's available in this environment
 */
if ( ! empty( $_ENV['REPLICA_DB_HOST'] ) && ! is_admin() ) {
        $wpdb->add_database(array(
                'host'     => $_ENV['REPLICA_DB_HOST'] . ':' . $_ENV['REPLICA_DB_PORT'],
                'user'     => $_ENV['REPLICA_DB_USER'],
                'password' => $_ENV['REPLICA_DB_PASSWORD'],
                'name'     => $_ENV['REPLICA_DB_NAME'],
                'write'    => 0, // replica doesn't take write queries
                'read'     => 1, // ... but it does take read queries
        ));
}

That's it!

To view or add a comment, sign in

More articles by Arif Amir

  • Create amazing websites using WordPress & React

    Frontity is a free and open source framework to build super fast WordPress themes using React. This guide will try to…

    1 Comment
  • How To Create a Network Only Plugin in WordPress

    If you’ve ever created a custom plugin in WordPress, you should be familiar with the standard plugin header code. The…

  • WP Widget Cloner

    Link: https://wordpress.org/plugins/wp-widget-cloner/ Author: Muhammad Arif Amir Description WP Widget Cloner helps you…

  • Local WordPress Development with Chassis Desktop Application

    Human Made has released its first public beta of Chassis Desktop, an application for local WordPress development…

  • Creating Users in WordPress Dashboard

    Creating users in WordPress allows you to add users with different roles and access privileges to your site. Once…

  • Excuses ‪‎online entrepreneurs‬ make to themselves

    Excuses ‪‎online entrepreneurs‬ make to themselves: I'll start when I have a real plan I'll start when I have more…

    2 Comments
  • WordPress Meetups Pakistan

    Are you a WordPress user from Lahore , Islamabad , Karachi or Peshawar ? Would you like to participate in a monthly…

  • Important Skills for Freelancers

    We often talk about how many things are needed to be successful as a freelancer, but we rarely say which exactly…

  • Beginner's Guide to WordPress Multisite

    What is WordPress Multisite? WordPress Multisite is a special “mode” built into WordPress, which allows you to create a…

    1 Comment

Others also viewed

Explore content categories