1VMOD_XKEY(3) VMOD_XKEY(3)
2
3
4
6 vmod_xkey - Surrogate keys support for Varnish Cache
7
9 import xkey [as name] [from "path"]
10
11 INT purge(STRING keys)
12
13 INT softpurge(STRING keys)
14
16 This vmod adds secondary hashes to objects, allowing fast purging on
17 all objects with this hash key.
18
19 You can use this to indicate relationships, a bit like a "tag". Then
20 clear out all object that have this tag set. Two good use cases are
21 news sites, where one might add all the stories mentioned on a particu‐
22 lar page by article ID, letting each article referenced create an xkey
23 header.
24
25 Similarly with an e-commerce site, where various SKUs are often refer‐
26 enced on a page.
27
28 Hash keys are specified in the xkey response header. Multiple keys can
29 be specified per header line with spaces and/or commas as separators.
30 Alternatively, they can be specified in multiple xkey response headers.
31
32 Preferably the secondary hash keys are set from the backend applica‐
33 tion, but the header can also be set from VCL in vcl_backend_response.
34
35 VCL example:
36
37 vcl 4.0;
38 import xkey;
39
40 backend default { .host = "192.0.2.11"; .port = "8080"; }
41
42 acl purgers {
43 "203.0.113.0"/24;
44 }
45
46 sub vcl_recv {
47 if (req.method == "PURGE") {
48 if (client.ip !~ purgers) {
49 return (synth(403, "Forbidden"));
50 }
51 if (req.http.xkey) {
52 set req.http.n-gone = xkey.purge(req.http.xkey);
53 # or: set req.http.n-gone = xkey.softpurge(req.http.xkey)
54
55 return (synth(200, "Invalidated "+req.http.n-gone+" objects"));
56 } else {
57 return (purge);
58 }
59 }
60 }
61
62 Example
63 On an e-commerce site we have the backend application issue an xkey
64 header for every product that is referenced on that page. So the header
65 for a certain page might look like this:
66
67 HTTP/1.1 OK
68 Server: Apache/2.2.15
69 xkey: 8155054
70 xkey: 166412
71 xkey: 234323
72
73 This requires a bit of VCL to be in place. The VCL can be found above.
74
75 Then, in order to keep the web in sync with the database, a trigger is
76 set up in the database. When an SKU is updated this will trigger an
77 HTTP request towards the Varnish server, clearing out every object with
78 the matching xkey header:
79
80 PURGE / HTTP/1.1
81 Host: www.example.com
82 xkey: 166412
83
84 Note the xkey header. It is probably a good idea to protect this with
85 an ACL so random people from the Internet cannot purge your cache.
86
87 Varnish will find the objects and clear them out, responding with:
88
89 HTTP/1.1 200 Purged
90 Date: Thu, 24 Apr 2014 17:08:28 GMT
91 X-Varnish: 1990228115
92 Via: 1.1 Varnish
93
94 The objects are now cleared.
95
96 INT purge(STRING keys)
97 Description
98 Purges all objects hashed on any key found in the keys argument.
99 Returns the number of objects that were purged.
100
101 The keys may contain a list of space-separated ids.
102
103 INT softpurge(STRING keys)
104 Description
105 Performs a "soft purge" for all objects hashed on any key found
106 in the keys argument. Returns the number of objects that were
107 purged.
108
109 A softpurge differs from a regular purge in that it resets an
110 object's TTL but keeps it available for grace mode and condi‐
111 tional requests for the remainder of its configured grace and
112 keep time.
113
114 Counters
115 #
116 varnish_vsc_begin:: xkey
117
119 Metrics from vmod_xkey
120
121 g_keys – gauge - info
122 Number of surrogate keys
123
124 Number of surrogate keys in use. Increases after a request that in‐
125 cludes a new key in the xkey header. Decreases when a key is purged
126 or when all cache objects associated with a key expire.
127
128 g_hashhead_bytes – gauge - debug
129 Bytes used by all xkey_hashhead objects
130
131 Total bytes used by hashhead objects. Tracks linearly with the num‐
132 ber of surrogate keys in use.
133
134 g_ochead_bytes – gauge - debug
135 Bytes used by all xkey_ochead objects
136
137 Total bytes used by ochead objects. Increases when an object is
138 added to a key or a key is added to an object. Decreases when the
139 relationship is removed.
140
141 g_oc_bytes – gauge - debug
142 Bytes used by all xkey_oc objects
143
144 Total bytes used by oc objects. Tracks linearly with the number of
145 cached objects that are referenced by surrogate keys.
146
147 g_bytes – gauge - info
148 Bytes used by xkeys
149
150 Current number of bytes used by xkeys and their references to the
151 object cache.
152
153
154
155
156 VMOD_XKEY(3)