Memory and encoder reuse
An encoder’s working memory (match finder tables, window and buffers) grows with the level and the window. tokers/zstd-nginx-module allocates it for every response and frees it when the response is complete. ngx_mzstd 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 |
|---|---|---|
zstd_reuse_memory | on | Whether encoders are reused at all |
zstd_retention_policy | aggressive | How much memory an idle encoder keeps |
zstd_idle_encoders | 4 | How many idle encoders each worker keeps |
Retention policies
| Policy | Kept between responses | Use when |
|---|---|---|
aggressive | Every buffer | Throughput matters most and workers may keep memory for their largest responses at their highest level |
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 |
With aggressive, a worker allocates only what its largest responses grow
into, and keeps it. That is the fastest option. In the
benchmarks, a worker compressing in the event loop
with it peaks at about the memory of the C module on long responses, and at far
less on short ones at high levels (5 MiB against 85 MiB for small.json at
level 19), because the encoder sizes its tables for the declared response
length.
http {
zstd on;
# Keep at most 8 MB of buffers per idle encoder.
zstd_retention_policy bounded 8m;
location /downloads/ {
zstd_comp_level 19;
# Large, rare responses: do not keep their memory.
zstd_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
zstd_idle_encoders. When a worker’s slots are full, the least recently used
encoder is freed.
Allocating per response
To allocate per response, as tokers/zstd-nginx-module does:
zstd_reuse_memory off;
Every response then gets a new encoder that is freed when the response is
complete. zstd_idle_encoders 0 has the same effect for the whole http
block.
With a thread pool
With zstd_thread_pool, the unit of reuse is a compressor:
an idle compressor keeps the encoders its segments used, up to
zstd_parallel_tasks of them, 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
zstd_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
zstd_idle_encodersof them. - An idle compressor is reused only for the same level, window and segment size.