Troubleshooting Varnish·续2
You can clearly spot the Cache-Control: max-age=0 response header that was sent by the backend. And although the first occurence of the TTL tag sees this as a cacheable response, the built-in VCL for this scenario will turn this into an uncacheable response and creates a Hit-For-Miss object as a result.
Because of private, no-cache, no-store#
Another scenario where a response is deemed uncacheable is when the Cache-Control response header contains keywords like private, no-cache or no-store.
Here’s some VSL output for 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 >> 43
-- BereqURL /
-- BerespHeader Cache-Control: private, no-cache, no-store
-- TTL RFC 120 10 0 1641298797 1641298797 1641298796 0 0 cacheable
-- TTL VCL 120 10 0 1641298797 cacheable
-- TTL VCL 120 10 0 1641298797 uncacheable
-- BerespHeader Vary: Accept-Encoding
The Cache-Control: private, no-cache, no-store response header is responsible for this response not being cacheable, which is in accordance with the the built-in VCL for this scenario.
Because of Surrogate-Control: no-store header#
When the Surrogate-Control response header contains the no-store keyword, Varnish will not cache the response.
Here’s some VSL output for 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 >> 46
-- BereqURL /
-- BerespHeader Surrogate-Control: no-store
-- TTL RFC 120 10 0 1641298986 1641298986 1641298985 0 0 cacheable
-- TTL VCL 120 10 0 1641298986 cacheable
-- TTL VCL 120 10 0 1641298986 uncacheable
-- BerespHeader Vary: Accept-Encoding
Have a look at the built-in VCL for this scenario.
Because of a Set-Cookie headers#
The built-in VCL describes how Varnish deals with cookies out-of-the-box.
But while the occurrence of a Cookie request header causes a cache bypass, the occurrence of a Set-Cookie header in the response causes a Hit-For-Miss object being created.
When setting a cookie, a state change is implied, which is not cacheable. The built-in VCL has measures in place to prevent responses with Set-Cookie headers from being cached.
Here’s some VSL output for 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 >> 49
-- BereqURL /
-- BerespHeader Set-Cookie: language=en; expires=Thu, 03-Feb-2022 12:31:26 GMT; Max-Age=2592000; path=/
-- TTL RFC 120 10 0 1641299487 1641299487 1641299486 0 0 cacheable
-- TTL VCL 120 10 0 1641299487 cacheable
-- TTL VCL 120 10 0 1641299487 uncacheable
-- BerespHeader Vary: Accept-Encoding
Because of wildcard variations#
The Vary header can be used to issue cache variations based on certain request header values.
A Vary: Accept-Language header will make sure a variation of a cached object is created based on the value of the Accept-Language request header. This ensures that you can cache multilingual content without having to write extra VCL.
The challenge is to strike the right balance between having enough cache variations and maintaining an acceptable hit rate.
But if a response contains a Vary: * header, Varnish is supposed to vary on all request headers. This defies the purpose of caching and the built-in VCL considers this an uncacheable scenario.
Here’s some VSL output for 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 >> 32813
-- BereqURL /
-- BerespHeader Vary: *
-- TTL RFC 120 10 0 1641299772 1641299772 1641299771 0 0 cacheable
-- TTL VCL 120 10 0 1641299772 cacheable
-- TTL VCL 120 10 0 1641299772 uncacheable
-- BerespHeader Vary: *, Accept-Encoding
Debug logs#
Throughout this tutorial we’ve used the Varnish Shared Memory Logs (VSL) quite extensively. However, from a troubleshooting and debugging point of view, we haven’t yet looked at the Debug VSL tag.
Varnish will occassionally add some debug information to the logs through the Debug tag. VMOD developers may also use this tag to log information that wouldn’t fit in any other tag.
One example is an HTTP/2 upgrade request on a Varnish system that doesn’t have HTTP/2 enabled. The protocol upgrade happens through the Upgrade: h2c request header.
When Varnish notices it cannot upgrade the connection to HTTP/2 because the -p feature=+http2 runtime parameter wasn’t passed, it continues to serve HTTP/1.1 content, but logs the upgrade attempt through a Debug VSL tag.
Here’s an example:
$ sudo varnishlog -g request -c -i "Req*" -i Debug
* << Request >> 54
- Debug "H2 upgrade attempt%00"
- ReqStart 127.0.0.1 47000 a0
- ReqMethod GET
- ReqURL /
- ReqProtocol HTTP/1.1
- ReqHeader Host: localhost
- ReqHeader User-Agent: curl/7.74.0
- ReqHeader Accept: */*
- ReqHeader Upgrade: h2c
- ReqHeader X-Forwarded-For: 127.0.0.1
- ReqAcct 87 0 87 273 33 306
Timeouts#
Varnish has some timeouts that can be configured, which can improve the overall experience.
Although you can focus on reducing the amount of time it takes to receive a request or the amount of time it takes to generate a response, sometimes it just makes sense to increase the timeout value.
Varnish allows you to configure both client-side timeouts as well as a collection of backend timeouts.
Client-side timeouts#
The timeout_idle parameter is one of these. It’s a sort of keep-alive timeout that defines how long a connection remains open after a request. If no new pipelined request is received on the connection within five seconds, the connection is closed.
The idle_send_timeout parameter defines how long Varnish is willing to wait for the next bytes, after having already received data. This is a typical between-bytes timeout.
And then there’s also the send_timeout parameter, which acts as a last-byte timeout.
From a Denial of Service (DoS) perspective these settings can help you prevent slowloris attacks.
Slowloris attacks are Denial of Service (DoS) attacks that hold the connection open as long as possible. The goal is to exhaust all available connections and cause new, valid connections to be refused.
From a performance point of view, these settings can also be used to improve the end-user experience. If your Varnish servers sit behind a set of load balancers, it makes sense to increase timeout_idle because you know they are the only devices that are directly connecting to Varnish, and they are most probably going to reuse their connections with Varnish.
If you’re handling large volumes of data that are processed by potentially slow clients, you can also increase the send_timeout value.
Backend timeouts#
For requests that cannot be served from the cache, a backend connection is made to the origin server, which acts as the source of truth.
If your backend is slow, or the connection is unreliable, backend connections might be left open for too long. It is also possible that the connection is closed while data is still being sent.
In order to strike the right balance, Varnish offers a set of backend timeouts:
connect_timeout: the amount of time Varnish is willing to wait for the backend to accept the connectionfirst_byte_timeout: the timeout for receiving the first byte from the originbetween_bytes_timeout: the amount of time we are willing to wait in between receiving bytes from the backend
Here’s a quick reminder on how to configure this in VCL:
vcl 4.1;
backend default {
.host = "origin.example.com";
.port = "80";
.connect_timeout = "10s";
.first_byte_timeout = "90s";
.between_bytes_timeout = "5s";
}
These settings are of course also available as varnishd runtime parameters, but it is important to know that the values in VCL are on a per-backend basis, and take precedence over the runtime parameters.
These parameters can also be specified on a per-request basis, using bereq.connect_timeout or bereq.first_byte_timeout from VCL_backend_fetch in VCL.
The backend_idle_timeout parameter is not configurable in VCL, defaults to 60 seconds , and defines how long a backend connection can be idle before Varnish closes it.
Performance issues#
As mentioned in the beginning of this tutorial: the performance of Varnish can depend on the available server resources and traffic patterns. The configuration of runtime parameters can also play a part in this.
It is important to point out that the default values of the various runtime parameters were carefully considered. But that doesn’t mean they are right for your environment.
Degraded performance in Varnish usually takes place when Varnish doesn’t have enough cache storage, when Varnish doesn’t have enough workspace memory or when Varnish doesn’t have enough threads.
Not enough caching storage#
By default, Varnish allocates 256 MB of memory for caching objects. That’s not a lot, but it’s conservative and should work on systems that have less than 1GB of RAM.
If Varnish is the only service running on the server, the rule of thumb is to allocate 80% of the server’s available memory to Varnish. This percentage only accounts for the object storage. Memory that is used to process requests is not part of that calculation though.
Monitoring the object storage memory consumption in Varnish can be done through the varnishstat program that is part of your Varnish installation.
varnishstat displays continuously updated counters, which can be filtered by using the -f option.
Here’s a varnishstat example that only monitors Shared Memory Allocation counters:
sudo varnishstat -f "SMA.*.g_*" -1
Here’s the output:
SMA.s0.g_alloc 40 . Allocations outstanding
SMA.s0.g_bytes 41445760 . Bytes outstanding
SMA.s0.g_space 63411840 . Bytes available
SMA.Transient.g_alloc 23 . Allocations outstanding
SMA.Transient.g_bytes 7176 . Bytes outstanding
SMA.Transient.g_space 0 . Bytes available
We can conclude that this server still has 63411840 bytes of storage available and 41445760 bytes of storage are in use.
Because the storage engine was unnamed through the -s runtime parameter for varnishd, Varnish will name the first storage engine s0.
The Transient storage engine is a secondary storage engine that is not limited in size. It is used for shortlived objects and for streaming uncacheable data.
If you’re running out of memory and Varnish needs to store new objects, it will evict the least recently used (LRU) objects to free up space.
The MAIN.n_lru_nuked counter in varnishstat will increase when this happens. This is a clear warning that you’re running out of memory. This is not disastrous because we’re dealing with a cache: the content can always be fetched from the origin server.
Increasing the cache storage#
If it turns out that you haven’t allocated enough memory to Varnish, you can increase the cache storage by modifying the -s runtime parameter in your systemd unit file.
You can edit the runtime configuration through the following command:
sudo systemctl edit --full varnish
An editor will open and this is what you’ll see:
[Unit]
Description=Varnish Cache, a high-performance HTTP accelerator
After=network-online.target nss-lookup.target
[Service]
Type=forking
KillMode=process
# Maximum number of open files (for ulimit -n)
LimitNOFILE=131072
# Locked shared memory - should suffice to lock the shared memory log
# (varnishd -l argument)
# Default log size is 80MB vsl + 1M vsm + header -> 82MB
# unit is bytes
LimitMEMLOCK=85983232
# Enable this to avoid "fork failed" on reload.
TasksMax=infinity
# Maximum size of the corefile.
LimitCORE=infinity
ExecStart=/usr/sbin/varnishd \
-a :80 \
-a localhost:8443,PROXY \
-p feature=+http2 \
-f /etc/varnish/default.vcl \
-s malloc,256m
ExecReload=/usr/sbin/varnishreload
[Install]
WantedBy=multi-user.target
