Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.)

like image 527
callum Avatar asked Apr 16 '12 12:04

callum


People also ask

How does varnish work with caching?

If the request is not cached, Varnish will forward the request to the web server’s backend and cache the result, as we already saw in the general reverse proxy paragraph. The cached requests are then stored in the memory: from this moment on, retrieving and delivering them to clients will be much faster.

What happens when varnish is unable to respond?

If varnish is unable to respond to the query from its cache, it forwards the request to the backend, receives the response from the backend, stores it in its cache and then delivers it to the client who made the request. The diagram above explains the process of caching using varnish in a simple manner.

How does Varnish cache reduce time to first byte (TTFB)?

Time To First Byte (TTFB) decreases because the processing time for the backend server database is lower. You can use Varnish as part of a highly available environment to serve cached content even when the web server is experiencing downtimes (more on this below). How Does Varnish Cache Work?

What is a Varnish cache reverse proxy?

Now that you know what a reverse proxy is, you’re ready to dig into the magic of Varnish Cache! Varnish acts as a cache HTTP reverse proxy and sometimes you can also see it defined as a front-end accelerator. It’s not a stand-alone solution, because it needs a dedicated web server to rely on, like NGINX or Apache.


Video Answer


1 Answers

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

like image 194
Łukasz Rysiak Avatar answered Oct 10 '22 12:10

Łukasz Rysiak