feat(kv): watch_prefixes — single multi-filter consumer (v0.3.2)#4
Merged
Conversation
KvWatcher::watch_prefixes(&[prefix]) watches the union of several prefixes through ONE NATS consumer, using server 2.10 multi-filter consumers (async-nats watch_many -> OrderedConfig.filter_subjects), instead of one consumer per prefix. This matters because JetStream consumers are a per-stream resource. Measured on a single stream: ~tens of KB of server RSS per consumer, growing super-linearly past a few thousand (1.5 -> 22 -> 33 -> 56 KB/consumer at 200/600/1200/2400). A node that watches K prefixes must therefore cost 1 consumer, not K — at fleet scale K-per-node is multiple TB of consumer state on one stream leader; 1-per-node is the only shape that fits. Proven by an integration test against a real nats-server (watch_prefixes_unions_on_a_single_consumer): the KV stream carries exactly one consumer, both watched prefixes are delivered, and a third prefix is filtered server-side (never delivered). Empty prefixes watch nothing (NOT the whole bucket). Trait default is required; all impls updated. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds
KvWatcher::watch_prefixes(&[prefix]): watch the union of several prefixes through ONE NATS consumer (server 2.10 multi-filter consumers, via async-natswatch_many→OrderedConfig.filter_subjects), instead of one consumer per prefix. Bumps to 0.3.2.Why (measured)
JetStream consumers are a per-stream resource. Measured server RSS per durable consumer on a single stream — and it's super-linear:
A node scoped to K prefixes must cost 1 consumer, not K. At fleet scale (e.g. 500k nodes × ~50 vpcs), K-per-node ≈ 25M consumers ≈ >1 TB of consumer state on one stream leader (impossible); 1-per-node is the only shape that fits.
Proof (integration test, real nats-server)
watch_prefixes_unions_on_a_single_consumer:consumer_count == 1),Empty prefixes watch nothing (explicitly guarded — an empty filter set would otherwise watch the whole bucket).
Notes
watch_prefixesis a required trait method;NatsKvWatcher(real), theappliedmocks, and the bench mock all implement it.server_2_10(a default feature).cargo testgreen;clippy --all-targets --features fjall -D warningsclean.🤖 Generated with Claude Code