PHP workers process a site’s PHP code. This includes building pages, processing background tasks, querying the database, etc.
One way to think of PHP workers is like employees at a store. Each worker can only handle one request at a time. If there are more customers than there are workers, those customers (processes) have to line up and wait for the next available worker to handle their request.
PHP workers really come into play when a site doesn’t or can’t cache most of its content. The more dynamic a website is, the more PHP workers it will likely need. Cached content doesn’t require PHP workers, they’re really only needed when the site needs to query the database to get or change information.
WordPress and PHP Workers
An uncached request on a WordPress site usually goes something like this:
- A visitor visits a page or performs an action on a page (e.g. adding something to a cart, submitting a form, etc.).
- The web server (Nginx here at Kinsta) receives that request.
- Nginx passes the request to PHP.
- PHP queries the MySQL database and gets the information it needs or makes any updates needed.
- PHP then uses your theme’s PHP files (and any plugin files if applicable) to generate an HTML page.
- PHP hands the rendered HTML page back to the web server.
- The page is served to the visitor.
In the process described above, Step 4 is the most time and resource-intensive (CPU and RAM). A well-optimized site with efficient PHP code and database queries will process that step fairly quickly.
On the other hand, poorly written or unoptimized PHP code and/or a lot of inefficient database queries will take much longer to get through Step 4. Requests that take longer to process monopolize PHP workers for longer periods of time.
Estimating Number of PHP Workers Needed
How many workers a site needs depends on several factors, such as: how dynamic the site is, how optimized the site’s code is (how quickly requests can be processed), and what kind of traffic the site receives. An optimized site handles requests quickly, freeing up PHP workers for the next request in the queue.
Dynamic sites like ecommerce stores, forums, learning sites, and membership sites will usually need more PHP workers than more static, brochure-type sites. Also, the busier a site is, the more PHP workers it will usually need.
PHP Workers, CPU, and RAM
When adding PHP workers, CPU and RAM resources must be taken into account. If you increase PHP workers, but the server doesn’t have enough CPU and RAM to support those workers, this will create a bottleneck because the requests won’t be handled efficiently.
Here at Kinsta, our custom LXD containers are configured with plenty of CPU and RAM resources. Additionally, by using compute-optimized C2 virtual machines equipped with Google Cloud’s fastest CPUs, we help your site’s PHP workers run more efficiently. Our scalable infrastructure ensures that your WordPress site’s PHP workers have enough CPU resources to operate at peak performance.
Identify Performance Issues Related to PHP Workers
If too many requests stack up in the queue because of a large influx of requests, long-running processes, or a combination of both, the site can experience performance issues that may result in 502 or 504 errors.
The PHP worker limit chart in MyKinsta analytics shows you how many times the maximum number of allocated workers has been reached on your site. This can help you determine if your site is continuously using up its PHP workers and can also show you how updating to a newer PHP engine version may help.
The cache analysis section in MyKinsta analytics can be used to view your site’s total cached requests and top cache bypasses.
Reduce and Optimize PHP Worker Usage
Caching is your best friend when it comes to optimizing your site and reducing the number of PHP workers needed. Remember, PHP workers aren’t needed for cached content, so cache everything you can. At Kinsta we take care of page caching for you; all sites use Nginx’s FastCGI cache module for super-fast performance.
Adding a persistent object cache like Redis in front of your database can boost performance and reduce the need for PHP workers. Without object caching, MySQL database queries are executed for each request, even when it’s the same query and results.
Redis stores the results of database queries in RAM so PHP can grab those results without having to run the query again. Removing the need for repetitive database queries allows PHP workers to conserve resources and fulfill requests more efficiently.
Check out our premium add-ons to learn more about adding Redis cache to your site.
Make sure your site’s code is optimized so it is as efficient as possible. This applies to custom code, theme code, and plugin code. If you’re not sure, we recommend asking a developer to review your site’s code.
If your website contains any custom code snippets in plugins or your theme, make sure they’re truly necessary and well-written.
Take a good look at the plugins used on the site and make sure they’re truly needed, don’t duplicate features, and are the best option for the need they fulfill. If any plugins aren’t compatible with the latest version of WordPress and PHP, it may be time to consider other options. If you have plugins on your site that aren’t being used, removing those is recommended.
Use a lightweight and performant theme. Avoid themes that contain functionality that’s best implemented via separate plugins (e.g. SEO, search filtering, custom fields, image sliders/slideshows, etc.) or isn’t needed for your site.
Enable Kinsta CDN
Enabling the Kinsta CDN provides another boost of efficiency and optimization for your site. Kinsta CDN is our high-performance HTTP/3 CDN powered by Cloudflare, at no additional cost. With it enabled, your site will be able to serve static assets from locations across the world.
Consult With a Performance Expert
If you’re familiar with site optimization, this is an optional step. An expert can help you analyze all aspects of your sites, identify bottlenecks, and implement solutions.