Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Compressing on a thread pool

By default the module compresses in the worker’s event loop, as tokers/zstd-nginx-module does. While one response is being compressed, every other connection of that worker waits. At low levels and for short responses that is cheap; at levels 15–19, or for responses of a megabyte and more, it is not.

One directive moves compression onto an NGINX thread pool:

zstd_thread_pool default;

NGINX must be built with --with-threads (nginx.org’s packages and most distributions’ are).

How a response is compressed

  1. The response is cut into segments of zstd_parallel_segment_size (1m by default).
  2. Up to zstd_parallel_tasks segments (4) of one response are compressed at once, each by one of the pool’s threads.
  3. Compressed segments are sent in order as they complete, while the next segments are read. When all tasks are busy or waiting to be sent, NGINX stops reading the response until the first of them has been sent.

A response that fits in one segment is compressed in one piece by one thread. Each segment is compressed with only a limited overlap of the input before it, so a response longer than one segment compresses slightly worse, and a flush (for example with proxy_buffering off) ends the current segment early. The response is still one standard zstd frame, which any decoder reads.

Choose a pool

The pool named default exists without being declared: 32 threads per worker process, shared with aio threads. To size compression on its own, declare a pool in the main context and name it:

thread_pool zstd threads=4;

http {
    zstd_thread_pool zstd;
}

Every worker process has its own threads, so worker_processes times the pool’s threads run at once. A pool with as many threads as the worker has CPUs to spare is a good start.

When to leave it off

Turn it off, per location, where compressing in the event loop is cheaper than handing work to a thread:

location /api/ {
    proxy_pass http://127.0.0.1:8080;
    zstd_thread_pool off;
}
  • Short responses at low levels. In the benchmarks, small.json (1.9 KB) at level 3 is served faster from the event loop. At level 9 and above even short responses gain from the pool.
  • Responses compressed with a dictionary. They are always compressed in the event loop: the parallel compressor does not take dictionaries.

Memory

A response being compressed holds up to zstd_parallel_tasks + 1 segments of input, their compressed output, and an encoder for each segment being compressed. At high levels on long responses this is the largest cost of the pool: in the benchmarks one worker serving libraries.json (2.2 MB) at level 15 peaks at 523 MiB with 1 task and 1,119 MiB with 4.

To bound it:

  • lower zstd_parallel_tasks, so fewer segments of one response are in flight;
  • lower zstd_parallel_segment_size, so each segment and its encoder are smaller (at the cost of ratio);
  • set zstd_parallel_compressors, which limits how many compressors each worker keeps segment encoders for;
  • pick a zstd_retention_policy other than aggressive.

See Memory and encoder reuse for how idle compressors are kept.

Tuning summary

GoalChange
Better ratio on long responsesRaise zstd_parallel_segment_size (up to 16m)
More parallelism per responseRaise zstd_parallel_tasks, and give the pool the threads
Less memory per responseLower zstd_parallel_tasks or zstd_parallel_segment_size
Less memory kept between responsesLower zstd_parallel_compressors or use a stricter zstd_retention_policy
Isolate compression from aio threadsDeclare a pool of its own with thread_pool

How it works describes how segments are scheduled inside NGINX.