aelvan / VarnishPurge-Craft

Craft plugin for purging Varnish when elements are saved.
MIT License
33 stars 13 forks source link

Surrogate Keys? #11

Open jamiepittock opened 7 years ago

jamiepittock commented 7 years ago

Hey André

We're currently using Fastly.com (kind of varnish-as-a-service) on a very heavy traffic website and it's working well for us. What I particularly like is their implementation of surrogate keys to selectively purge related content.

We're considering moving to a hosted version of Varnish. Although Fastly us a forked version of Varnish I think Surrogate Keys are available in Vernish 4.x. Have you looked into them?

https://docs.fastly.com/guides/purging/getting-started-with-surrogate-keys

aelvan commented 7 years ago

Interesting, I didn't know about surrogate keys. It's almost like Cache Flag for Varnish. :)

How do you use it with fastly? Do you have a craft plugin for deciding what surrogate keys should be sent pr section, entry type, etc? Or do you just set them from the template? And how is the purge request sent to Fastly with the surrogate keys that you want?

jamiepittock commented 7 years ago

Surrogate keys are set in the templates - a Twig array per template, and then a custom header in the base template to set them in Fastly.

We use a custom plugin to send purge requests to Fastly's API whenever an element is updated.

So I might have urls such as:

Blog list: /blog/ Blog detail: /blog/this-is-an-entry-slug

For each page/url on the site I set some surrogate keys in the templates.

On the blog listing I might have something like this:

{% set fastlySurrogateKeys = [ 'blog', 'blog-index' ] %}

On the blog detail page I set them like this:

{% set fastlySurrogateKeys = [ 'blog', 'blog-detail', 'blog-detail-' ~ entry.id ] %}

Then in my base layout I set a custom header:

{% header "Surrogate-Key: " ~ fastlySurrogateKeys | join(" ") %}

This means we can be very selective when purging.

We have a plugin that fires a request to the Fastly API when ever an entry is updated.

So for example when a blog entry is updated we tell Fastly to soft purge any objects that have the surrogate keys 'home blog-index blog-detail-xxx' (where 'xxx' is the entry ID).

If you get your surrogate key naming right you get really good control. We can soft purge all blog detail pages by telling fastly to soft purge any objects that have the surrogate key 'blog-detail'. Or we can soft purge the whole blog section using the surrogate key 'blog'.

It looks like Varnish 4.1 has surrogate keys via the xkey vmod:

http://book.varnish-software.com/4.0/chapters/Cache_Invalidation.html https://github.com/varnish/varnish-modules