"HyperDB" WordPress plugin for database redundancy and load balancing

My name is Ito and I am an infrastructure engineer

By using cloud services such as AWS, it is now possible to easily replicate servers and provide redundancy and load balancing for site access via a load balancer

In this state, the web servers are redundant and load balanced, but the databases are not

If there is a high volume of access to the database,
the load on the web server may be manageable, but the processing on the DB server side may become congested, potentially preventing the site from displaying.

To get to the point, WordPress is a content platform that runs in a so-called LAMP environment.
This means it uses a database, so even with redundant web servers, if the database becomes congested...

503 Service Temporarily Unavailable

I get an error that I don't want to see

In such cases, let's make the database redundant as well!
Therefore, I'd like to introduce "HyperDB," a plugin that enables database redundancy for WordPress.

In the case of AWS, RDS redundancy is easily achieved.
All you need to do is create a "read replica" of the master RDS instance.
So I will omit the explanation.

Install HyperDB

Installing the HyperDB plugin is a bit complicated.
While it can be installed from the WordPress admin panel,
the actual configurationneeds to be written directly into a PHP file.

After installation, you should find the following file in WordPress's wp-content/plugins/hyperdb:
"db-config.php"
If you can connect to the server via SSH, do so via SSH. If you can only connect via FTP, you will need to configure the settings beforehand and
upload it to the target directory.

First, write the following in wp-config.php: "The database configuration file is db-config.php."

# vim /pass/to/docroot/wp-config.php define('DB_CONFIG_FILE', ABSPATH . 'db-config.php');

Let's continue with the settings

Configuring HyperDB

Make the following settings for the installed db-config.php

First, configure the master server. (Around line 217, confirmed in v1.1)

# vim /pass/to/docroot/db-config.php $wpdb->add_database(array( 'host' => 'Enter RDS (master server) endpoint', 'user' => DB_USER, 'password' => DB_PASSWORD, 'name' => DB_NAME, 'write' => 1, 'read' => 2, ));

The host is where you enter the RDS endpoint, but if you are using a port other than the default 3306,
<エンドポイント>:<ポート番号>とコロンに続けてポート番号を入力する必要があります。

The key points are write and read

  • write: DB write priority
  • read: Priority of reading to DB

Setting this to "0" disables both writing and reading.
In the above case, writing to the master server will have a higher priority, while reading will have a lower priority.

Next, we'll configure the slave server.
Please add this below the master server configuration we just discussed.

# vim /pass/to/docroot/db-config.php $wpdb->add_database(array( 'host' => 'Enter RDS (slave server) endpoint', 'user' => DB_USER, 'password' => DB_PASSWORD, 'name' => DB_NAME, 'write' => 0, 'read' => 1, ));

Here too, write and read are effective

  • write: 0 = Do not write to the server
  • read: 1 = high priority for read, write is 0 so it is read only

With this setting, only read operations will be performed on the slave server.
Similarly, if you register multiple slave servers, read operations will be performed only on the slave servers.
This distributes the load of read operations (i.e., normal access) to the database across the slave servers.

In WordPress, only actions performed through the administration panel, such as changing settings or adding posts, are written to the database.
This is not a problem because the master server has a high write priority.

So, now WordPress database is redundant and load balanced!

Now you can rest assured even with sudden access!
HyperDB — WordPress Plugins

If you found this article helpful,please give it a "Like"!
0
Loading...
0 votes, average: 0.00 / 10
5,768
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author