HTTP Caching mit Varnish


IPC München, 24. Oktober 2016


© David Buchmann

Was ist schon wieder ein reverse proxy?

What could possibly go wrong?

httpstatusdogs.com

Überblick




HTTP Refresher

Das HTTP Protokoll

Request

GET /path
Accept-Encoding: text/html
            

Response

HTTP/1.1 200 OK
Content-Type: text/html

<html>...</html>
            

HTTP Verben

HTTP Antwort-Codes

twitter.com/stevelosh/status/372740571749572610




HTTP Cache Control

Cache Control Header

HTTP 1.1, RFC 2616, Kapitel 13.2 und 13.3

Cache Expiration

Cache-Control: s-maxage=3600, max-age=900
Expires: Thu, 15 May 2014 08:00:00 GMT
            
  1. s-maxage
  2. max-age
  3. Expires (HTTP 1.0 - avoid!)
  4. Wenn nichts angegeben: default_ttl

Cache Validation

ETag: 82901821233

If-None-Match: 82901821233

304 Not Modified

Nicht cachen

Cache-Control: s-maxage=0, private, no-cache
            

www.varnish-cache.org/trac/browser/bin/varnishd/default.vcl?rev=3.0 (Varnish 3)
www.varnish-cache.org/trac/browser/bin/varnishd/builtin.vcl?rev=4.0 (Varnish 4)

Voreingestellte Varnish Konfiguration

Varianten auseinander halten

Inhalt hängt von Request Header ab

GET /resource
Accept: application/json
            
GET /resource
Accept: text/xml
            
Vary: Accept
            

Varnish macht genau das,
was du ihm sagst


Bitte genau aufpassen und gut testen

Varnish Configuration Language

VCL: Debuggen time to live

sub vcl_backend_response {
    set beresp.http.TTL = beresp.ttl;
}
            

VCL: Debuggen cache hit / miss

sub vcl_deliver {
    # If X-Varnish contains only 1 id, we have
    # a miss, if it contains more (and
    # therefore a space), we have a hit.

    if (resp.http.X-Varnish ~ " ") {
        set resp.http.Debug-Cache = "HIT";
    } else {
        set resp.http.Debug-Cache = "MISS";
    }
}
            

VCL: Zwei Applikationen

backend default {
    .host = "127.0.0.1"; .port = "8080";}
backend legacy {
    .host = "127.0.0.1"; .port = "8000";}

sub vcl_recv {
    if (req.url ~ "^/archive/") {
        set req.backend_hint = legacy;
    } else {
        set req.backend_hint = default;
    }
}
            

VCL kann vieles


Aber bitte nicht zuerst schauen, dass die Webanwendung HTTP korrekt verwendet!

Fortgeschrittene Themen

Cache Invalidierung

Die zwei harten Probleme in der Computerwissenschaft:

  1. Naming things
  2. Cache Invalidierung
  3. Off by one Fehler

Cache busting

<link rel="stylesheet" href="/css/style.css?v1" type="text/css"/>
...
<script src="/js/scripts.js?v1"></script>
            

Explizite Cache Invalidierung

Arten der Invalidierung

Invalidierung senden

Eigene VCL für purge

acl invalidators {
    "localhost";
}

if (req.method == "PURGE") {
    if (!client.ip ~ invalidators) {
        return (synth(405, "Not allowed"));
    }
    return (purge);
}

...
            

Banning

vcl_backend_response {
    set beresp.http.X-Url = bereq.url;
    set beresp.http.X-Host = bereq.http.host;
}

vcl_recv {
  if (req.method == "BAN") {
    if (!client.ip ~ invalidators) {
      return (synth(405, "Not allowed"));
    }
    ban("obj.http.X-Host ~ " + req.http.X-Host
      + " && obj.http.X-Url ~ " + req.http.X-Url
    );
  }
}
            



Cache Tagging

Cache Tagging

$response->withHeader('X-Cache-Tags', 'id-42');
            
ban("obj.http.x-cache-tags ~ "
      + req.http.x-cache-tags
);
            

FOSHttpCacheBundle

/** @var $cm CacheManageer */
$cm->tagResponse($response, array('id-42'));
...
$cm->invalidateTags(array('id-42'));
use FOS\HttpCacheBundle\Configuration\Tag;
class CommentController extends Controller {
    /**
     * @Tag({"comments", "'comment-'~id"})
     */
    public function commentAction($id)
    // ...



Edge Side Includes

Edge Side Includes verwenden

Wie server side include, aber in Varnish:




Caching und Sessions

Strategien für Caching mit Sessions




Wrap-Up

Take-Aways

Ausblick: Code Bibliotheken

Outlook: Wie weiter?

Outlook: Caching ist nicht alles

Danke!


@dbu