Varnish Server throws error 503 when the back-end is down

cachevarnish

Varnish immediately starts showing a 503 Service Unavailable error when the back-end of the site is down. The configuration is that everything should be cached for 7 days.

I would expect the site to stay up if the back-end would be down for an hour. The cache-server should still serve pages from it's cache.

The site is an entirely static site with 300.000 page views per day. The hit-rate so far is 2:5 (MISS – HIT), I think this should be higher as well.

The /etc/varnish/default configuration:

backend www {
   .host = "ip address here";
   .port = "http";
   .connect_timeout = 1s;
   .first_byte_timeout = 5s;
   .between_bytes_timeout = 2s;
}
sub vcl_deliver {
        if (obj.hits > 0) {
                set resp.http.X-Cache = "HIT";
        }
        else {
                set resp.http.X-Cache = "MISS";
        }
}
sub vcl_recv {
        #if (req.request == "GET" && req.url ~ "\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|cgi|ico)$") {
                unset req.http.cookie;
                return (lookup);
        #}
}
sub vcl_fetch {
        if (beresp.http.Set-Cookie) {
        #if (req.request == "GET" && req.url ~ "\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|cgi|ico)$") {
                unset beresp.http.set-cookie;
                set beresp.ttl = 7d;
                return(deliver);
        }
}

The .htaccess from the website:

<IfModule mod_headers.c>
SetEnv force-no-vary
Header append Vary: Accept-Encoding
Header unset Pragma
Header unset ETag
Header unset Last-Modified
FileETag None

# YEAR
<filesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf)$">
  Header set Cache-Control "max-age=29030400"
</filesMatch>
# WEEK
<filesMatch "\.(js|css|swf)$">
  Header set Cache-Control "max-age=604800"
</filesMatch>
# 3 DAYS
<filesMatch "\.(php|cgi|pl|htm)$">
  Header set Cache-Control "max-age=259200"
</filesMatch>
</ifModule>

Best Answer

You need to look at implementing Grace mode and/or Saint mode.

Varnish Grace & Saint modes

Grace mode allows you to serve stale content when your backends are down or slow, and saint mode lets you retry another backend if the backend you used responds with an error.