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.
1 Answers
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.
- Varnish sees 500 status -> talks to backend and does not cache the page for 2 min
- 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);
}
}

- 8,725
- 2
- 29
- 35
-
you are 100% right! recently measured with timer myself - its 2min problem period. If just after 500 error, backend is OK again, why its response can not be cached before this 2m pass? – Molfar Jul 03 '17 at 13:09
-
If backend is ok after 10 sec after 500 error, how to avoid fetching it again and again within next 110 sec? – Molfar Jul 03 '17 at 13:18
-
See updated answer. Something like that should work (untested code). – Danila Vershinin Jul 03 '17 at 17:23
-
Is it OK to use deliver only? Without changing ttl and uncacheable – Molfar Jul 03 '17 at 18:30
-
Without those, you will not mark the page as uncacheable at all, and Varnish will be "unoptimized" in the way it sends requests to backend (when it gets many requests for that same page) – Danila Vershinin Jul 03 '17 at 18:52
-
Alright, I understand. Your way it will stay uncachable for 10 sec, whatever response from backend throughout this period. – Molfar Jul 03 '17 at 19:07