Varnish

Varnish Default TTL

by , , revisited on


We have by far the largest RPM repository with dynamic stable NGINX modules and VMODs for Varnish 4.1 and 6.0 LTS. If you want to install NGINX, Varnish, and lots of useful modules for them, this is your one-stop repository to get all performance-related software.
You have to maintain an active subscription in order to be able to use the repository!

Varnish default TTL is the time for which an object is cached when no cache information has been sent by your application. The default is 120 seconds (2 minutes).

When doest Varnish default TTL apply?

Most of the modern PHP apps do not send any cache-related headers (Cache-Control), thus the default 120 seconds apply if you installed Varnish and haven’t configured much.

Only cacheable status codes

It’s worth noting that only specific status codes are cacheable in Varnish by default (at least so in Varnish 4.1 series).
It means that neither default TTL nor “calculated TTL” (from caching headers like s-maxage) would apply for non-cacheable status codes.
So if your response code is any different than the ones listed below, you might have to create VCL tweaks in order to make those cacheable.

Cacheable status codes:

  • 200: OK
  • 203: Non-Authoritative Information
  • 300: Multiple Choices
  • 301: Moved Permanently
  • 302: Moved Temporarily
  • 304: Not modified
  • 307: Temporary Redirect
  • 410: Gone
  • 404: Not Found

When a response has any of the given codes, and Varnish finds no caching headers in backend’s response, the initial cache lifetime (TTL) for it, will be set to the default TTL.
This is how long Varnish will cache an object by default.

Deciding on the correct default TTL value

The common approach to the matter of deciding on the correct value depends on your application. But for the most part, the correct strategy is setting the default TTL to a really high value: raise it to 2 weeks, and make your application send a PURGE request to Varnish when there’s a need to invalidate an object in the cache.

Change Varnish default TTL via VCL

This is the most straightforward approach. Adjust your VCL to include:

sub vcl_backend_response {
    set beresp.ttl = 2w;
}

Note that this will actually enforce the given TTL to any object, unless you further fine-tune the TTL for specific objects, later in your VCL code.

Think of it as the enforcing default, because Varnish will then ignore any cache headers coming from the backend and use the specified value, so be careful with this approach.

Change default TTL in Varnish configuration

This is the preferred way to change it. However, not only depends on the operating system in use but also where the Varnish package came from.

CentOS 7. Varnish installed from EPEL repository

Open /etc/varnish/varnish.params and adjust the value (specified in seconds) to 2 weeks:

VARNISH_TTL=1209600

CentOS 7. Varnish installed from its official repository

Open /etc/varnish/varnish.params and add default TTL as parameter in DAEMON_OPTS like so:

DAEMON_OPTS="-t 1209600 ... other parameters ..."

Verify change to default TTL

The following command will show the default TTL that applies to currently launched Varnish instance:

varnishadm param.show default_ttl

Naturally, the command will show the correct value if you changed TTL via configuration and not the VCL approach.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.