Skip to content

Move MQTT QoS state into mqtt-sessions topic and consolidate session keys #1799

Closed

Description

@jfallows

Background

MqttKafkaPublishFactory and MqttKafkaSubscribeFactory currently persist MQTT QoS in-flight state via Kafka offset-commit metadata on per-clientId consumer-group identities (<groupIdPrefix>-<clientId>-session for publish-side QoS 2; <groupIdPrefix>-<clientId> for subscribe-side QoS 1+). The offset-commit metadata string carries in-flight QoS 1/2 packet IDs, QoS 2 idempotent producer state (producerId, producerEpoch, producerSequence), and per-partition consumed offsets.

This has two issues for long-lived MQTT sessions:

  1. Kafka offset retention deletes session state. Committed offsets are deleted after offsets.retention.minutes (default 7 days) once a group has no active members. Sessions with a session expiry interval longer than the retention window — or MQTT 3.1.1 Clean Session=false clients, which have no spec-defined expiry — can lose QoS state before the session is supposed to expire.
  2. Per-clientId Kafka consumer-group identities remain as offset-commit namespaces, contributing Kafka resource footprint proportional to MQTT client count.

Moving this state into the compacted mqtt-sessions topic — which already holds subscription, will, and expiry state — eliminates both. Compacted topic retention is "keep latest value per key, indefinitely." After this change, mqtt-kafka maintains zero Kafka consumer groups (combined with the coordination move in #1798).

Proposal

Store all per-session state, including QoS in-flight state, on the compacted mqtt-sessions topic under a fixed, bounded set of keys per session. Session state remains Kafka-native — no new store implementation and no StoreHandler SPI changes are required. (Ownership coordination is handled separately by #1798 via the Store SPI.)

Fixed key set per session

Key Holds Update frequency
<clientId> Subscriptions, session metadata (expiry config, flags) Low
<clientId>#offsets All QoS state as one blob: per-(topic, partition) consumed offset, in-flight QoS 1/2 packet IDs (publish + subscribe), QoS 2 idempotent producer state High
<clientId>#will Will message and delivery signal (renamed from #will-signal) Low
<clientId>#expiry Session-expiry signal (renamed from #expiry-signal) Low

The <clientId>#offsets blob is a single record holding a map of (topic, partitionId) → { consumedOffset, inFlightPacketIds, producerId, producerEpoch, producerSequence }. Each QoS 1/2 state change rewrites this single record. Each session is owned by a single worker on a single replica, so publish-side and subscribe-side updates to #offsets serialize naturally without concurrent-writer contention.

The blob is bounded in size: in-flight packet IDs are capped by the MQTT receive-maximum / in-flight limit, and partition count is bounded by topic configuration. QoS 0 traffic never touches it.

Key renames

#will-signal → #will and #expiry-signal → #expiry, performed in this same change since record keys on mqtt-sessions are already being modified. (#migrate is removed separately by #1798.)

Read/write changes

  • MqttKafkaPublishFactory.doCommitOffsetIncomplete: rewrite to update the <clientId>#offsets record instead of committing a Kafka offset.
  • MqttKafkaSubscribeFactory QoS 1+ offset persistence: rewrite to update the <clientId>#offsets record.
  • MqttKafkaSessionFactory session-resume: read the <clientId>#offsets record to restore offsets, in-flight packet IDs, and producer state, instead of issuing OffsetFetch against the per-clientId group.
  • Remove newOffsetFetchStream and newOffsetCommitStream paths and the per-clientId groupId construction in MqttKafkaSessionFactory.
  • Update #will-signal / #expiry-signal producers and consumers to use the new #will / #expiry key postfixes.

Cleanup

mqtt-sessions cleanup uses per-key tombstones (records with the same key and payload == null). With a fixed key set, cleanup is a known, bounded sequence — no enumeration or dynamic key discovery is required.

On session end (expiry signal fires, or Clean Start=true CONNECT arrives), tombstone the session keys in this order:

  1. <clientId>#offsets
  2. <clientId>#will
  3. <clientId>#expiry
  4. <clientId> (main record last)

This ordering ensures no replica reading mqtt-sessions ever observes a state where the main session record is gone while auxiliary records remain. Kafka log compaction physically removes tombstoned records after delete.retention.ms (default 24 hours).

Migration

The compacted topic and the legacy mechanisms coexist for one release.

QoS state: the session-resume path reads from both during this window — read the <clientId>#offsets record first; if absent, fall back to fetching offset commits from the per-clientId Kafka group. Going forward, write only to <clientId>#offsets. After the deprecation window, the offset-commit read fallback is removed; stale offset commits in __consumer_offsets are cleaned up by Kafka retention.

Key renames: on resume, read both the new (#will, #expiry) and legacy (#will-signal, #expiry-signal) keys; prefer the new key when present. Going forward, write only the new keys and tombstone the corresponding legacy key. After the deprecation window, drop the legacy-key read path.

Consequences

  • Zero Kafka consumer groups created by mqtt-kafka (with Migrate mqtt-kafka session ownership to Store SPI #1798).
  • No Kafka offset retention exposure for MQTT session state — sessions can have arbitrary expiry intervals.
  • MQTT 3.1.1 Clean Session=false persists indefinitely until tombstoned, matching protocol intent.
  • Single durable storage mechanism for all per-session state under a fixed key set.

In scope

  • Define the <clientId>#offsets blob schema (map of (topic, partition) → consumed offset, in-flight packet IDs, idempotent producer state).
  • Rewrite MqttKafkaPublishFactory.doCommitOffsetIncomplete (and equivalents) to update the #offsets record.
  • Rewrite MqttKafkaSubscribeFactory QoS 1+ offset persistence to update the #offsets record.
  • Rewrite session-resume in MqttKafkaSessionFactory to read QoS state from the #offsets record.
  • Rename #will-signal → #will and #expiry-signal → #expiry, with read-both/write-new/tombstone-legacy migration.
  • Fixed-key-set cleanup with defined tombstone ordering (#offsets → #will → #expiry → <clientId>).
  • Transitional read fallback to Kafka offset commits (for QoS state) and to legacy signal keys (for renames) for one release window.
  • After the deprecation window, remove offset-fetch/commit code paths, per-clientId groupId construction, and legacy-signal-key read paths.
  • Tests: QoS 1 in-flight replay on resume, QoS 2 publish idempotency across reconnect, session resume after offline duration greater than Kafka offsets.retention.minutes, MQTT 3.1.1 Clean Session=false indefinite persistence, fixed-key-set cleanup on session expiry and on Clean Start=true, key-rename migration (resume reads legacy keys, writes new keys, tombstones legacy).
  • Documentation update.

Acceptance criteria

  • All existing QoS 1/2 publish and subscribe tests pass with the new persistence mechanism.
  • A new test covers session resume after an offline duration greater than Kafka's offsets.retention.minutes, with full QoS state restored.
  • A new test covers MQTT 3.1.1 Clean Session=false resumption after an extended offline period with no state loss.
  • After the deprecation window, no mqtt-kafka code path creates, fetches, or commits to a Kafka consumer group identity for offset state.
  • kafka-consumer-groups.sh --list shows no groups attributable to mqtt-kafka for any active or recently-disconnected session.
  • The four session keys (<clientId>, <clientId>#offsets, <clientId>#will, <clientId>#expiry) are tombstoned in the documented order on session expiry and on Clean Start=true; compaction removes them after delete.retention.ms.
  • Sessions established under the legacy #will-signal / #expiry-signal keys resume correctly after upgrade, with state rewritten under the new keys and legacy keys tombstoned.
  • Documentation updated for the new persistence model, the fixed key set, and cleanup behavior.

References

  • runtime/binding-mqtt-kafka/src/main/java/io/aklivity/zilla/runtime/binding/mqtt/kafka/internal/stream/MqttKafkaSessionFactory.java (offset fetch/commit paths, signal keys, cleanup)
  • runtime/binding-mqtt-kafka/src/main/java/io/aklivity/zilla/runtime/binding/mqtt/kafka/internal/stream/MqttKafkaPublishFactory.java (doCommitOffsetIncomplete)
  • runtime/binding-mqtt-kafka/src/main/java/io/aklivity/zilla/runtime/binding/mqtt/kafka/internal/stream/MqttKafkaSubscribeFactory.java
  • specs/binding-mqtt-kafka.spec/src/main/scripts/io/aklivity/zilla/specs/binding/mqtt/kafka/config/ (mqtt-sessions topic schema/configuration)

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