Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Surrogate Keys? #11

Open
jamiepittock opened this issue Jan 20, 2017 · 2 comments
Open

Surrogate Keys? #11

jamiepittock opened this issue Jan 20, 2017 · 2 comments

Comments

@jamiepittock
Copy link

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
Copy link
Owner

aelvan commented Jan 20, 2017

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
Copy link
Author

jamiepittock commented Jan 20, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants