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 google/ngx_brotli 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 9–11, or for responses of a megabyte and more, it is not.

One directive moves compression onto an NGINX thread pool:

brotli_thread_pool default;

NGINX must be built with --with-threads (nginx.org’s packages and most distributions’ are). In the benchmarks, one worker with 4 threads serves 3.5–4.4 times as many requests as google/ngx_brotli at levels 4–6 on 85 KB–2.2 MB responses, and 4.5–14 times as many at levels 9–11.

How a response is compressed

  1. The response is cut into segments of brotli_parallel_segment_size (1m by default).
  2. Up to brotli_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. A flush (for example with proxy_buffering off) ends the current segment early. The response is still one standard Brotli stream.

What it costs in ratio

LevelEach segment but the firstBack-references across segments
2–9Also runs through the input before it: a quarter of the segment size at most, never more than the window (256 KiB for 1m segments with the default 512k window)Yes, into that overlap
0, 1, 10, 11Starts from nothingNo

Either way, a response longer than one segment compresses worse than in one piece: on the 1 MB and 2.2 MB files of the benchmarks, by 0.1–0.6% at levels 4–9 and by 3.8–5.2% at level 11. At levels 4–9 the overlap also costs CPU time; at level 11, 1 MiB segments compress faster than a whole file, so the pool uses less CPU per response than the event loop.

Larger segments compress better and, at levels 2–9, run less input twice, but fewer of a response’s segments are compressed at once, and each encoder needs more memory.

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 brotli threads=4;

http {
    brotli_thread_pool brotli;
}

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;
    brotli_thread_pool off;
}

Short responses at low levels cost little CPU either way: in the benchmarks, small.json (1.9 KB) at level 4 takes 0.032 ms in the event loop and 0.051 ms on the pool.

Memory

A response being compressed holds up to brotli_parallel_tasks + 1 segments of input, their compressed output, and an encoder for each segment. 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 9 peaks at 563 MiB with 1 task and 1,089 MiB with 4.

To bound it:

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

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

Tuning summary

GoalChange
Better ratio on long responsesRaise brotli_parallel_segment_size (up to 16m)
More parallelism per responseRaise brotli_parallel_tasks, and give the pool the threads
Less memory per responseLower brotli_parallel_tasks or brotli_parallel_segment_size
Less memory kept between responsesLower brotli_parallel_compressors or use a stricter brotli_retention_policy
More reuse on a busy, large poolRaise brotli_parallel_compressors towards the pool’s threads, with brotli_idle_encoders
Isolate compression from aio threadsDeclare a pool of its own with thread_pool

How it works describes how segments are scheduled inside NGINX.