Skip to content

Migrate mqtt-kafka session ownership to Store SPI #1798

Closed

Description

@jfallows

Background

MqttKafkaSessionFactory currently coordinates MQTT session ownership using a custom Kafka consumer-group protocol ("highlander"). This issue replaces that mechanism with StoreHandler-based coordination and folds in the related cross-replica session-lookup, redirect-hint plumbing, and the #migrate compacted-topic takeover signal.

A previous release added an optional store field and a deprecation warning announcing this change. This issue completes the migration.

Proposal

Coordinate session ownership via the Store SPI using the existing StoreHandler surface (lock, renew, unlock, put, watch). No SPI changes are required.

Cooperative takeover protocol

  1. New owner candidate puts its ownership record (replica identity, acquisition timestamp, identity strength, optional externally-reachable address) on the session key.
  2. Current owner's watch fires with the new value.
  3. Current owner detects displacement (value owner != self), stops lease renewal, drains pending state writes, calls unlock.
  4. New candidate locks the session key — succeeds.
  5. New candidate writes its initial session state and begins serving.

Strong vs weak session ownership

The ownership record includes an identity strength classification:

  • Strong identity: this replica has an externally-addressable identity (serverRef configured). The address is published in the ownership record.
  • Weak identity: no externally-addressable address is configured. The ownership record carries only an opaque replica identity.

The strength classification drives whether a CONNECT arriving on a non-owner replica should redirect the client back to the owner or initiate a takeover.

CONNECT handling with strong/weak ownership

When CONNECT for clientId X arrives at replica B:

  1. B reads the current ownership record for X from the Store.
  2. If the current owner A has strong identity → B responds with MQTT 5 CONNACK reason SERVER_MOVED (0x9C) and Server Reference property pointing to A. No takeover claim is put. Client reconnects to A; session locality is preserved.
  3. If the current owner A has weak identity (or there is no current owner) → B proceeds with the cooperative takeover protocol above.

If a CONNECT later arrives directly at A for the same clientId (either via redirect from B, or directly via the load balancer), normal MQTT session-takeover semantics apply locally at A — A kicks any stale prior connection and accepts the new one.

If A becomes unreachable or hung, the lease TTL ensures the lock is released within bounded time, after which a subsequent CONNECT at B (or any other replica) acquires the lock via Case (3) above.

Failure recovery via TTL

The lease TTL bounds recovery when the cooperative path cannot complete:

  • Owner dead — ceases renewal → lease expires → new candidate locks.
  • Owner drain hung — watch handler disables renewal → lease expires → new candidate locks.

Renewal must be gated on continued ownership: the watch handler sets a flag that suppresses further renewals.

Replacement of the #migrate compacted-topic takeover signal

MqttKafkaSessionFactory currently writes a <clientId>#migrate record on mqtt-sessions with a sender-id header during session establishment. Other replicas serving the same clientId observe this via a merged-stream filter (excluding their own sender-id) and yield ownership. This is an alternate channel for the same takeover signal carried by the highlander group, using the compacted topic instead of a Kafka consumer group.

The Store-based cooperative takeover protocol replaces this mechanism directly:

#migrate mechanism Store-based replacement
New owner writes <clientId>#migrate record with own sender-id New owner puts ownership record on session key
Existing owner's merged-stream filter triggers Existing owner's watch fires
Sender-id filter distinguishes self from others value.owner != self check

After this issue, the #migrate write, the merged-stream filter on clientIdMigrate, the associated SENDER_ID exclude clause, the MIGRATE_KEY_POSTFIX constant, and the clientIdMigrate field are removed.

Configuration

stores:
  memory0:
    type: memory

bindings:
  mqtt-kafka0:
    type: mqtt-kafka
    kind: proxy
    options:
      ...
      store: memory0
      serverRef: mqtt-node-1.example.com:1883   # optional; presence makes this replica's identity strong

serverRef continues to be the externally-reachable address advertised by this replica. Its presence classifies this replica's ownership as strong; its absence classifies it as weak. The option no longer drives any Kafka consumer-coordinator plumbing — it is published directly in the Store ownership record.

New mqtt-kafka options for lease duration and renewal interval, with defaults sized for fast cooperative takeover and bounded TTL fallback.

In scope

  • Schema change: store becomes required on mqtt-kafka binding; validation fails if absent.
  • Implement cooperative takeover protocol in MqttKafkaSessionFactory using StoreHandler.
  • Watch-gated renewal lifecycle.
  • Configuration knobs for lease duration and renewal interval.
  • Ownership record schema includes replica identity, acquisition timestamp, identity strength, and (for strong identity) the externally-reachable address.
  • On CONNECT in MqttServerFactory / MqttKafkaSessionFactory, consult the Store for current owner; if owner has strong identity, respond with MQTT 5 CONNACK SERVER_MOVED + Server Reference rather than initiating takeover. If owner has weak identity (or no owner), proceed with cooperative takeover.
  • Replace the cross-replica session lookup (currently the mqtt-clients Kafka consumer group with consumerId=serverRef) with the ownership record contents from the Store.
  • Remove KafkaResetEx.consumerId → MqttResetEx.serverRef plumbing for session redirect; the redirect hint now flows via the Store watch event payload.
  • Remove the #migrate compacted-topic takeover signal: stop writing <clientId>#migrate records; remove the merged-stream filter on clientIdMigrate and the associated SENDER_ID exclude clause; remove the MIGRATE_KEY_POSTFIX constant and clientIdMigrate field.
  • Remove the highlander-group code path from MqttKafkaSessionFactory.
  • Remove "highlander" protocol references from KafkaClientGroupFactory if no other callers remain.
  • Tests: cooperative takeover, TTL-recovery failure modes, Clean Start=true displacement, CONNACK redirect when owner has strong identity, takeover when owner has weak identity, fallback to takeover after lease expiry when a strong-identity owner becomes unreachable.
  • Update existing k3po specs that exercise session takeover, server redirect, and #migrate signaling.
  • Documentation update.

Acceptance criteria

  • All existing MqttKafkaSessionFactory tests pass under the new ownership coordination.
  • New tests cover: cooperative takeover, TTL-recovery failure modes, Clean Start=true displacement, CONNACK SERVER_MOVED redirect for strong-identity owners, cooperative takeover for weak-identity owners, and takeover via TTL expiry when a strong-identity owner is unreachable.
  • The "highlander" protocol name is no longer referenced by binding-mqtt-kafka; the corresponding election code in MqttKafkaSessionFactory is removed.
  • The mqtt-clients Kafka consumer group is no longer created by the binding.
  • No <clientId>#migrate records are written by the binding; MIGRATE_KEY_POSTFIX and clientIdMigrate are removed from MqttKafkaSessionFactory.
  • Client redirect hint (when owner identity is strong) is sourced from the Store ownership record, not from KafkaResetEx.consumerId.
  • Configurations missing the store option fail validation with a clear error message.
  • Documentation updated for the new session ownership model, strong/weak identity classification, CONNACK redirect behavior, and configuration knobs.

References

  • runtime/binding-mqtt-kafka/src/main/java/io/aklivity/zilla/runtime/binding/mqtt/kafka/internal/stream/MqttKafkaSessionFactory.java
  • runtime/binding-mqtt/src/main/java/io/aklivity/zilla/runtime/binding/mqtt/internal/stream/MqttServerFactory.java
  • runtime/binding-kafka/src/main/java/io/aklivity/zilla/runtime/binding/kafka/internal/stream/KafkaClientGroupFactory.java
  • runtime/engine/src/main/java/io/aklivity/zilla/runtime/engine/store/StoreHandler.java
  • runtime/store-memory/src/main/java/io/aklivity/zilla/runtime/store/memory/internal/MemoryStoreHandler.java

Activity

  1. self-assigned this
    on May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions