Typically, WordPress opens five (5) database connections. However, the actual number of connections can vary depending on multiple factors, including the type of PHP server being used.
For example, a LAMP stack setup with PHP-FPM, without any customization, usually has a pm.max_children
value of 5, resulting in a maximum of five database connections.
Why does this matter? 🤔
Relational databases have a limit on the number of connections. This includes MySQL and MariaDB, which power most WordPress sites.
So, if you are thinking about using any
Database-as-a-Service provider, you will notice that there is number of connection limit.
If you are using Shared Hosting, the maximum number of database connections entirely depends on the hosting provider configuration.
If you are self-hosting the database, the maximum number of database connections are entirely up to you. You can define any number as long as your server RAM permits.
The more database connections that are opened, the higher the memory consumption will be.
Why doesn’t this matter? 🤨
Here the good news. 😯
The default max_connections
for MySQL and MariaDB is 150 connections which is huge enough for most cases.
However, this must be fine-tuned depending on the server RAM capacity and if you are using Shared Hosting, be sure to check with your hosting provider.
So, chances, you will never hit a maximum database connection limit issue unless you are thinking about making WordPress to be on HA (High-Availability).
Making High-Availability WordPress means, there is multiple servers serving WordPress. Multiple servers serving WordPress will utilize more connections where it will likely cause database connection limit issue.
Factors affecting database connections 📋
This is a list of factors (non-exhaustive):
- Number of servers hosting the WordPress site (typical WordPress installed only on single server. Unless it is configured to run under load-balancer).
- Usage of WordPress plugins (properly written plugin should make use the existing
$wpdb
global variable provided by WordPress where it will make use the available connection. However, if a plugin didn’t utilize the$wpdb
, then it will cause to create new database connection. This result to more than 1 database connection per request). - Current active request at specific point of time (If there is no visitor to your WordPress site, there will be no active database connection).
- Server configuration. Depending on type of PHP Servers (either PHP-FPM or mod_php), it will cause the database connection opened to be vary.
Experiment 🔬
In this section, we will go through how we can confirm if the default WordPress site installation only consume 1 database connection per request.
1/5: How to check current usage of database connection.
You may use any database tool to check the current usage of database connection per user. In this case, to simplify the screenshot, I am using the code below to tell how many connections that are currently active.
Make sure to use a user that have access to
connections
table.
2/5: How to make WordPress load long enough to see the connections
WordPress clean install load pretty fast. So, how you can tell if a request made to WordPress open how many connections to WordPress?
There are several ways to do it. However, in this case, we will put a sleep(10)
command in wp-settings.php
. This means, the WordPress will load until the end and sleep for 10 seconds before the execution ends.
3/5: Demo –> Single (1) request to WordPress site
Then, we will make a load to WordPress site and check the connection count.
4/5: Demo –> Five (5) requests to WordPress site
To simulate 5 requests to WordPress site, we can’t simply use a web browser with 5 tabs and load the same website. This because, modern web browser does have HTTP/2 Multiplexing capability which will only open single connection.
Hence, in this case, we will be using K6 load test tools to simulate 5 requests to WordPress site.
5/5: Demo –> Ten (10) requests to WordPress site
This test case will be different. Here’s why: 🤔
This test environment run using PHP-FPM where the pm.max_children
is set to 5. That’s means, regardless of the request count are being made to the WordPress site, it will only serve 5 requests at any single point of time.
Verdict
By default, WordPress will open a database connection until the execution ends. Once the execution ends, the database connection will be released and available for use.
Either we increase the pm.max_children (vertical scaling) or adding more instance (horizontal scaling), be sure to check out the database connection limit. Adjust the database connection limit accordingly to prevent Error establishing a database connection error on high load.