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.
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 workers come into play. Read more below about what PHP workers are and how they are used to speed up the processing of requests on your site.
What Is a PHP Worker?
In the context of WordPress, PHP workers build pages, process scheduled background tasks, and more. Since PHP workers 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 workers 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 workers as one of the variables for our different plan tiers. For example, Business 1 plans have 4 PHP workers per site, while Enterprise 4 plans have 16.
Although we implement server-level caching, for requests where the cache is bypassed or missed, PHP workers become very important as they have to do work for each request.
Typically we see a lot of uncached requests on ecommerce and community forum sites. Therefore, these sites will require additional PHP workers 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 workers and 4 requests, all four requests would be handled very quickly.
Simply put, a PHP worker is a background process on a server that runs PHP code.
How Does WordPress Use PHP Workers?
Before we get into how to optimize PHP worker usage for WordPress, we have to first understand how WordPress uses PHP workers in the first place.
A typical request in an uncached environment goes something like this:
- The web server (Nginx or Apache) receives a request from a visitor.
- Nginx passes the request to PHP.
- PHP queries the MySQL database as needed and uses your themeâ€™s PHP templates to generate an HTML page.
- PHP hands a rendered HTML page back to the web server.
- 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 workers for longer periods of time.
The Relationship Between PHP Workers and CPU
When it comes to WordPress performance, the relationship between PHP workers 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 workers 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 workers.
With the above in mind, you should be wary if your host is constantly advising you to increase PHP workers without mentioning CPU as well.
Here at Kinsta, our custom LXD containers are configured to autoscale CPU and RAM. We also use compute-optimized C2 virtual machines equipped with Google Cloudâ€™s fastest CPUs to help your siteâ€™s PHP workers run more efficiently. Our autoscaling feature ensures that your WordPress siteâ€™s PHP workers 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 workers as an option to improve performance.
How to Optimize Your Siteâ€™s PHP Worker Usage
Weâ€™ve explained that PHP workers are background processes that generate HTML pages with PHP code. Now, the most obvious way to reduce and optimize PHP worker 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 worker 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.
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 workers 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.
- Server-level page caching with a web server like Nginx.
- 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.
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 workers.
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 workers 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 workers 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 worker 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
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 workerâ€™s efficiency is directly correlated with CPU and RAM, hosting your site on a modern server with the latest hardware can help you optimize PHP worker usage.
Here are two examples showing why choosing a performance-focused host is important for your WordPress sites.
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 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.
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 workers 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.
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 workers.
To summarize PHP worker optimization, keep the following tips in mind.
- CPU and RAM should be scaled up alongside PHP workers. If CPU usage is locked at 100%, adding more PHP workers will not improve performance.
- Hosting your site with a performance-focused host can solve many performance issues.
- Page caching and object caching can reduce PHP worker load significantly.
- Using quality WordPress plugins and themes can reduce the amount of unnecessary code bloat on your site.
- If necessary, work with a performance expert to identify and solve complex problems.
Results of Not Enough PHP Workers
In order to achieve fast and reliable performance for your WordPress site, itâ€™s important to make sure it has enough PHP workers. When PHP workers are already busy on a site, they start to build up a queue.
Once youâ€™ve reached your limit of PHP workers, 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 workers 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 workersâ€™ 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.
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 worker would be occupied for at least half a minute. If your site only has two PHP workers, 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 workers if CPU isnâ€™t already maxed out can improve performance.
Estimating the Number of Required PHP Workers
Each of the hosting plans at Kinsta includes a certain number of PHP workers. The included number of PHP workers 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 workers.
For larger WordPress sites with more dynamic functionality like eCommerce or discussion forums, weâ€™ve found 4 PHP workers 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 workers may be needed for fast and reliable performance. If you are unsure about how many PHP workers your site needs on Kinsta, our sales and support teams can help you find out.
PHP Worker Limit Chart
The PHP worker limit chart in MyKinsta analytics lets you see how many times the PHP engine reported reaching the maximum allocated worker number in its error log. This chart can help you gauge whether or not performance optimizations are impacting your PHP worker usage.
For example, if you switched your siteâ€™s PHP version from 5.6 to 7.4, you would likely see a drop in PHP worker 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 worker 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 workers.
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.
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 workers.
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 worker 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.
The goal of maintaining a fast WordPress site is maximizing the efficiency of the backend. When PHP workers are utilized properly by finding a balance between worker count, CPU usage, and code optimization, WordPress can be an extremely performant CMS.
If you have any questions regarding how many PHP workers you might need, or you think you might be seeing errors due to the lack of PHP workers, 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!