Skip to content

feat: support zstd HTTP response compression between Druid services - #20945

Merged
FrankChen021 merged 8 commits into
apache:masterfrom
FrankChen021:zstd-http-compression
Dec 3, 2026
Merged

FrankChen021 merged 8 commits into
apache:masterfrom
FrankChen021:zstd-http-compression

Conversation

@FrankChen021

@FrankChen021 FrankChen021 commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Fixes #20918.

Description

With metadataSegmentCacheEnable on, every Broker downloads the full used-segment list from the Coordinator every minute. With ~77k segments that is ~137 MiB of JSON, and gzip at level 6 took ~41% of Coordinator CPU. zstd at level 1 compresses ~6× faster and produces ~27% smaller output.

Server: Jetty CompressionHandler with zstd and gzip

  • Replace the deprecated Jetty GzipHandler with Jetty 12.1's CompressionHandler, registering zstd and gzip. Among the encodings the client accepts, the server uses the first one in druid.server.http.compression.codecs.
  • zstd is opt-in: codecs defaults to ["gzip"], because zstd uses much more native memory per response in flight than gzip, on both the server and the client (see the memory table below). zstd is registered only when it is in the list, so the default does not load the zstd native library on the server.
  • NormalizeAcceptEncodingHandler works around two Jetty negotiation bugs: a * entry could pick a codec the client declined with q=0, and the gzip exclusion was case-sensitive. The handler normalizes Accept-Encoding before Jetty negotiates and restores the original header for servlets.
  • gzip is always registered so gzip request bodies are still decompressed, as before. Request bodies are decompressed for every method, as the old handler did.
  • Add jetty-compression-server and jetty-compression-zstandard. zstd-jni is excluded from jetty-compression-zstandard, so Druid keeps a single managed version of the native library, as parquet-extensions does.

Client: Netty HTTP client asks for zstd

  • Druid's internal HTTP client sends Accept-Encoding: zstd, gzip;q=0.5 by default, so enabling zstd on a server is enough to use it. This does not depend on the server's codec list. If the zstd native library cannot be loaded, the client asks for gzip only.
  • zstd responses are decoded by Netty's HttpContentDecompressor, which supports zstd when zstd-jni is on the classpath, so no custom decoder is needed.
  • druid.global.http.compressionCodec / druid.broker.http.compressionCodec still ask for a single codec. They are now parsed as HttpClientConfig.CompressionCodec (case-insensitive), so an invalid value fails at startup with a message listing the supported codecs (identity, gzip, zstd). The deflate codec is removed: it was undocumented, and no Druid server ever sent deflate responses, so it behaved like identity.

Configuration

Property Default Description
druid.server.http.compression.codecs ["gzip"] Response encodings, most preferred first. ["zstd", "gzip"] enables zstd; [] disables response compression. Duplicates are ignored; identity is rejected.
druid.server.http.compression.zstd.level 1 1 to 19
druid.server.http.compression.gzip.level -1 (level 6) 0 to 9. 0 means no compression. -1 means the zlib default, which is level 6
druid.server.http.compression.gzip.inflateBufferSize 4096 gzip request decoder buffer size; 0 disables request decompression for all encodings, as inflateBufferSize=0 did before
druid.server.http.compressionLevel not set Deprecated alias of compression.gzip.level; logs a warning
druid.server.http.inflateBufferSize not set Deprecated alias of compression.gzip.inflateBufferSize; logs a warning

The deprecated properties apply only when the new ones are not set. Level validation lives in the HttpServerCompression.Zstd and HttpServerCompression.Gzip records.

Memory

Native memory per response in flight, measured with zstd-jni 1.5.7-17 and Netty 4.2.19, for a response larger than the zstd window (512 KB at level 1, 8 MB at level 19). It is allocated by zstd's C code, outside the heap and MaxDirectMemorySize.

Side gzip (level 6) zstd level 1 zstd level 3 zstd level 9 zstd level 19
Server (encoder) 250 KB 0.9 MB 3 MB 15 MB 90 MB
Client (decoder) 40 KB 0.8 MB 2.3 MB 4.5 MB 8.5 MB

A Broker decodes one response per data-server request in flight, so zstd on Historicals could use several GB on a large cluster. The docs recommend enabling zstd only where it pays off, such as on the Coordinator for Broker segment polling.

Compatibility

Encoding is negotiated per request, so a rolling upgrade works in any order. zstd is used only between a new client and a new server with zstd enabled; every other combination uses gzip.

Other behavior changes:

  • Jetty does not compress responses under 32 bytes (gzip) or 48 bytes (zstd) when their length is known.
  • Jetty-based HTTP clients also pick up the zstd decoder through ServiceLoader. The proxy servlets clear decoders, so proxying is unchanged.
  • Netty's zstd decoder, like its gzip decoder, does not fail a response whose compressed body ends in the middle of a frame.

Scope

zstd is used for traffic sent through Druid's internal HTTP client without its own Accept-Encoding, when the server runs this change with zstd enabled in druid.server.http.compression.codecs. Examples: Broker → Historical/Peon query responses, Broker → Coordinator segment polling, and Coordinator → Historical/Peon inventory sync and load/drop.

Unchanged:

  • MSQ worker/controller channels, which send Accept-Encoding: identity.
  • Proxied requests (Router, Coordinator → Overlord), which forward the original client's header.
  • Services with a compressionCodec override.
  • Servers that are older or have zstd disabled.
  • Request bodies, which Druid's client does not compress.

Results on a test cluster

Measured on the Coordinator after upgrading the Coordinators and Brokers and enabling zstd on the Coordinators, so Broker segment polls use zstd:

Metric Before (gzip) After (zstd)
CPU usage 11% 5%
Max HTTP response time 2.5 s 1.1 s
Avg HTTP response time 300 ms 200 ms
HTTP response body throughput 1.7 MB/s 1.33 MB/s

Release note

Druid services can now compress HTTP responses with zstd. It is not enabled by default; set druid.server.http.compression.codecs to ["zstd", "gzip"] on a service to use zstd for clients that accept it, such as other Druid services. For example, enabling it on the Coordinator reduces the CPU spent compressing the segment lists that Brokers poll.

  • Deprecated: druid.server.http.compressionLevel and druid.server.http.inflateBufferSize. Use druid.server.http.compression.gzip.level and druid.server.http.compression.gzip.inflateBufferSize instead.
  • Druid's internal HTTP client now sends Accept-Encoding: zstd, gzip;q=0.5 by default and loads the zstd native library at startup to do so. If the library cannot be loaded, it asks for gzip only.
  • Upgrade note: druid.global.http.compressionCodec and druid.broker.http.compressionCodec now accept only identity, gzip and zstd. A value of deflate now fails at startup; it never produced deflate responses, so replace it with identity to keep the same behavior.
  • Upgrade note: requests sending Accept-Encoding with identity;q=0 and no supported codec now get 415 instead of an uncompressed 200.
  • Known issue: when a client closes the connection during a compressed response, the response's compressor is released at the next garbage collection instead of immediately. This is a Jetty bug, tracked in EncoderSink.release() is not called when the client closes the connection during a compressed servlet response jetty/jetty.project#16064 and fixed by Make sure EncoderSink.release() is called when the client closes the connection during a compressed servlet response jetty/jetty.project#16196, which a later Jetty upgrade will pick up.
  • For extension developers: ServerConfig's constructor now takes an HttpServerCompression instead of inflateBufferSize and compressionLevel, and JettyServerInitUtils.wrapWithDefaultGzipHandler is replaced by wrapWithCompressionHandler. HttpClientConfig.DEFAULT_COMPRESSION_CODEC is removed, and HttpClientConfig.getCompressionCodec() returns null when no codec is set, in which case the client uses its default Accept-Encoding.

Key changed/added classes in this PR
  • HttpServerCompression
  • ServerConfig
  • JettyServerInitUtils
  • NormalizeAcceptEncodingHandler
  • HttpClientConfig
  • NettyHttpClient
  • DruidHttpClientConfig
  • HttpClientModule

This PR has:

  • been self-reviewed.
  • added documentation for new or modified features or behaviors.
  • a release note entry in the PR description.
  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
  • added or updated version, license, or notice information in licenses.yaml
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • been tested in a test Druid cluster.

Replace the deprecated Jetty GzipHandler with Jetty's CompressionHandler,
supporting zstd and gzip responses. Among the encodings a client accepts, the
server uses the first one in druid.server.http.compression.codecs (default
["zstd", "gzip"]; [] disables response compression). Accept-Encoding is
normalized before Jetty negotiates, so codec names match ignoring case and a
"*" entry never selects a codec the client declined.

Druid's internal HTTP client now sends "Accept-Encoding: zstd, gzip;q=0.5" by
default, so services that both run this change use zstd, and fall back to gzip
with older services. zstd responses are decoded by Netty's
HttpContentDecompressor, which supports zstd when zstd-jni is on the classpath.
druid.global.http.compressionCodec and druid.broker.http.compressionCodec still
ask for a single codec, and are now validated at startup. deflate is no longer
accepted, since Druid servers never sent deflate responses.

gzip settings move under druid.server.http.compression.gzip. The old
druid.server.http.compressionLevel and druid.server.http.inflateBufferSize
properties still apply when the new ones are not set, and log a deprecation
warning. inflateBufferSize=0 still disables request decompression.

@FrankChen021 FrankChen021 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Preserve method-independent request decompression before merging. The new allow-list omits GET, so gzip-compressed GET bodies can reach existing servlets still compressed, causing a backward-compatibility regression.

Reviewed 32 of 32 changed files; findings in 1 file.

Severity Findings
P0 0
P1 0
P2 1
P3 0
Total 1

This is an automated review by Codex GPT-5.6-Luna(max)

After addressing the findings or replying to the comments, you can request another review from me to trigger a new automated review.

@FrankChen021

Copy link
Copy Markdown
Member Author

@maytasm you can review the change if you're interested in this feature

@FrankChen021 FrankChen021 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Preserve method-independent request decompression for custom methods. The new allow-list omits methods outside Jetty's HttpMethod.values(), so an extension-defined method explicitly permitted by druid.server.http.allowedHttpMethods can reach its servlet with a gzip/zstd request body still compressed.

Reviewed 32 of 32 changed files; findings in 1 file.

Validation: git diff --check passed. No tests or builds were run.

Severity Findings
P0 0
P1 0
P2 1
P3 0
Total 1

This is an automated review by Codex GPT-5.6-Luna(max)

After addressing the findings or replying to the comments, you can request another review from me to trigger a new automated review.

@FrankChen021 FrankChen021 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No actionable issues found in this review. The prior request-decompression gap for extension-defined methods is resolved in the current head: configured druid.server.http.allowedHttpMethods entries are now included alongside Jetty-known methods at every production compression-handler call site.

Reviewed 32 of 32 changed files (full PR scope); the 11-file incremental delta was reviewed first.

Validation: git diff --check passed for both the full and incremental ranges. No tests or builds were run.


This is an automated review by Codex GPT-5.6-Luna(max)

@FrankChen021 FrankChen021 added the Approval Recommended Used when AI completes a review and reports no findings label Nov 23, 2026
),
config.getReadTimeout(),
config.getCompressionCodec()
toAcceptEncoding(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: If zstd-jni can't load in this JVM, this still sends Accept-Encoding: zstd, gzip;q=0.5. Any peer that has zstd enabled will then answer with zstd, and Netty passes those bytes through undecoded. The documented workaround, codecs=["gzip"], only changes what this process's server sends, not what its client asks for (i.e. scenario where a client that can't decode zstd calling a server that offers it). Suggest offering zstd only when Zstd.isAvailable().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 797dfc0. If the zstd native library cannot be loaded, the client's default Accept-Encoding drops zstd and asks for gzip only, and logs a warning once (HttpClientInit.defaultCompressionCodecs). An explicitly configured compressionCodec=zstd is left as is.

if (compressionConfig.getCodecs().contains(CompressionCodec.ZSTD)) {
final ZstandardCompression zstd = new ZstandardCompression();
final ZstandardEncoderConfig zstdEncoderConfig = new ZstandardEncoderConfig();
zstdEncoderConfig.setCompressionLevel(compressionConfig.getZstd().level());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zstd costs much more memory per in-flight response than gzip, mostly on the client side. This isn't documented.

I measured RSS per open stream with zstd-jni 1.5.7-17. The decoder figures are the client side, where Netty's ZstdDecoder uses ZstdInputStreamNoFinalizer.

gzip zstd level 1 zstd level 19
Decoder (client) 42 KB 0.4 MB after 64 KB read, 1.0 MB once past the 512 KB window 5.3 MB after 4 MB read (8 MB window)
Encoder (server) 233 KB (level 6) 0.9 MB ≥ 25 MB

Each zstd decoder also allocates a 128 KB byte[] on the heap when it's created, even for tiny responses.

A Broker holds one decoder for every data-server response in flight, up to numConnections × servers. With the default 20 connections and 400 Historicals, that's up to about 8 GB at level 1, compared with about 340 MB with gzip. The memory is native, outside the heap and outside MaxDirectMemorySize, so JVM limits don't cover it. A Broker whose container is sized to heap plus direct memory can get OOM-killed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document this. (and that the memory per in-flight response on zstd.level)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for measuring this. I re-measured with zstd-jni 1.5.7-17 and Netty 4.2.19, counting only native malloc memory across many concurrent streams, using the same encoder setup as Jetty and Netty's ZstdDecoder:

Side gzip (level 6) zstd level 1 zstd level 3 zstd level 9 zstd level 19
Server (encoder) 250 KB 0.9 MB 3 MB 15 MB 90 MB
Client (decoder) 40 KB 0.8 MB 2.3 MB 4.5 MB 8.5 MB

Level 1 matches your numbers. Level 19 is higher than your table: its match tables take about 80 MB up front, and the decoder grows to about 8.5 MB once the 8 MB window fills. Decoder memory is the window plus about 0.3 MB.

Given this, zstd is now opt-in in 797dfc0: druid.server.http.compression.codecs defaults to ["gzip"]. The docs have a "Memory usage" section with this table, and a new "Enabling zstd" section that recommends enabling it only on the Coordinator for Broker segment polling. If it is enabled on data servers, they suggest druid.broker.http.compressionCodec=gzip on the Broker.

Comment thread docs/operations/http-compression.md Outdated
|`druid.server.http.compressionLevel`|The compression level. Value should be between [-1,9], -1 for default level, 0 for no compression.|-1 (default compression level)|
|`druid.server.http.inflateBufferSize`|The buffer size used by gzip decoder. Set to 0 to disable request decompression.|4096|
|`druid.server.http.compression.codecs`|Ordered list of response encodings the server may use, most preferred first. Supported values are `zstd` and `gzip`. Set to `["gzip"]` to use gzip only, as in earlier Druid versions. Set to `[]` to disable response compression.|`["zstd", "gzip"]`|
|`druid.server.http.compression.zstd.level`|zstd compression level, between 1 and 19. Higher levels produce smaller responses but use more CPU.|1|

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please mention memory here too: a higher level raises memory per concurrent response on both the server and the client (see the comment on JettyServerInitUtils).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 797dfc0. The zstd.level row now says that higher levels also use more memory per in-flight response on both the server and the client, and links to the "Memory usage" section.

* {@link CompressionHandler} and the handler it wraps, then restores the header the client sent, so that servlets,
* including the proxy servlets that forward the header, see it unchanged. Use {@link #wrap} to set up both.
*
* This class can be removed once Jetty fixes these bugs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have the link or ticket to Jetty bug? Would be good to link it here otherwise it's hard to keep track

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a Jetty issue for either bug, so there is nothing to link yet. To keep track of it, 797dfc0 adds testJettyStillHasNegotiationBugs, which runs Jetty's CompressionHandler without the workaround and asserts that both bugs still occur. When a Jetty upgrade fixes either one, the test fails with a message to consider removing NormalizeAcceptEncodingHandler.

@maytasm maytasm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@FrankChen021 FrankChen021 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No actionable issues found in this review. The final changes keep server-side zstd opt-in, gate the default client advertisement on native zstd availability, and preserve HTTP negotiation and legacy configuration behavior.

Reviewed 32 of 32 changed files; the 9-file incremental delta was reviewed first.


This is an automated review by Codex GPT-5.6-Luna(max)

@FrankChen021 FrankChen021 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No actionable issues found in this review. The final head preserves zstd opt-in, native-library fallback to gzip, legacy configuration behavior, allowed-method request decompression, and the managed zstd-jni dependency.

Reviewed 32 of 32 changed files; the incremental delta was reviewed first, followed by the full PR scope.

Validation: git diff --check passed. No tests or builds were run.


This is an automated review by Codex GPT-5.6-Luna(max)

@amaechler amaechler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A handful of bot findings to consider.

…ng, and keep a single client codec

- Exclude zstd from request decompression, since Jetty's zstd decoder has no window limit.
  NormalizeAcceptEncodingHandler lowercases Content-Encoding so that the exclusion also applies to ZSTD.
- Ignore a malformed Accept-Encoding header and respond uncompressed, as the old GzipHandler did, instead of 500.
- Restore HttpClientConfig's single nullable compressionCodec, and send a fixed default Accept-Encoding.

@FrankChen021 FrankChen021 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No actionable issues found. The incremental scope was reviewed first, followed by the complete diff and surrounding code; the zstd HTTP response-compression changes remain coherent across configuration, request/response handling, and tests.

Reviewed 32 of 32 changed files; incremental scope covered 9 files.

Validation: git diff --check passed for both the full and incremental ranges. No tests or builds were run.


This is an automated review by Codex GPT-5.6-Luna(max)

@amaechler

Copy link
Copy Markdown
Contributor

Thanks for 05a2f14, the zstd request-body exclusion with the Content-Encoding lowercasing, the malformed-header handling and the single client codec all look good to me.

Two points from the review summary are still open:

Deflater leak when a response is aborted (P2). Jetty 12.1's EncoderSink releases its encoder only on the last=true write, and CompressionResponse registers no failure listener. In a standalone test against Jetty 12.1.15 core handlers, 20 compressed responses reset mid-stream by the client left 11 Deflater pool entries permanently in use with CompressionHandler, against 0 with the old GzipHandler, which released them in GzipResponseAndCallback.failed. Broker cancellations abort Historical responses routinely, and gzip is the default, so every cluster picks this up on upgrade. It is capped at the pool capacity (1024 Deflaters, roughly 250 MB of zlib state) and Jetty falls back to unpooled Deflaters after that, so nothing starves. I did not test the ee8 HttpOutput path Druid uses. I don't see a clean way to release the encoder from Druid's side, since the sink is private to CompressionResponse, so I'd suggest: (1) confirm it on the ee8 path, for example by aborting a few streamed responses in HttpCompressionNegotiationTest and checking the pool's in-use count; (2) if it reproduces, file a Jetty issue and link it in the PR description, alongside the negotiation bugs NormalizeAcceptEncodingHandler works around. If the leak is real, I'd also mention it in the release note until Jetty fixes it.

Release note (P2). The "Release note" section still leaves out the changes users will notice. Suggested addition:

  • Druid's internal HTTP client now sends Accept-Encoding: zstd, gzip;q=0.5 by default and loads the zstd native library at startup to do so. If the library cannot be loaded, it asks for gzip only.
  • Upgrade note: druid.global.http.compressionCodec and druid.broker.http.compressionCodec now accept only identity, gzip and zstd. A value of deflate now fails at startup; it never produced deflate responses, so replace it with identity to keep the same behaviour.
  • Upgrade note: requests sending Accept-Encoding with identity;q=0 and no supported codec now get 415 instead of an uncompressed 200.
  • For extension developers: ServerConfig's constructor now takes an HttpServerCompression instead of inflateBufferSize and compressionLevel, and JettyServerInitUtils.wrapWithDefaultGzipHandler is replaced by wrapWithCompressionHandler.

@FrankChen021

Copy link
Copy Markdown
Member Author

@amaechler Thanks.

Deflater leak: confirmed on the ee8 path. In a test that aborted 20 streamed gzip responses, 13 Deflaters stayed in use, and after a GC all were reclaimed as leaked. This is already tracked in Jetty as jetty/jetty.project#16064, with a fix in jetty/jetty.project#16196 that a later Jetty upgrade will pick up. The release note lists it as a known issue until then.

Release note: added your suggested items, plus the HttpClientConfig changes for extension developers.

@amaechler amaechler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐻

@FrankChen021

Copy link
Copy Markdown
Member Author

@maytasm @amaechler Thanks for the review

@FrankChen021
FrankChen021 merged commit 44c2a4b into apache:master Dec 3, 2026
28 checks passed
@FrankChen021
FrankChen021 deleted the zstd-http-compression branch December 3, 2026 06:06
@github-actions github-actions Bot added this to the 40.0.0 milestone Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approval Recommended Used when AI completes a review and reports no findings Area - Dependencies Area - Documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: support zstd HTTP response compression between Druid services

3 participants