Maximizing Magento 2 Performance: Full‑Page Cache with Varnish Explained
Learn why Magento 2’s Full‑Page Cache (FPC) paired with Varnish is the gold‑standard for speed, how to enable it, a concrete configuration example, and the trade‑offs you need to watch out for.
09 Mar 2026, 00:12 UTC

Why Full‑Page Cache Matters for Magento 2
Magento 2’s default rendering pipeline is heavy: every page request spins up PHP, loads the database, builds the layout, and renders HTML. For high‑traffic stores this adds latency and CPU cost. Full‑Page Cache (FPC) stores the final HTML in memory or disk, so repeat visitors hit a static response and avoid the PHP stack entirely. When Varnish is used as a reverse proxy, the cache lives in fast, in‑memory storage, giving the fastest possible response times.
What the Stack Looks Like
Typical production deployment:
- Client Browser → Varnish (reverse proxy) → Magento Application (PHP/FPM) → Redis (session & cache store)
- Varnish holds cached pages; Magento updates the cache on product, CMS, or configuration changes.
Enabling FPC with Varnish – Step‑by‑Step
Configure Magento for Varnish
- In the admin, navigate to
Stores → Configuration → Advanced → System → Full Page Cache. - Set
Caching ApplicationtoVarnish 4(or the version you plan to run). - Save and
bin/magento cache:cleanto flush existing cache.
- In the admin, navigate to
Install Varnish
- On a Linux host, install via package manager (e.g.,
yum install varnish). - Use Magento’s autogenerated VCL:
bin/magento setup:config:set --backend-frontname=admin --http-cache-hosts=127.0.0.1:8080to generatevarnish/default.vcl. - Copy the file to
/etc/varnish/default.vcland reload Varnish:systemctl reload varnish.
- On a Linux host, install via package manager (e.g.,
Point the Web Server to Varnish
- Configure Nginx or Apache to listen on port 80 and proxy to Varnish on port 8080.
- Example Nginx snippet:
server { listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
Verify Cache Status
- Hit a product page:
curl -I http://example.com/product/sku. - Check for
X‑Varnishheader andX‑Magento‑Cache‑Debug: 1. A value of 1 indicates a cache hit; 0 means a miss. - To confirm a miss, clear Magento cache:
bin/magento cache:cleanand re‑request the page. The header should switch to 0.
- Hit a product page:
Concrete Example: Custom Varnish VCL Rules
Magento’s default VCL excludes cart, checkout, and admin URLs. If you add a custom module that introduces a new non‑cacheable route, you must update VCL. Below is a minimal example that blocks /my-custom-route from caching.
# Inside /etc/varnish/default.vcl
sub vcl_recv {
if (req.url ~ "^/my-custom-route") {
return (pass);
}
# ... existing Magento rules ...
}
After editing, reload Varnish and run bin/magento cache:flush to ensure the new rule takes effect.
Trade‑Offs and Caveats
- Dynamic Content Exposure – If a VCL rule mistakenly allows a session‑specific page to be cached, users could see each other’s cart contents. Always double‑check that URLs containing session identifiers or customer data are marked
pass. - Stale Pages under Heavy Load – Varnish’s default memory limits may cause evictions when traffic spikes. Monitor
varnishstatand consider increasingstoragesize or usingVarnish 6with more efficient memory usage. - Cache Invalidation Lag – While Magento triggers cache invalidation on product or CMS changes, there can be a short window where an updated page is still served from Varnish. Use
bin/magento cache:cleanafter bulk updates to reduce the window.
Actionable Next Steps
Enable FPC with Varnish following the steps above.
Run the
X‑Magento‑Cache‑Debugheader test to confirm hits. Document the header values in your monitoring dashboard.Review all custom modules for non‑cacheable routes and update VCL accordingly.
Set up a routine
bin/magento cache:flushafter any bulk catalog changes, and validate that the new content appears.Monitor Varnish memory usage with
varnishstat -1and adjuststorageparameters if evictions increase.
By following this guide, you’ll reduce page load times, lower server CPU usage, and deliver a smoother shopping experience. Remember that the key to successful FPC is ongoing observation—keep an eye on cache hit rates, memory usage, and the freshness of dynamic content.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.