How does Varnish know how long to cache each response for?

callum picture callum · Apr 16, 2012 · Viewed 8.2k times · Source

Does Varnish simply follow the Cache-Control header from the origin server?

And are there any other ways that you can control how long it caches a response for? For example, can you tell Varnish to cache a response “indefinitely” (i.e. “until further notice”) and then later explicitly instruct it to delete that object from the cache when you know the underlying data has changed?

(Please note: I've never used Varnish; I'm just trying to work out whether it would be a good fit for a forthcoming project.)

Answer

Łukasz Rysiak picture Łukasz Rysiak · Apr 17, 2012

Those are very basic questions. I think you should start from reading great docs on https://www.varnish-cache.org/docs/

To answer your question: It depends on how you configure varnish.

You can leave the defaults so it'll use expires;

You can set it up to have different TTL(Time To Live) for each domain/backend/filetype/cookie...

If you set it up with ie. 1year cache TTL, you can remove it from cache by "Purging" specific address/url or whole domain.

You can do so in two ways:

  1. by PURGE HTTP Method if you have it configured in your vcl file
  2. by using purge command in varnishadm/varnish console

https://www.varnish-cache.org/docs/2.1/tutorial/purging.html