Troubleshooting Varnish·续3
Please change -s malloc,256m with a value that suits your needs. Once you’re done, save the file and close the editor.
Please restart Varnish to enable these new settings by running the following command:
sudo systemctl restart varnish
Not enough workspace memory#
Workspace memory is a Varnish-specific concept. This type of memory is used for request and response parsing, for VCL storage, and also for any VMOD requiring memory space to store data.
There are different kinds of workspaces, and each of them can be tuned. When a workspace overflow occurs, this means the transactions couldn’t allocate enough memory to perform their tasks.
- The
workspace_clientparameter, with a default value of 64 KB , is used to limit memory allocation for HTTP request handling. - The
workspace_backendparameter, which also has a default value of 64 KB , sets the amount of memory that can be used during backend processing. - The
workspace_sessionparameter limits the size of workspace memory used to establish the TCP connection to Varnish. The default value is 0.5 KB.
When a task consumes more memory than allowed in one of the specific workspace contexts, the transaction is aborted, and an HTTP 503 response is returned. When a workspace_session overflow occurs, the connection will be closed.
It is always possible to increase the size of the various workspaces. Memory consumption depends on what happens in VCL , but also depends on the input Varnish receives from clients and backends.
A better solution is to optimize your VCL , or reduce the size and the amount of headers that are sent by the backend. But sometimes, you have no control over this, or no way to significantly reduce memory consumption. In that case, increasing workspace_client or workspace_backend is your best move.
Luckily there are ways to monitor workspace overflow. These workspaces have a varnishstat overflow counter:
MAIN.ws_client_overflowMAIN.ws_backend_overflowMAIN.ws_session_overflow
When these counters start increasing, don’t blindly increase the workspace size. Instead, have a look at your logs, see which transactions cause the overflow, and try to figure out if you can optimize that part of your VCL to avoid the overflows in the future.
As always, varnishstat and varnishlog will be the tools you need to figure out what is going on before deciding to increase the size of the workspaces.
These runtime parameters can be set by adding -p options to the varnishd program. This happens in the system unit file. The procedure involves sudo systemctl edit --full varnish to open the unit file and sudo systemctl restart varnish to restart Varnish.
Not enough threads#
The threading settings are the best way to tune the concurrency of Varnish. The thread_pool_min and thread_pool_max runtime parameters control how many threads are in the thread pool.
Instead of creating threads on demand, a couple of thread pools are initialized that contain a certain number of threads that are ready to use. Creating and destroying threads can create overhead and cause a slight delay.
By default there are two thread pools, which can be changed by modifying the thread_pools parameter. But benchmarks have shown no significant improvement by changing the value.
When varnishd starts, 200 threads are created in advance: 100 threads per thread pool. The thread_pool_min parameter can be used to tune this number.
Growing the thread pools#
When the thread pools are running out of available threads, Varnish will grow the pools until thread_pool_max is reached. Growing the pools is also somewhat resource intensive.
By increasing thread_pool_min, it’s easier to cope with an onslaught of incoming traffic upon restart. This is common when Varnish sits behind a load balancer and is suddenly added to the rotation.
On the other hand, starting varnishd with too many threads will have an impact on the resource consumption of your server.
A good indicator is the MAIN.threads counter in varnishstat. It lets you know how many threads are currently active. You can correlate this number to your resource usage, and it helps you establish a baseline.
The thread_pool_min parameter should at least be the number of threads in use for an average traffic pattern.
When the MAIN.threads_limited counter increases, it means the thread pools ran out of available threads. If there is room to grow the pools, Varnish will add threads. If not, tasks will be queued until a thread is available.
The MAIN.threads_limited counter might increase early on when the MAIN.threads counter reaches the thread_pool_min threshold. As the pools are grown, it won’t occur that much. But when MAIN.threads reaches the thread_pool_max value, the change rate of the MAIN.threads_limited counter can indicate problematic behavior.
The thread queue doesn’t have an infinite size: each thread pool has a queue limit of 20 tasks. This is configurable via the thread_queue_limit parameter. When the queue is full, any new task or request will be dropped.
So when the MAIN.threads_limited counter increases, and the MAIN.sess_dropped or MAIN.req_dropped counters are increasing as well, know that the queue is full and sessions/streams are being dropped.
The MAIN.sess_dropped counter refers to HTTP/1.1 sessions being dropped, whereas MAIN.req_dropped refers to HTTP/2 streams being dropped.
You can choose to increase the thread_queue_limit, which will allow more tasks to be queued. Unless resources are too tight, you really want to increase thread_pool_min because it will make your system more responsive.
Shrinking the thread pools#
When a thread has been idle for 300 seconds, Varnish will clean it up. This is controlled by the thread_pool_timeout parameter. The MAIN.threads counter will reflect this.
This means Varnish will automatically shrink the thread pools based on demand, but with a delay. But if increased server load, caused by varnishd worker threads, is too much for your system, you should decrease thread_pool_max to an acceptable level.
If you believe Varnish needs to clean up idle threads quicker, you can reduce the thread_pool_timeout. But remember: destroying threads also consumes resources.
Tuning the worker stack size#
Another factor that will impact server load is the worker stack size. This is stack space that is consumed by every worker thread. By limiting the stack size, we manage to reduce the memory footprint of Varnish on the system. The size is configurable via the thread_pool_stack parameter. The default stack size in Varnish is 48 KB. The default process stack size on Linux is typically multiple orders of magnitude larger than the stack sizes we use in Varnish.
Stack space is typically consumed by third-party libraries that are used by Varnish. libpcre, the library to run Perl Compatible Regular Expressions , can consume quite a bit of stack space. If you write very complicated regular expressions in VCL, you might even cause a stack overflow.
When a stack overflow happens, you should increase the value of thread_pool_stack. But this, in its turn, will have a direct impact on resource consumption because the worker stack size is per thread.
If you set your worker stack size to 100 KB and you have 5000 threads in two thread pools, this will consume almost 1 GB of memory. So be careful, and consider reducing thread_pool_max when this would be too taxing on your system.
HTTP limits#
HTTP requests and responses are parsed by Varnish. As mentioned earlier, parsing them requires a bit of workspace memory.
Incoming requests and cached responses are parsed in the client context, and use client workspace memory. When a cache miss takes place, and the response needs to be parsed from the origin server, we operate in the backend context. This will consume backend workspace memory.
There are certain limits in place that prevent Varnish from having to waste too much memory on request and response parsing and to avoid DoS attacks.
Here’s an overview of runtime parameters that limit the length and size of requests and responses:
http_max_hdr: the maximum number of headers an HTTP request or response may contain. The default value is 64.http_req_hdr_len: the maximum size of an individual request header. By default this is 8 KB.http_req_size: the maximum total size of the HTTP request headers. This defaults to 32 KB.http_resp_hdr_len: the maximum size of an individual response header. By default this is 8 KB.http_resp_size: the maximum total size of the HTTP response headers. This defaults to 32 KB.
When requests or responses exceed these limits, the transaction will fail.
HTTP request limit examples#
Here’s some example logging output when the http_max_hdr threshold is exceeded:
* << Request >> 5
- Begin req 4 rxreq
- Timestamp Start: 1611051232.286266 0.000000 0.000000
- Timestamp Req: 1611051232.286266 0.000000 0.000000
- BogoHeader Too many headers: foo:bar
- HttpGarbage "GET%00"
- RespProtocol HTTP/1.1
- RespStatus 400
- RespReason Bad Request
- ReqAcct 519 0 519 28 0 28
- End
As you can see, an HTTP 400 status code is returned when this happens.
Here’s an example where an individual request header exceeds the http_req_hdr_len limit:
* << Request >> 98314
- Begin req 98313 rxreq
- Timestamp Start: 1611051653.320914 0.000000 0.000000
- Timestamp Req: 1611051653.320914 0.000000 0.000000
- BogoHeader Header too long: test:YnEJyVqxTMgn7aX
- HttpGarbage "HEAD%00"
- RespProtocol HTTP/1.1
- RespStatus 400
- RespReason Bad Request
- ReqAcct 10081 0 10081 28 0 28
- End
When the total request size exceeds http_req_size, the following output can be found in your VSL :
* << Session >> 32793
- Begin sess 0 HTTP/1
- SessOpen 172.21.0.1 60576 http 172.21.0.3 80 1611052643.429084 30
- SessClose RX_OVERFLOW 0.001
- End
HTTP response limit examples#
When the origin server returns too many headers and exceeds the http_max_hdr limit, this doesn’t result in an HTTP 400 status, but in an actual HTTP 503.
You might see the following output appear in your VSL :
-- BogoHeader Too many headers:foo: bar
-- HttpGarbage "HTTP/1.1%00"
-- BerespStatus 503
-- BerespReason Service Unavailable
-- FetchError http format error
And when this happens, the MAIN.losthdr counter in varnishstat will also increase.
When the http_resp_hdr_len limit is exceeded, you will see the following output end up in VSL :
-- BogoHeader Header too long: Last-Modified: Tue,
-- HttpGarbage "HTTP/1.1%00"
-- BerespStatus 503
-- BerespReason Service Unavailable
-- FetchError http format error
And finally, when the http_resp_size limit is exceeded, the following VSL line may serve as an indicator:
-- FetchError overflow
Get help#
Troubleshooting can be hard sometimes. Although this detailed troubleshooting tutorial can get you a long way, you might need some help figuring out what’s going on.
Luckily there are ways to get help: help from the community and help from Varnish Software’s support department.
