Troubleshooting Varnish·续1
The log output has 2 transactions:
- One for the client-side transaction with the incoming request and outgoing response
- One for the backend fetch and backend response
In the backend transaction, the FetchError tag clearly indicates a Connection refused error. The fact that the backend server is not available is the reason for the failure.
Of course the output can be reduced by only selecting the necessary tags.
Backend health monitoring#
Instead of waiting for backend fetch failures to occur, you can actively monitor the health of your backends. This requires a health probe to be defined and assigned to the backends:
vcl 4.1;
probe health {
.request =
"HEAD / HTTP/1.1"
"Host: localhost"
"Connection: close"
"User-Agent: Varnish Health Probe";
.interval = 10s;
.timeout = 5s;
.window = 5;
.threshold = 3;
}
backend default {
.host = "127.0.0.1";
.port = "8080";
.probe = health;
}
The probe named health will send an HTTP HEAD call for the / resource to the backend every 10 seconds. The probe will time out after 5 seconds. As soon as 3 out of 5 health checks fail, the backend is considered sick.
VSL backend health checks#
The Backend_health VSL tag will return the backend health on a real-time basis. You can run the following command to display the backend health:
sudo varnishlog -g raw -i Backend_health
Here’s the output for the failing backend:
$ sudo varnishlog -g raw -i backend_health
0 Backend_health - boot.default Still sick -------- 2 3 5 0.000000 0.000000 Open error 111 (Connection refused)
0 Backend_health - boot.default Still sick -------- 2 3 5 0.000000 0.000000 Open error 111 (Connection refused)
As you can see, the same Connection refused error is returned. Because the interval of the health probe was set to 10 seconds, a new log line will appear every 10 seconds.
When the backend failure is fixed, the VSL will reflect the fix:
$ sudo varnishlog -g raw -i backend_health
0 Backend_health - boot.default Still sick 4---X-RH 1 3 5 0.003040 0.001013 HTTP/1.1 200 OK
0 Backend_health - boot.default Still sick 4---X-RH 2 3 5 0.005553 0.002148 HTTP/1.1 200 OK
0 Backend_health - boot.default Back healthy 4---X-RH 3 3 5 0.001177 0.001906 HTTP/1.1 200 OK
0 Backend_health - boot.default Still healthy 4---X-RH 4 3 5 0.001419 0.001784 HTTP/1.1 200 OK
Although the first log line already displays an HTTP/1.1 200 OK status, the backend health is not yet healthy. For the backend the be fully healthy, 3 out of 5 checks should succeed according to the health probe settings.
As of the third log line, the backend is considered Back healthy and as of the fourth log line the backend is considered Still healthy.
CLI backend health#
If you don’t want to consult the VSL and want a backend health summary, you can call the backend.list CLI command.
As you can see in the output below, the default backend is considered healthy thanks to the health check probe that is monitoring this backend:
$ sudo varnishadm backend.list
Backend name Admin Probe Last updated
boot.default probe Healthy 5/5 Tue, 04 Jan 2022 10:00:31 GMT
If the backend becomes sick, the backend.list command will reflect this:
$ sudo varnishadm backend.list
Backend name Admin Probe Last updated
boot.default probe Sick 2/5 Tue, 04 Jan 2022 10:04:41 GMT
Note: by default, backend.list only displays the backends attached to the active VCL , which can be confusing if you use VCL labels, for example. To show all the backends, use the optional globbing pattern '*.*' (quoted to avoid shell expansion):
$ sudo varnishadm backend.list '*.*'
Backend name Admin Probe Last updated
boot.default probe Sick 2/5 Tue, 04 Jan 2022 10:04:41 GMT
foo_vcl.backend1 probe Sick 5/5 Tue, 04 Jan 2022 10:04:42 GMT
foo_vcl.backend2 probe Sick 5/5 Tue, 04 Jan 2022 10:04:40 GMT
bar_vcl.default probe Sick 5/5 Tue, 04 Jan 2022 10:04:42 GMT
Varnish is not caching#
Troubleshooting is not only about dealing with errors in Varnish. Sometimes the hit rate of the cache will be too low and the backend will be overwhelmed while Varnish is still in perfect shape.
Figuring out why Varnish isn’t caching pages it was supposed to cache can be done using specific VSL commands.
Cache misses aren’t usually that bad: a miss is just a cache hit that hasn’t taken place yet. Eventually the object will end up in the cache. However, if the TTL of your objects is too low or if the cache is full, cached objects will be removed from the cache.
Cache bypasses on the other hand have a bigger impact: these objects will never end up in the cache. This is either caused by built-in VCL logic that prevents data to be stored in the cache or served from the cache, or by custom VCL logic that has the same effect.
VSL commands#
You could list all VSL transactions that resulted in a pass. The following command will help you with that:
sudo varnishlog -i ReqUrl -i ReqMethod -i ReqProtocol -I ReqHeader:Host \
-I ReqHeader:Cookie -I ReqHeader:Authorization \
-i VCL_call -i VCL_return \
-q "VCL_call eq 'PASS'"
You could also be looking at a specific URL and ask yourself the same question:
sudo varnishlog -i ReqUrl -i ReqMethod -i ReqProtocol -I ReqHeader:Host \
-I ReqHeader:Cookie -I ReqHeader:Authorization \
-i VCL_call -i VCL_return \
-q "ReqUrl eq '/'"
In this case we’re looking for requests that hit the homepage (/).
The output of these commands will tell us why Varnish wasn’t serving the response from the cache
Because it was a POST request#
The following VSL output indicates that an HTTP POST call was used for the request:
$ sudo varnishlog -i ReqUrl -i ReqMethod -i ReqProtocol -I ReqHeader:Host \
-I ReqHeader:Cookie -I ReqHeader:Authorization \
-i VCL_call -i VCL_return \
-q "VCL_call eq 'PASS'"
* << Request >> 2
- ReqMethod POST
- ReqURL /
- ReqProtocol HTTP/1.1
- ReqHeader Host: localhost
- VCL_call RECV
- VCL_return pass
- VCL_call HASH
- VCL_return lookup
- VCL_call PASS
- VCL_return fetch
- VCL_call DELIVER
- VCL_return deliver
And that’s the reason why this request was not served from the cache.
Remember: only GET and HEAD requests are cacheable.
Because the request contained a cookie#
The built-in VCL for the vcl_recv will not serve objects from the cache if the request contains a Cookie header.
The VSL output below illustrates this behavior:
$ sudo varnishlog -i ReqUrl -i ReqMethod -i ReqProtocol -I ReqHeader:Host \
-I ReqHeader:Cookie -I ReqHeader:Authorization \
-i VCL_call -i VCL_return \
-q "VCL_call eq 'PASS'"
* << Request >> 3
- ReqMethod GET
- ReqURL /
- ReqProtocol HTTP/1.1
- ReqHeader Host: localhost
- ReqHeader Cookie: lang=en
- VCL_call RECV
- VCL_return pass
- VCL_call HASH
- VCL_return lookup
- VCL_call PASS
- VCL_return fetch
- VCL_call DELIVER
- VCL_return deliver
Although cookies aren’t cacheable out-of-the-box , you should account for cookie usage in your own VCL logic. Depending on your use case, some cookies may be allowed while the occurence of other cookies should still result in a cache bypass.
Because an Authorization header was passed#
The built-in VCL for the vcl_recv also bypasses the cache when an Authorization header is sent. The idea is that authorized content is private and should not be cached.
Here’s the VSL output for such a request:
$ sudo varnishlog -i ReqUrl -i ReqMethod -i ReqProtocol -I ReqHeader:Host \
-I ReqHeader:Cookie -I ReqHeader:Authorization \
-i VCL_call -i VCL_return \
-q "VCL_call eq 'PASS'"
* << Request >> 98327
- ReqMethod GET
- ReqURL /
- ReqProtocol HTTP/1.1
- ReqHeader Host: localhost
- ReqHeader Authorization: Basic dGVzdDp0ZXN0
- VCL_call RECV
- VCL_return pass
- VCL_call HASH
- VCL_return lookup
- VCL_call PASS
- VCL_return fetch
- VCL_call DELIVER
- VCL_return deliver
Because of an invalid request method#
In an earlier example we bypassed the cache for uncacheable request methods such as POST. Only GET and HEAD are cacheable.
But Varnish will also not cache if it doesn’t recognize the request method. When an invalid request method is used, Varnish will assume the input is not an HTTP request.
At that point the request will be piped to the backend. This means that a TCP connection is opened to the backend, all the incoming bytes are sent, and the response is returned. No effort is made to treat the request or response as HTTP.
In terms of the Varnish Finite State Machine, a pass call is no longer made and instead a pipe call is made.
Here’s how you query for these requests in VSL:
sudo varnishlog -g request -i ReqUrl -i ReqMethod -i ReqProtocol -I ReqHeader:Host \
-I ReqHeader:Cookie -I ReqHeader:Authorization \
-i VCL_call -i VCL_return \
-q "VCL_return eq 'pipe'"
And here’s some example VSL output:
$ sudo varnishlog -g request -i ReqUrl -i ReqMethod -i ReqProtocol -I ReqHeader:Host \
-I ReqHeader:Cookie -I ReqHeader:Authorization \
-i VCL_call -i VCL_return \
-q "VCL_return eq 'pipe'"
* << Request >> 22
- ReqMethod FOO
- ReqURL /
- ReqProtocol HTTP/1.1
- ReqHeader Host: localhost
- VCL_call RECV
- VCL_return pipe
- VCL_call HASH
- VCL_return lookup
** << BeReq >> 23
-- VCL_call PIPE
-- VCL_return pipe
In this example the FOO request method was used, which results in a transition to the vcl_pipe subroutine.
Because of a Hit-For-Miss#
When Varnish receives a response from the backend server, the vcl_backend_response logic will examine the cacheability of the response. If the response is not deemed cacheable, Varnish will create a Hit-For-Miss object that will allow the next requests to bypass the cache as well as the request coalescing waiting list.
As long as the backend response returns uncacheable responses, Hit-For-Misses will occur. We can spot these in VSL by looking for the HitMiss tag.
The following command will filter out all VSL transactions that contain a HitMiss tag.
sudo varnishlog -g request -q "HitMiss"
From a Finite State Machine point of view a transition from vcl_recv to vcl_hash is made. Although the requested object is found in the cache, it is a Hit-For-Miss object, which causes a transition to the vcl_miss subroutine.
A more targeted approach to find uncacheable content is to filter the logs for backend transactions that have uncacheable as one of the values in the TTL tag:
sudo varnishlog -g request -b -i BeReqUrl -I BerespHeader:Cache-Control \
-I BerespHeader:Expires -I BerespHeader:Vary -I BerespHeader:Set-Cookie \
-I BerespHeader:Surrogate-Control -i TTL -q "TTL[6] eq 'uncacheable'"
Let’s use this command, that only displays specific tags, to explore some uncacheable scenarios.
Because of a zero TTL#
One of the potential root causes of a Hit-For-Miss is a response that results in a zero TTL. A TTL of zero can be set through the Cache-Control header or the Expires header.
Here are a couple examples of such header values:
Cache-Control: max-age=0
Cache-Control: s-maxage=0
Expires: Thu, 1 Jan 1970 12:00:00 GMT
Here’s some VSL output that illustrates this scenario:
$ sudo varnishlog -g request -b -i BeReqUrl -I BerespHeader:Cache-Control \
-I BerespHeader:Expires -I BerespHeader:Vary -I BerespHeader:Set-Cookie \
-I BerespHeader:Surrogate-Control -i TTL -q "TTL[6] eq 'uncacheable'"
** << BeReq >> 32810
-- BereqURL /
-- BerespHeader Cache-Control: max-age=0
-- TTL RFC 0 10 0 1641298426 1641298426 1641298426 0 0 cacheable
-- TTL VCL 120 10 0 1641298426 cacheable
-- TTL VCL 120 10 0 1641298426 uncacheable
-- BerespHeader Vary: Accept-Encoding
