mqtt-kafka proxy
mqtt-kafka proxy
The mqtt-kafka proxy binding for adapting MQTT topic streams to Kafka topic streams. By configuring the Kafka topics that the proxy will use to route mqtt messages and session states an mqtt server binding can allow clients to connect and proxy MQTT messages onto Kafka topics.
Scaling across multiple Zilla instances
MQTT server redirection, used to scale the mqtt server across multiple Zilla instances, is configured via the engine's service.hostname property rather than a binding option.
mqtt_kafka_proxy:
type: mqtt-kafka
kind: proxy
options:
store: my_store
topics:
sessions: mqtt-sessions
messages: mqtt-messages
retained: mqtt-retained
clients:
- place/{identity}/#
routes:
- when:
- publish:
- topic: place/+/device/#
- subscribe:
- topic: place/+/device/#
with:
messages: mqtt-devices
exit: kafka_cache_client
exit: kafka_cache_clientShared subscriptions
The binding maps MQTT 5 shared subscriptions, $share/<ShareName>/<filter>, to Kafka share groups. Each shared subscription, identified by its share name and topic filter, is its own Kafka share group. The Kafka broker splits the messages between the sessions subscribed to it, so the load may not be split evenly. A message is delivered to one session at QoS 0, 1 or 2.
mqtt_kafka_proxy:
type: mqtt-kafka
kind: proxy
options:
store: my_store
topics:
sessions: mqtt-sessions
messages: mqtt-messages
retained: mqtt-retained
exit: kafka_cache_clientRequirements
- Configure
options.storeon the binding. Without it themqttserver advertises that shared subscriptions are not available, and a client subscribing to a$sharefilter is disconnected with reason code0x9E(shared subscriptions not supported). - Use Kafka 4.1 or later with the
share.version=1feature enabled. Share groups are a preview feature in Kafka 4.1. - Use MQTT 5 clients. A
$sharefilter in an MQTT 3.1.1 subscription is refused with SUBACK reason code0x80. A shared subscription with the no local option is a protocol error and the client is disconnected with reason code0x82.
Enable the Kafka feature, using the Kafka tools.
kafka-features.sh --bootstrap-server <bootstrap-server> upgrade --feature share.version=1A Kafka cluster with a single broker also needs a share coordinator state topic with a replication factor and minimum in-sync replicas of 1.
share.coordinator.state.topic.replication.factor=1
share.coordinator.state.topic.min.isr=1If the Kafka broker does not support share groups, the SUBSCRIBE is refused with SUBACK reason code 0x83 and the kafka binding reports BINDING_KAFKA_API_VERSION_REJECTED for ShareGroupHeartbeat.
The zilla.binding.mqtt.kafka.shared.subscription.available property, default true, set to false stops the binding from supporting shared subscriptions, as when no store is configured.
zilla start -Pzilla.binding.mqtt.kafka.shared.subscription.available=falseShare groups
The Kafka share group id is built from the binding, the share name, the topic filter and an epoch.
<prefix>.share.<ShareName>.<base64url(filter)>.<epoch><prefix>is the group id prefix of the binding,zilla:<zilla namespace>-<binding name>by default, with each:replaced by..<ShareName>has%escaped as%25and:escaped as%3A.<base64url(filter)>is the topic filter, base64url encoded without padding. Zilla filters the messages after the Kafka broker has acquired them, so the filter is part of the group id and a different filter is a different share group.<epoch>starts at0and increases by one each time a session joins the shared subscription after every session has left it, so the new group starts at the latest offset instead of the backlog of the previous group.
For example, a subscription to $share/group/sensor/# on the north_mqtt_kafka_mapping binding of the zilla-mqtt-kafka-proxy namespace is the share group zilla.zilla-mqtt-kafka-proxy-north_mqtt_kafka_mapping.share.group.c2Vuc29yLyM.0.
Zilla never deletes share groups. A group of an earlier epoch remains until it is deleted by an operator, for example with kafka-share-groups.sh.
The first session to join a new share group is acknowledged with SUBACK after about one Kafka share heartbeat interval, 5 seconds by default, and a later session after 1 to 2 seconds. If the Kafka broker restarts, the MQTT connections of subscribed sessions are closed, and published messages are delivered again after about 25 to 30 seconds.
Store
The store keeps the state shared by the sessions of a shared subscription. Each key starts with the group id prefix, <prefix> here with its : kept, and is stored without a time to live.
| Key | Value |
|---|---|
<prefix>:share:<ShareName>:<base64url(filter)>:registry | <epoch>:<count>, the current epoch and the number of sessions joined |
<prefix>:memberships:<clientId> | The shared subscriptions joined by the client |
<prefix>:tickets:<clientId> | The claim tickets of the QoS 2 messages in flight to the client |
A QoS 2 message is claimed from the Kafka share group with a ticket kept in the store until the exchange completes. If the client reconnects, the in-flight message is delivered again to the same client with the same packet id and the DUP flag set, or the PUBREL is sent again, instead of the message being delivered to another session.
The binding uses the get, put, putIfAbsent, delete and replace operations of the store. Use a store that is shared and durable for Zilla instances to recover after a restart or to run more than one instance, as a memory store only lasts as long as its own process.
Limitations
- Recovery of a QoS 2 message whose Kafka offset is at or beyond the end of the log is not verified, and may wait or drop the ticket.
- The tickets of a subscription that is not restored when the client reconnects remain until the session is terminated.
- The memberships and tickets have no time to live.
- Publishing with QoS 1 or 2 by a client of the
mqttclientbinding is not supported yet, see aklivity/zilla#3061. - Kafka compression is not supported yet when fetching from a share group.
Configuration (* required)
options*
object
The mqtt-kafka specific options.
options.topics*
object
The kafka topics Zilla needs when routing MQTT messages
options:
topics:
sessions: mqtt-sessions
messages: mqtt-messages
retained: mqtt-retainedtopics.sessions*
string
A Kafka topic for storing mqtt session states.
cleanup.policy Required
A compact cleanup.policy is required.
topics.messages*
string
The default Kafka topic used for routing mqtt messages.
topics.retained*
string
A Kafka topic for storing mqtt retained messages.
cleanup.policy Recommended
A compact cleanup.policy is recommended.
options.store
string
The name of a configured store used to keep the state of MQTT 5 shared subscriptions. Without a store, shared subscriptions are not available. Read more: Shared subscriptions
options:
store: my_storeoptions.clients
arrayofstring
Pattern defining how to extract client identity from the topic. Using this we can ensure that all messages for the same client identity are produced to Kafka on the same topic partition.
options:
clients:
- place/{identity}/#options.publish
object
The MQTT client publish specific options.
publish.qosMax*
enum[at_most_once,at_least_once,exactly_once] | Default:exactly_once
Highest allowed QOS level.
options:
publish:
qosMax: at_most_onceroutes
arrayofobject
Conditional mqtt-kafka specific routes.
routes:
- when:
- publish:
- topic: place/+/device/#
- subscribe:
- topic: place/+/device/#
with:
messages: mqtt-devices
exit: kafka_cache_clientroutes[].guarded
objectas map of namedarrayofstring
List of roles required by each named guard to authorize this route.
routes:
- guarded:
my_guard:
- publish:clientsroutes[].when
arrayofobject
List of conditions (any match) to match this route when adapting mqtt topic streams to kafka topic streams. Read more: When a route matches
routes:
- when:
- publish:
- topic: place/#
- subscribe:
- topic: place/#when[].publish
arrayofobject
Array of MQTT topic filters matching topic names for publish.
- publish:
- topic: place/#
- topic: subs/#publish[].topic
string| Pattern:^(\\/?([\\w{}\\.-]*|\\+)(\\/((?![-_])[\\w{}\\.-]*|\\+))*(\\/#)?|#|\\/|\\$SYS(\\/((?![-_])[\\w{}\\.-]*|\\+))*(\\/#)?)$
MQTT topic filter pattern.
when[].subscribe
arrayofobject
Array of MQTT topic filters matching topic names for subscribe.
- subscribe:
- topic: place/#
- topic: subs/#subscribe[].topic
string| Pattern:^(\\/?([\\w{}\\.-]*|\\+)(\\/((?![-_])[\\w{}\\.-]*|\\+))*(\\/#)?|#|\\/|\\$SYS(\\/((?![-_])[\\w{}\\.-]*|\\+))*(\\/#)?)$
MQTT topic filter pattern.
subscribe[].share
string| Pattern:^[^/+#]+$
MQTT 5 shared subscription name matched by this condition, either exactly or with * as a wildcard for any characters. A subscription matches the condition when both its share name and its topic filter match. When omitted, any share name matches, as does a subscription that is not shared. When set, only shared subscriptions match.
- subscribe:
- topic: sensor/#
share: consumers-*routes[].exit
string
Next binding when following this route.
routes[].with*
object
Kafka parameters for matched route when adapting mqtt topic streams to kafka topic streams.
with:
messages: mqtt-deviceswith.messages
string
Kafka topic to use for the route.
with:
messages: sensors.${params.sensorId}Supports dynamic parameter substitution using the following pattern:
${params.sensorId}– Substitutes a path parameter.
exit
string
Default exit binding when no conditional routes are viable.
exit: echo_servertelemetry
object
Defines the desired telemetry for the binding.
telemetry.metrics
array
Telemetry metrics to track
telemetry:
metrics:
- stream.*telemetry.attributes
objectas map of namedstringproperties
Telemetry attributes to add to the binding metrics.

