24

I have discovered that I can set the TTL in Varnish as follows in my VCL file:

sub vcl_fetch {
    # 1 minute
    set obj.ttl = 1m;
}

But what is the default setting (assuming the backend server is setting no cache-control header) ?

Ade
  • 689
  • 3
  • 10
  • 21
  • Looks like I've found the answer: 120 seconds - this is in the default VCL logic. – Ade May 15 '12 at 11:42

2 Answers2

34

Default TTL can be passed through the varnishd command via the -t commandline switch and is probably sourced from a properties file on your filesystem. On the CentOS system I'm looking at it is set using DEFAULT_TTL from /etc/sysconfig/varnish.

You can see the live setting using varnishadm like so,

varnishadm param.show default_ttl

Actually, following default VCL logic relates to non-cacheable objects.

  sub vcl_fetch {
      if (beresp.ttl <= 0s ||
          beresp.http.Set-Cookie ||
          beresp.http.Vary == "*") {
                  /*
                   * Mark as "Hit-For-Pass" for the next 2 minutes
                   */
                  set beresp.ttl = 120 s;
                  return (hit_for_pass);
      }
      return (deliver);
  }

means "if object is not cacheable - pass client requests for this object to backend directly and simultaneously for 2 minutes, do not queue them"

Read more at https://stackoverflow.com/questions/12691489/varnish-hit-for-pass-means

Sergej Alikov
  • 564
  • 4
  • 3
24

This is in the default template:

sub vcl_fetch {
    if (beresp.ttl <= 0s ||
        beresp.http.Set-Cookie ||
        beresp.http.Vary == "*") {
                /*
                 * Mark as "Hit-For-Pass" for the next 2 minutes
                 */
                set beresp.ttl = 120 s;
                return (hit_for_pass);
    }
    return (deliver);
}

So, 120 seconds.

Bart De Vos
  • 17,761
  • 6
  • 62
  • 81
  • Where did you find the default template? – Neil Oct 25 '13 at 15:37
  • https://www.varnish-cache.org/docs/3.0/reference/vcl.html – Bart De Vos Oct 25 '13 at 19:11
  • There is a default.vcl that ships with varnish as of at least 3.0.3 which has all of the default config subroutines commented out in it. The 4.0 default config can be seen here: https://github.com/mattiasgeniar/varnish-4.0-configuration-templates/blob/master/default.vcl – th3morg Aug 07 '15 at 16:43
  • 2
    This doesn't mirror my observations. It is true that the default ttl is 120 seconds, but the source of this number can't be that snippet. If it were, objects without a ttl would be marked as hit_for_pass, but they actually remain in the cache for 120 seconds. – Kritzefitz Mar 14 '17 at 15:54
  • You'll find the source of the builtin vcl here - https://github.com/varnishcache/varnish-cache/blob/master/bin/varnishd/builtin.vcl. Use the `varnish-` tags to see the version for your Varnish release. – Danny Thomas May 06 '19 at 17:50