feat(kafka): broker-per-tenant multi-tenancy (dedicated cluster per tenant)#3315
Merged
Merged
Conversation
Kafka already supported named brokers; this adds broker-per-tenant so each Wolverine tenant is served by its own dedicated Kafka cluster while sharing the topic topology, routed at runtime by Envelope.TenantId. A Kafka "connection" is config-only (three Confluent config bags + builder callbacks), so mirroring the RabbitMQ tenant model each tenant owns a child KafkaTransport whose config is cloned from the parent and re-pointed at the tenant's own BootstrapServers (KafkaTenant.Compile). - New KafkaTenant + KafkaTransport.Tenants cache; ConnectAsync compiles each tenant and, under AutoProvision, provisions the shared topics + DLQ on every tenant cluster (each is an independent broker). - KafkaTopic/KafkaTopicGroup fan out to a TenantedSender (outbound) and a CompoundListener with one KafkaListener per tenant cluster stamping the tenant id via ReceiverWithRules(TenantIdRule) (inbound). DLQ is produced to the tenant's own cluster. - New public API on KafkaTransportExpression: TenantIdBehavior, AddTenant(id, bootstrapServers) and AddTenant(id, Action<KafkaTransportExpression>). - Tenant/default senders under TenantedSender use fire-and-forget InlineKafkaSender: TenantedSender is intentionally not ISenderRequiresCallback (GH-2361) and does not forward RegisterCallback, so a BatchedSender beneath it would silently drop messages. Consumer-group caveat: because each tenant is a separate cluster with its own offsets, tenant listeners keep the SAME group id — it is not suffixed per tenant. Per-tenant retry topics / replay remain bound to the default cluster (documented). Tests: 6 CI-safe unit tests (tenant compile / group-id-not-suffixed / TenantedSender resolution) + 3 Testcontainers integration tests (tenant vs default cluster routing, inbound tenant-id stamping). Full Kafka suite green (213 passed). Docs section + compiling snippet added to kafka.md. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
jeremydmiller
marked this pull request as ready for review
July 6, 2026 03:05
This was referenced Jul 9, 2026
Merged
This was referenced Jul 13, 2026
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.
Closes #3303
Kafka already supported named brokers; this PR adds the missing broker-per-tenant feature. Each Wolverine tenant is served by its own dedicated Kafka cluster while sharing the topic topology, routed at runtime by
Envelope.TenantId.Approach
A Kafka "connection" is config-only (three Confluent config bags + builder callbacks), so — mirroring the RabbitMQ tenant model — each tenant owns a child
KafkaTransportwhose config is cloned from the parent and re-pointed at the tenant's ownBootstrapServers(KafkaTenant.Compile). The child transport getsCreateProducer/Consumer/AdminClientfor free and inherits EOS / idempotence / static-membership / SASL/SSL config.Changes
KafkaTenant(new) +KafkaTransport.Tenantscache.ConnectAsynccompiles each tenant and, underAutoProvision, provisions the shared topics and the DLQ on every tenant cluster (each is an independent broker).KafkaTopic/KafkaTopicGroupfan out to a frameworkTenantedSender(outbound) and aCompoundListenerwith oneKafkaListenerper tenant cluster, stamping the tenant id inbound viaReceiverWithRules([new TenantIdRule(...)]). Dead-lettered messages are produced to the tenant's own cluster.KafkaTransportExpression:TenantIdBehavior(...),AddTenant(id, bootstrapServers),AddTenant(id, Action<KafkaTransportExpression>)(full auth/SASL/SSL, seeded from parent).TenantedSenderuse fire-and-forgetInlineKafkaSender.TenantedSenderis intentionally notISenderRequiresCallback(Message is stuck in outbox with multiple tenants and durable messaging #2361) and does not forwardRegisterCallback, so aBatchedSenderregistered beneath it would never get its callback and would silently drop messages — inline production is the correct fire-and-forget model (same as the RabbitMQ / NATS tenant senders).Consumer-group caveat
Because each tenant is a separate cluster with its own offsets, tenant listeners keep the same consumer
GroupId— it is deliberately not suffixed per tenant (that would be wrong here, unlike NATS subject prefixing). Per-tenant retry topics / replay remain bound to the default cluster and are documented as out of scope.Tests
KafkaPerTenantConfigurationTests, no broker): tenant compile clones config + re-points bootstrap servers, group id is not suffixed, parent config (SASL/idempotence/DLQ) inherited,Action-configured tenant seeded-from-parent-and-overridable, topics tenant-aware by default, a TenantAware endpoint resolves aTenantedSender.KafkaPerTenantConnectionTests,[Trait("Category","Integration")], second cluster viaTestcontainers.Kafka, skips if Docker/broker unavailable): aDeliveryOptions{TenantId="tenantB"}message lands on the tenant cluster and not the default; a default message lands on the shared cluster; a tenant message round-trips back stamped with itsTenantId.Flakyexcluded).dotnet build wolverine.slnx -c Releaseclean (0 warnings, 0 errors).Docs
New "Multi-Tenancy with a Broker per Tenant" section in
kafka.mdcontrasting named-broker (static) vs tenant-routed (runtimeDeliveryOptions.TenantId), with a compiling mdsnippets sample and the group-id / auto-provision caveats.🤖 Generated with Claude Code