Memory and encoder reuse
An encoder’s working memory (window, match finder and buffers) grows with the level and the window. google/ngx_brotli allocates it for every response and frees it when the response is complete. ngx_mbrotli keeps idle encoders in each worker, so the next response can reuse what a previous one allocated.
Three directives control this:
| Directive | Default | What it sets |
|---|---|---|
brotli_reuse_memory | on | Whether encoders are reused at all |
brotli_retention_policy | aggressive | How much memory an idle encoder keeps |
brotli_idle_encoders | 4 | How many idle encoders each worker keeps |
Peak memory still follows the number of responses being compressed at once,
times the working set that brotli_comp_level and brotli_window need. In the
benchmarks, a worker compressing in the event loop
peaks at most 4 MiB above google/ngx_brotli, and below it on
jquery.min.js at level 9 (8 MiB against 36 MiB).
Retention policies
| Policy | Kept between responses | Use when |
|---|---|---|
aggressive | Every buffer | Throughput matters most and workers may keep memory for their largest responses at level 11 with a 16m window |
current_config | Buffers that suit the current level and window | Locations use different levels or windows |
bounded <size> | Buffers while they fit in size | Memory per worker must stay below a known bound |
release_all | Nothing but the encoder itself | Memory matters more than allocation cost |
http {
brotli on;
# Keep at most 8 MB of buffers per idle encoder.
brotli_retention_policy bounded 8m;
location /downloads/ {
brotli_comp_level 11;
brotli_window 16m;
# Large, rare responses: do not keep their memory.
brotli_retention_policy release_all;
}
}
Idle encoders are reused only by locations with the same retention policy, so a
worker that serves locations with several policies may need a higher
brotli_idle_encoders. When a worker’s slots are full, the least recently used
encoder is freed.
Allocating per response
To allocate per response, as google/ngx_brotli does:
brotli_reuse_memory off;
Every response then gets a new encoder that is freed when the response is
complete. brotli_idle_encoders 0 has the same effect for the whole http
block.
With a thread pool
With brotli_thread_pool, the unit of reuse is a
compressor: an idle compressor keeps the encoders of up to
brotli_parallel_tasks segments, and the retention policy applies to all of
them together.
- A response holds its compressor until it has been sent, including while its segments wait for a free thread.
- Each worker keeps encoders for at most
brotli_parallel_compressorscompressors, idle or in use. Other responses allocate encoders for their segments and free them once they have been sent. - Idle compressors are kept separately from idle event-loop encoders, up to
brotli_idle_encodersof them.
A higher brotli_parallel_compressors lets more responses reuse encoders, and
costs the memory of their encoders: in single runs with a 4-thread pool and 32
connections on jquery.min.js at level 9, 32 served 8% more requests per
second than the default, and the worker peaked at 168 MiB instead of 91 MiB.