Skip to content

feat(kv): watch_prefixes — single multi-filter consumer (v0.3.2)#4

Merged
jaredLunde merged 1 commit into
mainfrom
jared/kv-watch-prefixes
Jun 10, 2026
Merged

feat(kv): watch_prefixes — single multi-filter consumer (v0.3.2)#4
jaredLunde merged 1 commit into
mainfrom
jared/kv-watch-prefixes

Conversation

@jaredLunde

Copy link
Copy Markdown
Contributor

What

Adds KvWatcher::watch_prefixes(&[prefix]): watch the union of several prefixes through ONE NATS consumer (server 2.10 multi-filter consumers, via async-nats watch_manyOrderedConfig.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:

consumers marginal cost
200 ~1.5 KB
600 ~22 KB
1200 ~33 KB
2400 ~56 KB

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:

  • the KV stream carries exactly one consumer (consumer_count == 1),
  • both watched prefixes are delivered,
  • a third prefix is filtered server-side (never delivered).

Empty prefixes watch nothing (explicitly guarded — an empty filter set would otherwise watch the whole bucket).

Notes

  • watch_prefixes is a required trait method; NatsKvWatcher (real), the applied mocks, and the bench mock all implement it.
  • Requires async-nats server_2_10 (a default feature).
  • cargo test green; clippy --all-targets --features fjall -D warnings clean.

🤖 Generated with Claude Code

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>
@jaredLunde jaredLunde merged commit 549a9c7 into main Jun 10, 2026
1 check passed
@jaredLunde jaredLunde deleted the jared/kv-watch-prefixes branch June 10, 2026 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant