Varnish vs Nginx FastCGI Cache: Which is Best for WordPress?

When it comes to optimizing WordPress performance, caching is a crucial technique. Among the various caching solutions available, Varnish and Nginx FastCGI Cache are two of the most popular options. Both have their strengths and are suitable for different use cases. In this article, we'll delve into the details of Varnish and Nginx FastCGI Cache, comparing their features, performance, ease of use, and suitability for WordPress to help you make an informed decision.

What is Varnish?

Varnish is a high-performance HTTP accelerator designed for content-heavy dynamic websites. It acts as a reverse proxy, caching static and dynamic content to reduce the load on your web server and improve page load times. Varnish sits between your users and the web server, serving cached content whenever possible.

Key Features of Varnish:

  1. High Performance: Varnish is known for its speed, capable of handling thousands of requests per second.

  2. VCL (Varnish Configuration Language): VCL allows for advanced customization and fine-grained control over caching rules.

  3. Edge Side Includes (ESI): ESI support enables caching of partial page content, making it suitable for complex pages with both static and dynamic elements.

  4. Backend Health Checks: Varnish can automatically detect and handle backend server failures.

What is Nginx FastCGI Cache?

Nginx FastCGI Cache is a caching module built into the Nginx web server. It stores cached content on disk, reducing the need to generate content dynamically for every request. FastCGI Cache is particularly useful for dynamic content generated by PHP applications, such as WordPress.

Key Features of Nginx FastCGI Cache:

  1. Integrated with Nginx: FastCGI Cache is built into Nginx, eliminating the need for additional software.

  2. Ease of Configuration: Setting up FastCGI Cache is straightforward and involves minimal configuration.

  3. Low Resource Usage: FastCGI Cache has a low memory footprint and efficient disk I/O operations.

  4. Cache Purging: Nginx allows for easy cache purging, ensuring that outdated content is quickly refreshed.

Performance Comparison

Both Varnish and Nginx FastCGI Cache offer significant performance improvements for WordPress sites. However, their performance characteristics differ:

Varnish:

  • Speed: Varnish is extremely fast, often outperforming other caching solutions in raw speed.

  • Memory Usage: Varnish keeps most of its cache in memory, which can lead to high memory usage on busy sites.

  • Scalability: Varnish can handle a large number of simultaneous connections, making it suitable for high-traffic sites.

Nginx FastCGI Cache:

  • Speed: While slightly slower than Varnish, Nginx FastCGI Cache still offers impressive performance.

  • Memory Usage: FastCGI Cache primarily uses disk storage, resulting in lower memory usage compared to Varnish.

  • Integration: FastCGI Cache is integrated into Nginx, reducing the overhead of managing separate software.

Ease of Use

Varnish:

  • Setup Complexity: Varnish requires a separate installation and configuration. VCL, while powerful, has a steep learning curve.

  • Maintenance: Managing Varnish involves additional maintenance, including configuring backend health checks and cache purging mechanisms.

Nginx FastCGI Cache:

  • Setup Simplicity: FastCGI Cache is easier to set up, especially if you are already using Nginx as your web server.

  • Configuration: Basic configuration is simple, but advanced caching rules might require more detailed Nginx knowledge.

  • Maintenance: As part of Nginx, FastCGI Cache benefits from a single point of configuration and maintenance.

The Server Stack

All benchmarks will utilize the same server stack, which consists of the following software:

Ubuntu 22.04 LTS

PHP 7.4.14

Nginx 1.18.0

MariaDB 8.0.37

Varnish 6.2.1

WordPress 5.6, Twenty Twenty-One

Varnish will be completely disabled when not needed for the current set of benchmarks. Nginx will be used to terminate HTTPS requests, because Varnish is unable to do so. This will result in the following configuration:

Nginx:443 > Varnish:80 > Nginx:8080

Notice we have Nginx acting as a proxy server to terminate HTTPS. There are other proxies available to perform this function, but I’ve opted for Nginx to keep the number of moving parts to a minimum.

We’ll be testing four different scenarios:

WordPress – No caching

Simple Cache – Caching via a WordPress plugin

FastCGI Cache – Nginx

Varnish – Varnish using Nginx for HTTPS termination

Requests Per Second

As expected, WordPress without caching doesn’t perform well which is due to the bottleneck created by the database server. Simply taking the database server out of the equation by enabling a page cache gives nearly a 10x increase in requests. Varnish improves things further by ensuring requests don’t have to be processed by PHP. However, Varnish is way behind Nginx when it comes to raw throughput (requests per second), likely due to the additional step of Nginx HTTPS termination. Using Nginx alone achieves optimal throughput.

Average Response Time

A high number of requests per second doesn’t mean much if those requests are slow to complete, which is why it’s important to also measure response time. The average response time is the total time it takes for a request to complete.

When a server is under heavy load, the average response time will usually increase. This happens because the server is only able to handle a certain number of concurrent connections usually due to CPU or memory bottlenecks. When this number is exceeded, all additional connections will be queued and processed as resources become available. If those requests are queued for too long, they will time out.

Generally, the fewer moving parts you have in a request lifecycle, the lower the average response time will be. That’s why Nginx FastCGI caching performs so well, because it only has to serve a static file from disk (which will likely be cached in memory due to the Linux Page Cache). A request to a WordPress site with no caching will hit Nginx, PHP and the database server on the backend. With a caching plugin, you only hit Nginx and PHP. With page caching via Nginx FastCGI Cache or Varnish, you only hit Nginx or Varnish.

Suitability for WordPress

Varnish:

  • Complex Sites: Varnish is ideal for complex WordPress sites with high traffic and a mix of static and dynamic content.

  • Advanced Caching: If your site requires advanced caching techniques like ESI, Varnish is the better choice.

  • Additional Layer: Adding Varnish introduces an additional layer between the user and the web server, which can add complexity to your setup.

Nginx FastCGI Cache:

  • Simpler Sites: FastCGI Cache is well-suited for simpler WordPress sites where ease of setup and maintenance is a priority.

  • Integrated Solution: For sites already using Nginx, FastCGI Cache offers a seamless and efficient caching solution.

  • Resource Constraints: If server resources are limited, FastCGI Cache's lower memory usage makes it an attractive option.

Conclusion

Choosing between Varnish and Nginx FastCGI Cache depends on your specific needs and the complexity of your WordPress site. Varnish excels in high-performance scenarios with advanced caching requirements, while Nginx FastCGI Cache offers simplicity, ease of use, and integration benefits for many WordPress setups.

For most users, Nginx FastCGI Cache provides an excellent balance of performance and simplicity, especially if you are already using Nginx as your web server. However, if your site demands the highest performance and you are comfortable with the added complexity, Varnish can be a powerful tool in your caching arsenal.

Last updated