web app for the FT 2. Created our Origami component system 3. Ran FT Labs for 3 years 4. Now working with Nikkei to rebuild nikkei.com 5. Also W3C Technical Architecture Group 6. Live in Tokyo, Japan 2 Pic of me.
2. Everything happens in metadata 3. Very restricted: No loops or variables 4. Extensible: some useful Fastly extensions include geo-ip and crypto 5. Incredibly powerful when used creatively 7
useSsl, }, … ] {{#each backends}} backend {{name}} { .port = "{{p}}"; .host = "{{h}}"; } {{/each}} let vclContent = vclTemplate(data); fs.writeFileSync( vclFilePath, vclContent, 'UTF-8' ); services.json Defines all the backends and paths that they control. routing.vcl.handlebars VCL template with Handlebars placeholders for backends & routing build.js Task script to merge service data into VCL template
set req.backend = {{backendName}}; • Match a route pattern: if (req.url ~ "{{pattern}}") • Remember to set a Host header: set req.http.Host = "{{backendhost}}"; • Upload to Fastly using FT Fastly tools ◦ https://github.com/Financial-Times/fastly-tools 11
hit ratio This is great if... • You have a response that is tailored to different device types • There are a virtually infinite number of User-Agent values 2
Agent to the URL and restart the original request Add a Vary: User-Agent header to the response before sending it back to the browser We call this a preflight request
request headers: set req.http.tmpOrigURL = req.url; • Change the URL of the backend request: set req.url = "/api/normalizeUA?ua=" req.http.User-Agent; • Reconstruct original URL adding a backend response header: set req.url = req.http.tmpOrigURL "?ua=" resp.http.NormUA; • Restart to send the request back to vcl_recv: restart; 18
VCL This is great if... • You have a federated login system using a protocol like OAuth • You want to annotate requests with a simple verified authentication state 3
front end perf or cache efficiency This is great if... • You want to serve different versions of your site to different users • Test new features internally on prod before releasing them to the world 4
JSON file will be fine ◦ Include all possible values of each flag and what percentage of the audience it applies to ◦ Publish it statically - S3 is good for that • A flag toggler tool ◦ Reads the JSON, renders a table, writes an override cookie with chosen values • An API ◦ Reads the JSON, responds to requests by calculating a user's position number on a 0-100 line and matches them with appropriate flag values • VCL ◦ Merges flag data into requests
(req.flags.has('highlights')) { // Enable highlights feature } }); HTTP/1.1 200 OK Vary: Nikkei-Flags ... Middleware provides convenient interface to flags header Invoking the middleware on a request automatically applies a Vary header to the response
VCL This is great if... • You have a bug you can't reproduce without the request going through the CDN • You want to test a local dev version of a service with live integrations 5
forwarded IP is whitelisted or auth header is also present GET /article/123 Backend-Override: article -> fc57848a.ngrok.io Detect override header, if path would normally be routed to article, change it to override proxy instead. ngrok fc57848a .ngrok.io Normal production backends
override: set req.http.tmpORBackend = regsub(req.http.Backend-Override, "\s*\-\>.*$", ""); • Check whether current backend matches if (req.http.tmpORBackend == req.http.tmpCurrentBackend) { • Use node-http-proxy for the proxy app ◦ Remember res.setHeader('Vary', 'Backend-Override'); ◦ I use {xfwd: false, changeOrigin: true, hostRewrite: true} 35
response header This is great if... • You find it hard to understand what path the request is taking through your VCL • You have restarts in your VCL and need to see all the individual backend requests, not just the last one 6
And no backend. This is great if... • You want to track down hotspots of slow response times • You'd like to understand how successfully end users are being matched to their nearest PoPs 7
transaction ID to the API, and get all varnishlog events relating to that transaction, including related (backend) transactions 55 > fastly log 1467852934 17 SessionOpen c 66.249.72.22 47013 :80 17 ReqStart c 66.249.72.22 47013 1467852934 17 RxRequest c GET 17 RxURL c /articles/123 17 RxProtocol c HTTP/1.1 17 RxHeader c Host: www.example.com ...