0

I'm wondering if my (possibly strange) use case is possible to implement in Varnish with VCL. My application depends on receiving responses from a cacheable API server with very low latencies (i.e. sub-millisecond if possible). The application is written in such a way that an "empty" response is handled appropriately (and is a valid response in some cases), and the API is designed in such a way that non-empty responses are valid for a long time (i.e. days).

So, what I would like to do is configure varnish so that it:

  1. Attempts to look up (and return) a cached response for the given API call
  2. On a cache miss, immediately return an "empty" response, and queue the request for the backend
  3. On a future call to a URL which was a cache miss in #2, return the now-cached response

Is it possible to make Varnish act in this way using VCL alone? If not, is it possible to write a VMOD to do this (and if so, pointers, tips, etc, would be greatly appreciated!)

dcrosta
  • 26,009
  • 8
  • 71
  • 83

1 Answers1

1

I don't think you can do it with VCL alone, but with VCL and some client logic you could manage it quite easily, I think.

In vcl_miss, return an empty document using error 200 and set a response header called X-Try-Again in the default case. In the client app, when receiving an empty response with X-Try-Again set, request the same resource asynchronously but add a header called X-Always-Fetch to the request. Your app does not wait for the response or do anything with it once it arrives. Also in vcl_miss, check for the presence of the same X-Always-Fetch header. If present, return (fetch) instead of the empty document. This will request the content from the back end and cache it for future requests.

I also found this article which may provide some help though the implementation is a bit clunky to me compared to just using your client code: http://lassekarstensen.wordpress.com/2012/10/11/varnish-trick-serve-stale-content-while-refetching/

Johnny C
  • 1,799
  • 1
  • 16
  • 27
  • Thank you for your answer! I have a similar use case and am wondering if you know if anything has changed in the last 5 years to have Varnish handle this itself as opposed to having the client re-request. – Gil Oct 10 '18 at 03:33