Magento 2 Full-Page Cache with Varnish: Faster Product Pages
Enable Magento 2’s Varnish full-page cache to serve cached HTML and reduce server load during traffic spikes.
25 Jan 2026, 01:44 UTC

The problem: page load slows as traffic grows
A Magento 2 store can render a product detail page by running controllers, layout XML, blocks, and database queries on every request. Under normal traffic that works. During a flash sale or newsletter spike, the same pages are rendered repeatedly for different visitors, consuming CPU and database connections. The result is slower time-to-first-byte and, in extreme cases, timeouts.
What Magento 2 full-page cache does
Magento 2’s native full-page cache stores the rendered HTML for a URL so subsequent requests can skip most of the PHP application stack. When Varnish is configured as the caching backend, it sits in front of the web server and returns cached HTML directly. A cache hit avoids executing controllers and layout rendering entirely. A cache miss sends the request to Magento, which renders the page and stores the result for the next visitor.
Cache keys are typically built from the request URL, store view, customer group, and other request parameters. This means a logged-in wholesale customer and a guest shopper can receive different cached versions of the same URL. “Full-page cache” (FPC) is Magento’s feature for storing complete page HTML; “Varnish” is an HTTP accelerator that stores responses in memory and serves them before they reach the application.
Enable Varnish in Magento 2 Admin
Log in to the Magento Admin as a user with system configuration privileges. Navigate to System > Configuration > General > Web > Full Page Cache (the exact path can vary by Magento version). Set the TTL for public content, choose Varnish as the caching application, and save.
Generate the Varnish configuration file from the Magento root directory. Run this as the file-system owner:
php bin/magento varnish:config:generate --export-version=6 --output-file=/etc/varnish/default.vcl
Replace /etc/varnish/default.vcl with your Varnish VCL path. Required permissions: write access to the Varnish configuration directory, usually root or sudo. Back up the existing VCL first; an invalid VCL prevents Varnish from starting.
Restart Varnish after installing the generated VCL:
sudo systemctl restart varnish
Then enable and flush Magento’s full-page cache:
php bin/magento cache:enable full_page
php bin/magento cache:flush full_page
Rollback: return to System > Configuration > Full Page Cache, select Built-in, save, and run php bin/magento cache:flush full_page to restore Magento’s file-based full-page cache.
Worked example: product page layout change
Suppose you update catalog_product_view.xml to move the product short description above the add-to-cart block. After deploying the XML change, the old HTML may remain in Varnish. Flush Magento’s full-page cache:
php bin/magento cache:flush full_page
Request the product page and inspect response headers:
curl -I https://example.com/product-url-key.html
Look for X-Magento-Cache-Status: HIT on repeat requests. The first request after a flush is typically a MISS; later requests should be HIT if the page is cacheable. This check requires network access to the storefront and does not modify data.
Limitations and what to verify
Varnish does not fix every performance problem. Third-party extensions that set session cookies or modify response headers can force cache misses or bypass Varnish entirely. Low-memory servers may run out of memory if Varnish storage is oversized. Stale content is the most common operational risk: if a product price changes but the cache is not invalidated, visitors see the old price until TTL expires or the cache is flushed.
Practical verification: open System > Cache Management in Admin and confirm Full Page Cache status is Valid. Run curl -I on a product URL twice; the second response should show X-Magento-Cache-Status: HIT. For load comparison, run ApacheBench before and after enabling Varnish and compare average response time and 95th percentile latency. Results depend on your hardware and traffic pattern, so no specific numbers are included here.
Closing steps
Enable Varnish in a staging environment first. Verify cache headers on product, category, and CMS pages. Confirm that personalized blocks, such as the mini-cart, are handled through hole-punching or private content so they are not cached for all users. If cache hits are lower than expected, review third-party extensions and custom layout XML for non-cacheable blocks before increasing Varnish memory.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.