Caching based on response header

Kristian Lyngstol kristian at
Thu Sep 15 16:02:17 CEST 2011


On Thu, Sep 15, 2011 at 04:51:24PM +0300, Barak Yaish wrote:
> The server (Tomcat) basically acts as a cache, where the key is page url and
> the value is some info on the page. Clients accessing the service pass the
> url in the request, and if the data exist in the cache, it will be returned
> to client, otherwise a default response will be returned to client, and a
> background process will start process the page, in its end the page data
> will be placed in the cache. So, since Tomcat (java) do not handle network
> very well in high scales, I was thinking I could use Varnish to cache
> backend responses in case found in the service cache. Isn't it possible to
> configure Varnish to fetch from cache based on some header in the request
> (in my case it will be the page url that client is querying)?

You two seem to be talking about slightly different things.

You can use response headers to determine if something is to be cached.
This is done in VCL, in the vcl_fetch method, and is fairly common.
If you set a Cache-Control header on your response with either a
s-maxage or max-age variable (s-maxage takes priority), Varnish will, by
default cache the content for that period of time. In addition, if
neither variables are present and no Expires header is present, there is
a default cache period of 120 seconds (configurable).

There are some other factors here too, like cookies, response codes etc,
but that's better left to the documentation.

And while you can't ask the web server if you should fetch something
from cache (well, you can, but I doubt that's what you really want, and
it's not trivial), I suspect you did not mean "decide to fetch from
cache" so much as "decide to cache"?

As for documentation, is the
place to go. Particularly the part about Using Varnish, which should
have ample examples.

Hope this cleared things up!

- Kristian

More information about the varnish-misc mailing list