Proxy cache as stale on Nginx-Proxy in EasyEngine v4

EE Site with Redis cache 🚤

In an EE site with Redis cache enabled, site Nginx checks if the cache is available in Redis then serves the page from Redis cache, otherwise sends the request to PHP.

After that PHP will process the request and then the page will be stored in Redis.

What happens with high traffic sites? 📈

PHP has a fixed number of child processes that can be triggered at a time. If that number of PHP processes is filled out then the PHP won’t be accepting any more requests.

This mechanism works fine in general, but on sites with high traffic, the scenario changes. 🔁

When the cache purges on high traffic sites then all of the sudden multiple requests for the same page may go to PHP and can potentially fill up all the PHP children processes with the same request.

Proxy cache as stale 🚀

The expected solution would be something like if the page cache is expired then serve the page from expired cache and send only one request to the PHP and then update the cache.

Currently, OpenResty srcache-nginx-module does not have that feature. We tried to implement it in other ways like https://github.com/openresty/srcache-nginx-module/issues/38. But that did not work.

Another Nginx module ngx http proxy module does provide that feature with proxy cache use stale directive. This module was present in the EasyEngine Nginx-Proxy docker image.

We leveraged the power of ngx http proxy module module to enable a small cache on Nginx-Proxy container. 

This cache by default will be of 1 second and then expires but does not get deleted. When the cache gets expired Nginx-Proxy container will keep serving the stale cache while only one request will be passed to site Nginx.

                 +---------------+                +--------------+
 Request         |               |                |              |
 --------------->+  NGINX PROXY  +--------------->+  SITE NGINX  |
                 |               |                |              |
                 +-------+-------+                +-------+------+
                         |                                |
                         |                                |
                         |                                |
                         |                                |
                         v                                v
                    PROXY CACHE                      REDIS CACHE

Check if cache available.

  1. If Available => serve from cache.
  2. If Expired => serve from stale cache and pass request to site Nginx.
  3. If Not Available => send request to site Nginx.

How to enable 🏁

Use this doc to enable the Proxy-cache on site.

Debugging and testing 🛠

Once the Proxy-cache is enabled for a site you can see the following header in the cURL output or in the network tab:

X-Proxy-Cache: <VALUE>

You can check this Nginx FAQ for values of X-Proxy-Cache header: https://www.nginx.com/blog/nginx-caching-guide/#Frequently-Asked-Questions-(FAQ)