Repository navigation
Description
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:
- 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.1Clean Session=falseclients, which have no spec-defined expiry — can lose QoS state before the session is supposed to expire. - 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>#offsetsrecord instead of committing a Kafka offset.MqttKafkaSubscribeFactoryQoS 1+ offset persistence: rewrite to update the<clientId>#offsetsrecord.MqttKafkaSessionFactorysession-resume: read the<clientId>#offsetsrecord to restore offsets, in-flight packet IDs, and producer state, instead of issuingOffsetFetchagainst the per-clientId group.- Remove
newOffsetFetchStreamandnewOffsetCommitStreampaths and the per-clientId groupId construction inMqttKafkaSessionFactory. - Update
#will-signal/#expiry-signalproducers and consumers to use the new#will/#expirykey 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:
<clientId>#offsets<clientId>#will<clientId>#expiry<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=falsepersists 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>#offsetsblob schema (map of(topic, partition)→ consumed offset, in-flight packet IDs, idempotent producer state). - Rewrite
MqttKafkaPublishFactory.doCommitOffsetIncomplete(and equivalents) to update the#offsetsrecord. - Rewrite
MqttKafkaSubscribeFactoryQoS 1+ offset persistence to update the#offsetsrecord. - Rewrite session-resume in
MqttKafkaSessionFactoryto read QoS state from the#offsetsrecord. - Rename
#will-signal→#willand#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.1Clean Session=falseindefinite persistence, fixed-key-set cleanup on session expiry and onClean 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=falseresumption after an extended offline period with no state loss. - After the deprecation window, no
mqtt-kafkacode path creates, fetches, or commits to a Kafka consumer group identity for offset state. kafka-consumer-groups.sh --listshows no groups attributable tomqtt-kafkafor 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 onClean Start=true; compaction removes them afterdelete.retention.ms. - Sessions established under the legacy
#will-signal/#expiry-signalkeys 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.javaspecs/binding-mqtt-kafka.spec/src/main/scripts/io/aklivity/zilla/specs/binding/mqtt/kafka/config/(mqtt-sessionstopic schema/configuration)
Activity
feat(binding-mqtt-kafka): add session #offsets blob schema (#1799)
feat(binding-mqtt-kafka): add #offsets blob (de)serialization helper (#…
test(binding-mqtt-kafka): add sessionOffsets k3po builder/matcher (#1799
test(binding-mqtt-kafka): rewrite publish.qos2 spec to #offsets record (
feat(binding-mqtt-kafka): persist QoS2 offsets in #offsets record (#1799
test(binding-mqtt-kafka): migrate qos2.retained + mixture.qos specs t…
fix(binding-mqtt-kafka): emit #offsets completion produce on resume (#…
test(binding-mqtt-kafka): migrate qos2 init.producer/offset.commit ab…
fix(binding-mqtt-kafka): propagate #offsets fetch abort to mqtt conne…
feat(binding-mqtt-kafka): rename session signal keys to #will / #expi…
test(binding-mqtt-kafka): cover sessionOffsets matcher branches (#1799)
[2.0] Refactor MQTT session management to use store-based persistence (…