How Magento cache clearing works with Varnish
This topic discusses the basics of using Varnish as a web caching accelerator for Magento.
According to Varnish documentation, “A purge is what happens when you pick out an object from the cache and discard it along with its variants.” A Varnish purge is very similar to a Magento cache clean command (or clicking Flush Magento Cache in the Magento Admin).
In fact, as discussed in this section, when you clean, flush, or refresh the Magento cache, Varnish purges as well.
After you’ve installed and configured Varnish to work with Magento, the following actions can result in a Varnish purge:
Maintaining a website.
For example, anything you do in the Admin in:
- STORES > Configuration > GENERAL > General
- STORES > Configuration > GENERAL > Currency Setup
- STORES > Configuration > GENERAL > Store Email Addresses
When Magento detects such a change, a message displays informing you to refresh the cache. To do this, see Refresh the Magento cache.
Maintaining a store (for example, adding or editing categories, prices, products, and promotional pricing rules).
Varnish is purged automatically when you perform any of these tasks.
Maintaining source code.
You should refresh the cache and also periodically delete everything in the
generated/metadatadirectories. For information on refreshing the cache, see the next section.
Configure Magento to purge Varnish
Magento purges Varnish hosts after you configure Varnish hosts using the
magento setup:config:set command.
You can use the optional parameter
--http-cache-hosts parameter to specify a comma-separated list of Varnish hosts and listen ports. Configure all Varnish hosts, whether you have one or many. (Do not separate hosts with a space character.)
The parameter format must be
<hostname or ip>:<listen port>, where you can omit
<listen port> if it’s port 80.
magento setup:config:set --http-cache-hosts=192.0.2.100,192.0.2.155:6081
You can then purge Varnish hosts when you refresh the Magento cache (also referred to as cleaning the cache) in the Magento Admin or using the command line.
To refresh the cache using the Admin, click SYSTEM > Tools > Cache Management, then click Flush Magento Cache at the top of the page. (You can also refresh individual cache types.)