Error messages can be a frustrating experience for you and your site’s users. Sometimes errors are descriptive enough to tell you where the issue lies. For example, the error below tells us that the process tried to allocate too much Memory and caused a fatal error:
PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 3232498752423 bytes)
But 504 Gateway Timeout errors can sometimes be difficult to troubleshoot, because they don’t provide this kind of context. In this article we explain what 504 errors are, why they happen, and ways to prevent them.
What Does Gateway Timeout Mean?
To understand the cause of a 504 Gateway Timeout error on your site, it’s first important to understand what gateway is timing out. On WP Engine environments, the gateway is specifically referring to a queueing system where requests wait to be processed by PHP.
On your server there are a specific number of “PHP workers” or threads that can be active at once. Each uncached request on your server has to be fulfilled by one of these PHP workers. When all PHP workers are busy, a backlog queue of requests builds up. The backlog feeds the longest-waiting queued request to the next available PHP worker. Having this backlog means additional uncached page requests can come in and still be served, even when all workers are busy already.
But what happens when both the PHP workers are busy and the backlog queue is filled? When a new request comes in, older requests are then “evicted” from that queue to keep requests streamlined and allow for new traffic to continue being served.
When requests are evicted from the backlog queue, they receive a 504 Gateway Timeout error.
Now that we know what causes evictions, it’s easier to identify potential solutions. Specifically, if your site is receiving too much uncached traffic, caching more of this traffic is a good first step.
In some cases, specific IP addresses or user-agents might be spamming your site with uncached requests. You can take a look at your Access Logs in the User Portal to identify any patterns. Look at the Apache tab to see only uncached requests, or the Nginx tab to see all requests (including static resources).
504 and 502 Errors
502 Bad Gateway errors may look deceptively similar to 504 Gateway Timeout errors, but the reasons behind them are different. 502 errors happen when a request takes longer than 60 seconds to complete and is automatically rejected. You can learn more about this in our Troubleshooting 502 Errors article.
You may run into situations where you are seeing both 502 errors and 504 errors happen simultaneously. This can happen when too many long-running requests happen at once. Long-running processes can stay lodged in your available PHP Workers queue, and prevent many backlogged requests from being handled. Eventually 60 seconds passes and those long requests are rejected. But until then, that PHP worker is not able to take another request from the queue.
In these situations it’s best to address the long-running processes first. Doing this will help resolve both your 502 and your 504 errors.
If you know your site will be receiving a large influx of uncached traffic and you aren’t able to increase the number of pages served from cache, it may be time to contact your Account Manager to make sure your site is prepared to handle this extra traffic. If you are unsure of who your Account Manager is, simply reach out to Support in User Portal via 24/7 chat and we will help you get in touch.