Securing high performance and speed for web content delivery relies on smart caching strategies. Caching content is a well-understood principle, but this still doesn’t make smart caching a given. Cache invalidation isn’t easy, but important things rarely are. Varnish can help.
A good caching strategy defines how content should be cached as well as how it should be invalidated. A cache is designed to store and serve content so that content requests do not have to travel to the backend or origin server. This makes for speedier content delivery because of fewer trips back and forth to fetch content from the backend, and it relieves pressure on the backend, protecting the origin from being overloaded by too many requests.
Nothing lasts forever: Cached objects die eventually
But yes, inevitably, objects shouldn’t live in the cache forever. They stay in cache until they expire, are evicted to make room for other objects, or are invalidated. You’re always going to need to make room for fresh content when new or updated content appears.
The bottom line: If you want to make optimal use of your cache, you don’t want to touch anything from the backend or origin unless it has changed.
Cache invalidation: Telling the cache that content has changed
Cache invalidation is a method for telling the cache that content has changed, and that it’s time to fetch the latest version from the origin.
With Varnish cache invalidation is done using keys or tags. In Varnish Cache you will use the Xkey VMOD, while in Varnish Enterprise you will use the Ykey. What do these keys do? They let you attach tags to objects when you insert them into the cache; these are known as surrogate keys. When you assign these keys to any object or asset in the cache, the backend sends a header to instruct the cache to associate a key with the response.
For example, when a programmer sends this header, they want to be able to invalidate all assets in the cache that are related to a specific item. Let’s say it’s a pair of shoes in a shop, and there is a new price or a new image for the item. These keys associated with that item make it easy to invalidate all the items each time an update happens. A quick and practical way to make these content changes. Other examples include content that could change frequently, such as media articles and news items, but it could essentially apply to any element of a web page that contains elements you want to invalidate all at once.