Magento 2 Full Page Cache Application: Varnish vs Built-in cache
Magento 2 (M2) comes with Full Page Cache (FPC) functionality. That feature greatly improves page load speed and overall makes frontend pages faster. There are two options for M2 FPC – Built-in cache and Varnish cache. Which one to choose? Which one is better for performance? In this article we are going to find out.
What does Magento Team Recommend?
We will use an average 2G VPS on which we will install Debian 8. On the server we will also install Magento 2 and Varnish 4. Installation instructions for Varnish might vary from OS to OS. For Debian it is a simple package installation procedure.
For load testing we will use siege. Siege is a HTTP stress testing and benchmarking utility. You can download and compile it yourself or use precompiled binary for your operating system. We will use the Debian siege package.
We will use file mode with siege. We will create a file site.txt with 5 URLs:
Then we will run siege command:
siege -c5 -d2 -t1M -f site.txt
The command means the benchmark test will run 1 minute with 5 concurrent users. A delay between requests will be 2 seconds. Those 5 users will request 5 URLs that are in site.txt file.
Built-in Cache Results
Varnish Cache Results
A browser will send a request to Varnish. Varnish will query Magento if it doesn’t find content in cache. Varnish also gets dynamic blocks ( like cart items ) directly from Magento.
Common mistake when people setup Magento 2 and Varnish is that they didn’t put Varnish in front of Magento. In other words they didn’t make Varnish listen on port 80 and accept incoming requests.
Instead they simply set Varnish as Caching Application in Magento backend and hope for the best. Then they get poor time to first byte (TTFB) results and wonder why.
Alright back to the test. I run siege command 5 times to prevent any mistakes. Results you will see in the table below:
The average server response time (or time to first byte TTFB) is 1.47 seconds.
Varnish vs Built-in
Varnish is definitely a way to go about Magento 2 full page cache in production mode. It is a lot faster than the default built-in option.
Make sure you setup Varnish right. Make it listen on port 80 and put it in front of Magento 2. If you are using SSL there are ways to configure Varnish to work with HTTPS but it is out of scope of this article.
Konstantin Gerasimov is a Magento Certified Developer. He specializes in backend development, speed optimization and custom extensions.