Varnish 5.1 always online

Recently I posted about Varnish with secure AWS S3 bucket as backend and I wanted to have the “always online” enabled, meaning you can take the backend offline while serving from Varnish cache.

Here is how it works for Varnish 5.1:

vcl 4.0;

backend example {
  .host = "";
  .port = "80";

sub vcl_backend_response {
    //override page cache. Comment it out to allow backend to override it
    set beresp.ttl = 10s;
    //use the cached version of the page for up to a week long if backend is down
    set beresp.grace = 1w;
    //also hide 500+ errors and serve healthy cache instead
    if (beresp.status >= 500 && beresp.status <= 599) {
        return (abandon);

sub vcl_hit {
   if (obj.ttl >= 0s) {
       // normal delivery
       return (deliver);
   if (obj.ttl + obj.grace > 0s) {
       // Object is in grace, deliver it and try a fetch in the background
       return (deliver);
   return (miss);

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.