Should IoT data go through MQTT, Kafka or both?
MQTT and Apache Kafka compared: broker vs distributed log, connections, retention and replay, why IoT platforms bridge the two, and where Sparkplug fits.
Photo: İsmail Enes Ayhan / Unsplash
After reading this article you will be able to:
- Explain how an MQTT broker differs from a Kafka log in connections, storage and replay
- Design a bridge that carries device telemetry from MQTT into Kafka and commands back
- Decide where retention, Sparkplug decoding and stream processing belong in the pipeline
Drafted with AI from primary sources · checked by a separate AI agent · spot-checked by our editors · How we check
On this page
MQTT and Apache Kafka are both described as publish-subscribe messaging, which is why they are often compared, but they solve different problems. An MQTT broker connects large numbers of small, intermittently connected devices and delivers each message to whoever is subscribed right now. Kafka is a distributed, replicated log: it stores streams of events durably so that many back-end applications can read them at their own pace, and read them again later. One belongs at the device edge, the other in the data backbone.
The short answer. Use MQTT for the devices: it handles millions of lightweight connections, unreliable links, per-device security and commands sent back to devices. Use Kafka behind it when several systems need the same data, when you need to replay history, or when stream processing and analytics need a buffer that absorbs bursts. Most large IoT platforms use both, joined by a bridge. A small system with one consuming application may need only MQTT; a system whose “devices” are servers or gateways on stable networks may produce to Kafka directly.
| MQTT broker | Apache Kafka | |
|---|---|---|
| Main role | Device messaging at the edge | Durable event log in the back end |
| Typical clients | Many small devices, often offline | Fewer server-side producers and consumers |
| What it keeps | Queued messages for offline sessions, last retained value per topic | Every event, for a configured retention period |
| Replay | No | Yes, from any stored offset |
| Scaling unit | Topic hierarchy with wildcards | Partitioned topics spread across brokers |
| Messages to devices | Built in | Not designed for it |
How does an MQTT broker differ from Kafka?
An MQTT broker routes messages. A device publishes to a topic such as plant1/line2/press4/temperature, the broker matches it against current subscriptions and forwards it. Once delivered, the message is gone, apart from two exceptions: the broker queues QoS 1 and 2 messages for clients with a persistent session that are offline, and it can keep one retained message per topic as the “last known value”. The MQTT explained guide covers both in detail.
Kafka stores. Producers append events to a topic, which is split into partitions spread across brokers, and events are not deleted after consumption. Each consumer tracks its own position (offset), so a dashboard, a billing job and a machine learning pipeline can read the same stream independently, and a new application can start from the beginning of what is retained. Retention is configured per topic, by time or size. The broker default is 168 hours, seven days, and many deployments set longer or shorter values per topic.
Put simply, MQTT answers “who needs this message now?” and Kafka answers “where is the authoritative record of everything that happened, in order?”
Why not connect devices straight to Kafka?
Kafka’s client model assumes well-connected servers. A Kafka producer discovers the cluster, keeps connections to the brokers that lead the partitions it writes to, and batches records for throughput. That suits a back-end service; it does not suit a battery sensor on a cellular link that wakes for a few seconds an hour, or a fleet of a million devices each needing its own credentials and access rules.
MQTT was built for exactly that case: a small client library, one TCP connection per device, keep-alives tuned to the network, a Last Will when a device drops, and topic-level permissions per device. MQTT also carries commands and configuration down to devices over the same connection. Kafka has no notion of pushing a message to a specific device that is not running a consumer.
Gateways and edge servers are the exception. An edge gateway in a factory with a stable network can produce to Kafka directly, and some architectures do exactly that.
Why not use MQTT as the backbone?
Because a broker does not keep history. If an analytics service is down for an hour, it misses an hour of data unless every message used QoS 1 or 2 on a persistent session, and even then the broker is holding a queue it was not designed to hold for long. If you fix a bug in a processing job and need to recompute last week’s results, the broker cannot give you last week’s messages. If ten applications want the same data, the broker delivers it ten times in real time, and the slowest consumer becomes a problem.
Kafka handles all three: consumers fall behind and catch up from their offset, data is replayed from retention, and adding consumers does not change what producers do. It also feeds stream processing frameworks and connectors that load data into time-series databases and data lakes.
How does an MQTT-to-Kafka bridge work?
The bridge subscribes to MQTT topics and produces records to Kafka, and optionally does the reverse for commands. There are three common ways to build it:
- A broker extension or integration. The HiveMQ Enterprise Extension for Kafka forwards MQTT publishes to Kafka topics, using the MQTT topic as the Kafka key and the payload as the value, and can poll Kafka topics and publish them to MQTT. EMQX offers a Kafka sink and a Kafka source for the two directions.
- Kafka Connect. Kafka Connect is Kafka’s framework for streaming data between Kafka and other systems; an MQTT connector runs as an MQTT client and writes into Kafka.
- A custom service that subscribes with an MQTT client library and produces with a Kafka client, useful when messages need decoding or enrichment on the way.
Design choices matter more than the tool. Map many fine-grained MQTT topics onto a few Kafka topics, and use the device ID as the Kafka key so that each device’s events stay in order within one partition. Decide whether the bridge needs MQTT QoS 1 to avoid losing messages when Kafka is slow, and how it applies back-pressure. Keep the command path (Kafka to MQTT) separate and authorized, because it reaches physical devices.
Where does Sparkplug fit?
Sparkplug is an Eclipse specification, at version 3.0, that defines an MQTT topic namespace, a payload format and session state management for industrial systems. It lives entirely on the MQTT side: edge nodes announce themselves and their metrics, and SCADA and historians on the same broker understand them without custom parsing.
When Sparkplug data flows into Kafka, the bridge or a downstream stream processor has to decode the binary payloads and keep track of birth and death messages, otherwise consumers see opaque bytes and cannot tell a stale value from a live one. Many industrial architectures therefore keep a Sparkplug-aware MQTT layer for operations and send decoded, flattened events into Kafka for analytics.
What has changed in Kafka for IoT teams?
Operating Kafka used to mean also operating Apache ZooKeeper. Kafka 4.0, released in March 2025, is the first major release that runs entirely without ZooKeeper, in KRaft mode by default, which removes one cluster from the picture. The same release offered early access to share groups (“Queues for Kafka”), which let several consumers cooperate on one topic more like a traditional message queue. Neither change makes Kafka a device protocol, but both lower the cost of running it next to an MQTT layer.
Where to go next
- The IoT data pipeline shows the stages from ingestion to action that Kafka usually sits between.
- MQTT explained covers sessions, QoS and retained messages on the device side.
- MQTT vs HTTP compares MQTT with the other common device protocol.
- IoT platforms explained describes the managed services that often hide both layers.
Topics: MQTT, Apache Kafka, data pipeline, streaming, architecture, comparison