Search code examples
cachingvarnish

Common varnish: difference between beresp & resp, bereq & req; req.ttl & beresp.ttl


Tell me please, whats the difference between these concepts? I didn't found any answers in documentations of Varnish. They just operative these concepts, nothing more.

And what is better to use for caching: beresp.ttl or max-age in Cache-control header?

If you can do it with little examples - do it please :)


Solution

  • req: The request values as soon as it arrive at Varnish.

    bereq: The request that goes to the backend. All variables from req are automatically assigned to bereq. However, those values may slightly differ, because Varnish may modify client requests. For example, HEAD requests coming from clients may be converted to GET requests towards the backend.

    beresp: The backend respose. Any changes in beresp affect resp and obj which is the cached object. Tip: If you want to get any additional info to your object, set it to beresp.

    resp: The response that is delivered to the client. All the beresp values are passed on to resp.

    After all the names explained it is elementary to conclude that req.ttl is the TTL received from the request and it means nothing unless you configure it differently. beresp.ttl is the one that is going to set the TTL of your object.

    As about the what's best for caching, Varnish already gets the max-age and sets it to beresp.ttl. So in the end there's no difference. The difficulty of employing this is that some web apps do not treat the max-age correctly and end up always sending nocache causing Varnish to cache nothing. In those cases you should ignore what comes in beresp.http.cache-control and set your own TTL.

    Some relevant reading to deepen further in this topic can be found in the Varnish Book subroutines section.