One way to improve sites that experience 5xx errors is by extending a Cloud's performance through PHP-FPM's concept of PHP workers, which ties both CPU and RAM together.
You can double the provisioned PHP workers in a Cloud for $10/m, and if you make frequent updates via the Manager, add a pool dedicated for just the Manager for an additional $5/month. Do this from the MODX Cloud Dashboard: when editing a Cloud instance in the Dashboard, go to the Add-ons tab and find the Server Resources section.
How PHP Workers Work in MODX Cloud
Extra workers help Cloud instances handle more requests when the requests take longer to process than the rate at which requests come in. Each worker is responsible for processing a queue of requests that have been passed to PHP by nginx (our web server), processing one request at a time.
The standard Cloud allocation is a pool of 4 workers. This means that PHP can process up to 4 requests simultaneously. While this may sound like a small number, most requests should only take milliseconds and a well-built site can serve hundreds of concurrent visitors with this configuration.
The Add-on that you can purchase for extra workers doubles this to 8 workers, allow your site to handle twice the number of simultaneous PHP requests.
When Additional PHP Workers Help
In some cases, this is enough of a help to make sure the site continues to perform under some kinds of stress. It's important to know that in some cases, extra workers are only a temporary solution and perhaps only briefly buffer, before the site eventually fails again with 504 or 502 responses. Usually this is accompanied by changes to a site, broken external services (see below) or an increase in visitor traffic.Typically we see problems occur for a variety of reasons, including:
- Website traffic and visitor count increases
- Less than optimal caching of Snippets
- Heavy processing, e.g., processing image thumbnails
- Long-running database queries
- Sites makes blocking calls to external services like web fonts, S3 buckets, geocoding services, Twitter streams and other similar applications
What the above scenarios result in is that requests take too long to respond, causing a PHP worker handling each request to time out, filling up the pool of available workers. In other words, each PHP worker has to wait for a response from an external resource far away or responding slowly (or not at all), before completing and continuing to the next request.
Minimizing the Need for PHP Workers
With some extra care, you frequently can eliminate the need to increase workers:
- Make sure Snippets gracefully time out quickly
- Make sure Snippets handle failure to respond exceptions for external services
- Cache Snippets and DB queries whenever possible
- Use a CDN to offload serving static assets
- Follow the practices outlined in the Performance Optimization article
- Follow the practices outline in the Optimizing for Pagespeed article
Fortunately, in MODX Cloud tips #3 and #5 from the blog article are handled. But with a little extra effort you can really see significant performance gains for most sites when following the tips there. Note: while the article mentions older versions of PHP and nginx, MODX Cloud runs the latest stable versions at all times.