When it comes time to choosing a hosting plan, it’s important to pick one that will best match the demands of your WordPress site.

For example, an ecommerce site that gets 50,000 visitors per month is typically going to be a lot more demanding on resources than a simple blog with the same amount of traffic.

This is simply due to the fact that ecommerce sites are typically dynamic in nature, and require more resources for PHP and database queries.

That’s where PHP threads come into play. Read more below about what PHP threads are and how they are used to speed up the processing of requests on your site.

What is PHP threads?

In the context of WordPress, PHP threads build pages, process scheduled background tasks, and more. Since PHP threads are directly responsible for generating HTML pages to serve to your site’s visitors, they determine how many simultaneous uncached requests your site can handle at any given time.

For example, let’s say your WordPress site is equipped with two PHP threads and no page caching setup. If four requests come to your site at the exact same time, two of those requests will get processed immediately, while the other two will have to wait in the queue until the first two have finished processing.

Here at Kinsta, we use PHP threads as one of the variables for our different plan tiers. For example, WP 5 plans have 4 PHP threads per site, while Enterprise plans start from 8 PHP threads per site.

Although we implement server-level caching, for requests where the cache is bypassed or missed, PHP threads become very important as they have to do work for each request.

Typically we see a lot of uncached requests on e-commerce and community forum sites. Therefore, these sites will require additional PHP threads to ensure every request is processed without delays or timeouts.

If your site is highly optimized or does not have a lot of PHP code (e.g. a complex theme or a lot of WordPress plugins), then processing each request should happen almost instantly. Even with 2 PHP threads and 4 requests, all four requests would be handled very quickly.

Simply put, a PHP thread is a background process on a server that runs PHP code.

How does WordPress use PHP threads?

Before we get into how to optimize PHP threads usage for WordPress, we have to first understand how WordPress uses PHP threads in the first place.

A typical request in an uncached environment goes something like this:

  1. The web server (Nginx or Apache) receives a request from a visitor.
  2. Nginx passes the request to PHP.
  3. PHP queries the MySQL database as needed and uses your theme’s PHP templates to generate an HTML page.
  4. PHP hands a rendered HTML page back to the web server.
  5. The page is served to the visitor.

In the process highlighted above, Step 3 is the most time and resource-intensive (CPU and RAM). A highly optimized site with minimal database queries and efficient PHP code will get through the third step relatively quickly.

On the contrary, a site with poorly-written PHP code that makes a lot of unnecessary database queries will spend a lot more time getting through Step 3, which means requests will occupy PHP threads for longer periods of time.

What’s the relationship between PHP threads and CPU

When it comes to WordPress performance, the relationship between PHP threads and available CPU is an important one to consider.

If the lack of CPU resources is your site’s bottleneck, increasing the number of PHP threads will not increase your site’s performance – it will only allow your site to process more requests at the same time with slower per-request performance.

Let me explain.

Imagine a fire hydrant with a single hose attached to it. With only one hose connected, the hydrant is able to provide adequate water pressure. Now, what happens if we attach ten hoses to the hydrant?

The limited water pressure is spread over ten hoses, which means each individual hose has less water pressure to get the job done. In this analogy, the fire hydrant is the CPU, and the hoses are PHP threads.

With the above in mind, you should be wary if your host is constantly advising you to increase PHP threads without mentioning CPU as well.

Here at Kinsta, our custom LXD containers are configured to with ample CPU and RAM resources. We also use C2 and C3D virtual machines equipped with Google Cloud’s fastest CPUs to help your site’s PHP threads run more efficiently. Our scalable infrastructure ensures that your WordPress site’s PHP threads have enough CPU resources to operate at peak performance.

Let’s go back to the fire hydrant analogy for just a moment.

Imagine you are in a situation where you need to put out ten fires with five hoses. After connecting all five hoses, you realize that the hydrant is still providing adequate water pressure.

In this situation, it would make sense to connect a few more hoses because the hydrant’s water pressure is not the bottleneck.

Similarly, if your site is performing poorly even with adequate CPU and RAM overhead, that’s when you should look into increasing the number of PHP threads as an option to improve performance.

How to optimize your site’s PHP threads usage

We’ve explained that PHP threads are background processes that generate HTML pages with PHP code. Now, the most obvious way to reduce and optimize PHP threads usage is to reduce the amount of CPU and PHP resources required to fulfill requests to your site.

Here’s how to do it.

1. Set up caching for your WordPress site

The first step to reducing PHP threads usage is setting up caching layers for your WordPress site. By default, WordPress is a dynamic CMS that fulfills every page request on demand.

For many sites like blogs, online magazines, and portfolios, using PHP to dynamically generate pages for every request is unnecessary.

Page caching

The blog post you’re currently reading is the perfect example of a page that doesn’t need to be dynamically generated. Like many of our other posts, the content in this post is designed to be static, so there is no need to spend CPU resources to generate identical pages continuously.

Instead, it’s better to have PHP generate the page once and then cache it. Page caching has many obvious advantages over dynamically generating pages with PHP.

For example, imagine if a blog post on your site goes viral and receives 100,000 page views within a few hours of publishing. Without page caching, your PHP threads would likely get overwhelmed and your server would likely crash.

With page caching, only the first-page view would be generated dynamically. The other 99,999 requests would be served from your page cache, which uses relatively little CPU resources.

There are two ways to set up page caching for your WordPress site.

  1. Server-level page caching with a web server like Nginx.
  2. Plugin-based page caching with a WordPress plugin like WP-Rocket.

For maximum performance, we recommend using server-level page caching whenever possible. On Kinsta, all of our sites use Nginx’s FastCGI cache module for super-fast performance.

If your host doesn’t offer server-level page caching option, the next best option is to use a WordPress caching plugin to implement page caching at the application level.

Object caching

For WooCommerce stores, community forums, and other WordPress sites that can’t make use of page caching efficiently, adding a persistent object cache like Redis in front of your MySQL database can boost performance and reduce load on PHP threads.

Without a persistent object cache, MySQL database queries will be executed for each request even if the result is identical to a previous query.

For example, a community forum site that bypasses page cache will make separate identical queries to the database to grab post data in order to build a page.

For high-traffic and database-heavy sites, this method of querying the database is inefficient because it uses PHP threads to generate identical query results for separate requests. That’s where Redis comes in.

Redis stores the results of database queries in RAM, which allows PHP to grab the results of queries that have already been executed. This method of object caching allows PHP threads to conserve CPU resources and spend less time fulfilling a request because it removes the need for repetitive database queries.

2. Optimize your PHP code

In addition to setting up page caching, another strategy that will help you reduce PHP threads usage is optimizing your PHP code. In the context of WordPress, “optimizing PHP code” can mean a variety of different things, so let’s take a deeper look.

One of WordPress’ most beloved and most hated features (depending on who you ask) is its extensibility via plugins and code snippets.

If you want to add a stock ticker widget to your WordPress site, there’s a plugin for that. Similarly, if you want to add custom fonts, there’s a functions.php code snippet for that as well.

Extending WordPress core with additional features has become so easy that oftentimes we go overboard without thinking about the potential impact on site performance.

Therefore, the first way to optimize your PHP code is to perform a site-wide audit to determine what plugins and code snippets are truly necessary.

Choose quality plugins

More often than not, the number of plugins on your WordPress site isn’t as important as the quality of the plugins. If a plugin hasn’t been updated within the past six months, we’d recommend choosing another one that fits the bill.

The reason for this is WordPress is constantly being improved. If a plugin hasn’t been updated in years, chances are its code isn’t utilizing the latest WordPress development and security best practices.

Conversely, if a plugin is constantly updated every few weeks, there’s a good chance that the developer is serious about quality, which makes it a good choice for your WordPress site.

Only use plugins when needed

If you’re looking to perform a simple task on your site like adding JavaScript or CSS, you don’t always need a plugin for that. Instead, you can add code directly to your theme’s PHP templates or style.css file with a child theme.

Next time you’re in a situation where you’re thinking about installing a plugin, spend some time to look into whether it’s 100% necessary first. Sometimes, there’s no way around installing another plugin and that’s okay. Other times, you may be able to avoid adding additional code bloat by not installing unnecessary plugins.

Choose lightweight themes

From our experience monitoring thousands of WordPress sites, we’ve found that themes are occasionally the cause for poor PHP performance. To cater to WordPress’ versatility as a general purpose CMS, some developers code themes to work for a variety of use cases.

Oftentimes, this results in code-heavy and bloated themes that don’t make use of PHP and database queries in an efficient manner.

When building a WordPress site, it’s important to choose a theme that is most performant and customizable – GeneratePress, OceanWP, and Astra are three examples.

3. Choose a performance-focused WordPress host

Believe it or not, choosing the right WordPress host can have a huge impact on the performance of your site. Since a PHP threads’ efficiency is directly correlated with CPU and RAM, hosting your site on a modern server with the latest hardware can help you optimize PHP threads usage.

Here are two examples showing why choosing a performance-focused host is important for your WordPress sites.

High-performance CPUs

PHP uses CPU resources to execute code. A faster CPU means faster code execution. At Kinsta, we use Google Cloud’s fastest servers – compute-optimized C2 and compute engine general-purpose C3D VMs.

These VMs are equipped with the latest Intel Xeon processors capable of operating at 3.8 GHz all-core turbo. In our benchmark tests, we saw C2 machines outperforming traditional N1 machines by 2-4x, while C3D machines saw response times improve by a further 20% to 50%.

Fast SSD storage

Disk I/O speed can have a direct impact on code execution and database queries. If your database is stored on a slow mechanical disk or a cloud-based SSD without sufficient IOPS (input/output operations per second), your PHP threads will be forced to spend more time fulfilling a request.

We use Google Cloud Platform’s high-performance SSD storage to ensure your WordPress site has access to fast disk I/O.

4. Work with a performance expert (optional)

If you are unsure about how to tackle a performance issue on your site, we recommend working with a qualified performance expert to diagnose the problem.

An expert can help you identify specific bottlenecks in your code by using advanced monitoring tools like New Relic or the Query Monitor WordPress plugin.

By zooming in and inspecting individual PHP processes and database queries, it’s possible to identify specific blocks of code and their associated features that are putting high load on your site’s PHP threads.

To summarize PHP threads optimization, keep the following tips in mind.

  1. CPU and RAM should be scaled up alongside PHP threads. If CPU usage is locked at 100%, adding more PHP threads will not improve performance.
  2. Hosting your site with a performance-focused host can solve many performance issues.
  3. Page caching and object caching can reduce PHP threads load significantly.
  4. Using quality WordPress plugins and themes can reduce the amount of unnecessary code bloat on your site.
  5. If necessary, work with a performance expert to identify and solve complex problems.

Results of not enough PHP threads

In order to achieve fast and reliable performance for your WordPress site, it’s important to make sure it has enough PHP threads. When PHP threads are already busy on a site, they start to build up a queue.

Once you’ve reached your limit of PHP threads, the queue starts to push out older requests which could result in 504 errors or incomplete requests.

Another common error we see due to the lack of PHP threads is 502 bad gateway errors. These are slightly different from 504 errors because the error occurs after a timeout of 60 seconds in the PHP threads’ queue.

Not only do these errors present a bad user experience for your visitors, but they can also have a negative impact on your site’s SEO.

A 502 (Bad Gateway) error.
A 502 (Bad Gateway) error.

There are a number of different factors that can cause slow page loads or errors. For example, if an uncached request requires a lot of data from the database, the resulting query could take 20-30 seconds to complete.

In this situation, a PHP thread would be occupied for at least half a minute. If your site only has two PHP threads, just two or three of these long requests can be enough to start causing errors.

To resolve this, optimizing the MySQL database and increasing PHP threads if CPU isn’t already maxed out can improve performance.

Estimating the number of required PHP threads

Each of the hosting plans at Kinsta includes a certain number of PHP threads. The included number of PHP threads is based on historical resource usage metrics we have collected over the past few years. In general, sites with primarily static content – articles, static pages, and portfolios – don’t require many PHP threads.

For larger WordPress sites with more dynamic functionality like e-commerce or discussion forums, we’ve found 4 PHP threads to be a good starting point. However, this can vary per site as each one will have its own unique set of themes, plugins, database queries, and cached-to-uncached ratio.

In some cases, more PHP threads may be needed for fast and reliable performance. If you are unsure about how many PHP threads your site needs on Kinsta, our sales and support teams can help you find out.

PHP threads limit chart

The PHP threads limit chart in MyKinsta analytics lets you see how many times the PHP engine reported reaching the maximum allocated thread number in its error log. This chart can help you gauge whether or not performance optimizations are impacting your PHP thread usage.

Top cache bypasses
Top cache bypasses.

For example, if you switched your site’s PHP version from 5.6 to 7.4, you would likely see a drop in PHP threads limits because PHP 7.4 is much faster than 5.6.

Similarly, if you worked with a performance expert to fix long database queries and switch to a more lightweight theme, you can use the PHP threads limit chart to see the differences before and after the optimizations.

Cache analysis chart

You can also use the cache analysis report in MyKinsta to determine the number of cache hits, bypasses, misses, and expirations. This data can be especially useful when optimizing your site’s usage of PHP threads.

Cache bypass with query strings

By default, URLs with query strings like https://kinstalife.com/?query=123 bypass the page cache. In some cases, query strings can result in a large spike of unnecessary PHP and CPU usage.

For example, if you visit a link from Facebook, you’ll often see the ?fbclid= query string at the end of the URL. Similarly, you may see UTM tracking parameters after clicking on a link in an email newsletter.

A URL with a query string (?querystring=123).
A URL with a query string (?querystring=123).

If a post on your site goes viral, and is being constantly accessed with a query string, you’ll be able to identify the specific URL with the cache analysis report.

With that key piece of information, you can then contact our support team to force cache that specific URL to reduce load on your PHP threads.

Identifying resource-heavy plugins

In some cases, the cache analysis chart can also be used to identify resource-heavy plugins and processes.

For example, if you see that the top cache bypass URL points to a file inside a specific plugin’s directory, there’s a good chance that plugin is responsible for high PHP thread usage.

If you see a lot of plugin-related requests in your cache bypass list, you can work with a developer to address the issue or switch to a plugin that uses fewer resources.

Summary

The goal of maintaining a fast WordPress site is maximizing the efficiency of the backend. When PHP threads are utilized properly by finding a balance between thread count, CPU usage, and code optimization, WordPress can be an extremely performant CMS.

If you have any questions regarding how many PHP threads you might need, or you think you might be seeing errors due to the lack of PHP threads, please open a ticket with our support team for assistance.

Now your turn: What optimization strategies do you use to keep your WordPress site running smoothly? Let us know in the comments!

Brian Li

Brian has been a WordPress user for over 10 years, and enjoys sharing his knowledge with the community. In his free time, Brian enjoys playing the piano and exploring Tokyo with his camera. Connect with Brian on his website at brianli.com.