Molfar
Molfar

Reputation: 1501

Why Varnish stops caching 200 response after 500 error

I cant understand the behavior of Varnish in case of 500 error from backend. - Why it increments MAIN.n_object counter? I think it should cache only 20x and redirects. - If first request finished with 500 response from backend, all subsequent requests to the same url not caching, even if backend begins return 200 response. Help me to understand this logic.

Upvotes: 0

Views: 1374

Answers (1)

Danila Vershinin
Danila Vershinin

Reputation: 9895

If you're really using default VCL, then the default logic is as you describe. But you're missing that it does start caching it after a while. Typically 2 minutes.

  1. Varnish sees 500 status -> talks to backend and does not cache the page for 2 min
  2. Later Varnish sees 200 status -> Varnish caches the page and delivers it further on from cache.

This is required to implement hit-for-pass - My understanding of this is the following: Varnish will by default pile-up requets to backend and not send them as they arrive for optimization. When Varnish sees that something is not cacheable (500 status, etc.) it will not do the pile-up behavior and talk to backend directly (hit-for-pass).

In case you want to decrease the amount of time that pages are marked as hit-for-pass, you would need to add some VCL. This will make sure that built-in VCL with 120s value is not run. The following will mark a page with 500 status as uncacheable for 10 seconds:

sub vcl_backend_response {
    if (beresp.status == 500) {
        set beresp.ttl = 10s;
        set beresp.uncacheable = true;
        return (deliver);
    }
}

Upvotes: 1

Related Questions