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

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:

DirectiveDefaultWhat it sets
zstd_reuse_memoryonWhether encoders are reused at all
zstd_retention_policyaggressiveHow much memory an idle encoder keeps
zstd_idle_encoders4How many idle encoders each worker keeps

Retention policies

PolicyKept between responsesUse when
aggressiveEvery bufferThroughput matters most and workers may keep memory for their largest responses at their highest level
current_configBuffers that suit the current level and windowLocations use different levels or windows
bounded <size>Buffers while they fit in sizeMemory per worker must stay below a known bound
release_allNothing but the encoder itselfMemory 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_compressors compressors, 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_encoders of them.
  • An idle compressor is reused only for the same level, window and segment size.