Scrivio Interview library
ScenarioIntermediate7 min read · updated 13 November 2026

A smart-home product must send commands to 2 million devices. Each device must receive only its own commands, devices are often offline for days, and commands must be delivered when they reconnect.

A team proposes a Kafka topic per device, or a single topic that each device consumes filtered by its key. Evaluate this and propose an architecture.

Neither proposal works: a topic per device means two million topics, far beyond what Kafka can manage, and a shared topic cannot be filtered by key on the broker, so every device would download everyone's commands and would need broker credentials. Per-device delivery with offline buffering is a device-messaging problem, best solved with an MQTT broker using persistent sessions and QoS 1 (or a per-device inbox in a database) behind a device gateway. Kafka fits behind that gateway, carrying telemetry in and commands keyed by device id, and every command carries an expiry so that an unlock does not run three days late.

Why this question is asked

Kafka is so central in many companies that it becomes the default answer to every messaging question. This scenario tests whether we know where Kafka's model stops fitting: millions of small, often offline clients that each need only their own messages. Interviewers ask it to see whether we can reason from how partitions, topics and consumers really work to a sensible architecture with the right tool at each layer. In this answer, we will recap the relevant Kafka basics, see why both proposals fail, design a gateway with an MQTT broker, place Kafka where it shines, and look at command expiry.

1A Quick Recap of Topics, Partitions and Consumers

A Kafka topic is divided into partitions, and each partition is a log stored on brokers as segment files with index files beside them. Every partition has a leader and followers, appears in the cluster metadata, and costs open file handles, memory and replication work. Clusters commonly hold tens of thousands of partitions, and KRaft in Kafka 4 raises the practical ceiling, but each one still has a real cost.

A consumer reads whole partitions. It can ask for a partition and an offset, but it cannot ask the broker for only the records with key device-42. Filtering by key happens in the consumer, after the records have been fetched. That one fact shapes the whole answer.

2Why a Topic per Device Fails

Two million devices means two million topics, and with replication factor 3, six million partition replicas. That is far beyond what a Kafka cluster can manage: metadata would be enormous, every broker would hold hundreds of thousands of open files, leader elections after a broker restart would take a very long time, and most of these partitions would hold a handful of records a week.

There is also a client problem. Devices on home networks with intermittent connections are poor Kafka clients. A Kafka consumer keeps fetch sessions, heartbeats with a group coordinator and expects reasonably stable connectivity. And each device would need credentials for the broker, which means two million identities with ACLs, any one of which could be extracted from a device and abused.

3Why One Shared Topic Filtered by Key Fails

With one commands topic keyed by device id, all commands for a device land in the same partition, which sounds right. But a device cannot read only its own key. With, say, 48 partitions, each partition holds the commands for about 40,000 devices, so a device would download 40,000 devices' worth of commands and throw away all but its own. That wastes bandwidth on constrained connections and, worse, delivers other customers' commands to every device, which is a security failure no filter in the client can fix.

So the problem is not choosing the right topic layout. It is that per-device delivery with offline buffering is a different problem from what Kafka solves. Kafka is excellent at a few consumers reading a lot of data; here we have millions of consumers each reading a little.

The two proposals
ProposalWhat breaksWhy
Topic per deviceCluster limits2 million topics, 6 million replicas
Topic per deviceDevice clientsKafka clients and credentials on devices
Shared topic, filter by keyBandwidthDevice reads the whole partition
Shared topic, filter by keySecurityEvery device sees other devices' commands

4The Architecture: a Device Gateway With MQTT

Device messaging has its own mature protocol: MQTT. Each device keeps a long-lived, lightweight connection to an MQTT broker (for example EMQX, HiveMQ, Mosquitto at small scale, or a cloud IoT service) and subscribes to its own topic, such as devices/42/commands. With a persistent session and QoS 1 (delivered at least once), the MQTT broker keeps undelivered messages for a device while it is offline and delivers them when it reconnects. Device identity comes from per-device certificates, and the broker's access rules let device 42 subscribe only to devices/42/#.

Around the MQTT layer we put a gateway service that we own. It handles authentication and authorization, routing and the rules about commands. If we prefer not to rely on the MQTT broker's session storage for days of buffering, the gateway can keep a per-device inbox instead, a table or a Redis list keyed by device id with pending commands, and push them when the device connects. A database table indexed by (device_id, created_at) handles two million inboxes easily, because most of them are empty at any moment.

5Where Kafka Fits

Kafka belongs behind the gateway, where it is strong. In the many-to-one direction, telemetry from two million devices flows through the MQTT layer into a few Kafka topics, keyed by device id, where analytics, alerting and storage services consume it with normal consumer groups. In the one-to-many direction, backend services publish commands to a device-commands topic keyed by device id. The gateway tier is a consumer group on that topic; each gateway instance reads its partitions and, for each command, either forwards it to the MQTT broker for a connected device or stores it in the inbox for later.

Keying by device id keeps all commands for one device in one partition, so they are processed in order. And the backend services never need to know which gateway a device is connected to or whether it is online.

Step throughDevice command and telemetry architecture
  1. Backend servicespublish commands, consume telemetry
  2. Kafkadevice-commands and telemetry topics, keyed by device id
  3. Device gatewayauth, routing, expiry checks, per-device inbox
  4. MQTT brokerpersistent sessions, QoS 1, per-device topics
  5. 2 million devicesone MQTT connection each, often offline

5 layers, from the top down. Step through them one at a time.

Kafka handles the few-consumers, high-volume path; MQTT handles the millions of small device connections.

6Command Expiry

Buffering for days creates a new risk: stale commands. Unlock the door is fine within seconds and dangerous three days later. So every command carries a deadline, for example expires_at set by the service that created it, and the gateway checks it at every step: when it reads the command from Kafka, when it pushes from the inbox, and the device itself checks again before acting, since its clock is the last line of defence. Expired commands are dropped and reported back as expired, so the backend can show the user that the door was not unlocked.

Some commands are better as desired state than as commands. Set the thermostat to 21 degrees can be stored as the device's desired configuration; when the device reconnects, it receives the latest desired state, not a queue of every change made while it was offline.

7Follow-up questions

How many partitions would the device-commands topic need?

Size it for throughput and the number of gateway instances, not for devices. If commands peak at a few thousand per second and we run 12 to 24 gateway instances, 48 partitions give room to grow. Keying by device id spreads the two million devices across them.

Why not let devices connect to Kafka through a REST proxy?

A REST proxy solves the protocol issue but not the model: the device would still read partitions rather than its own messages, and polling over HTTP from two million devices is expensive. MQTT's persistent connection and per-device topics are built for this.

How does the backend know a command was executed?

The device publishes an acknowledgement over MQTT, the gateway turns it into a CommandAcknowledged event on a Kafka topic keyed by device id, and the backend matches it by command id. Combined with expiry, every command ends as acknowledged, expired or failed.

8Conclusion

In this answer, we saw why both proposals fail: a topic per device overwhelms the cluster with millions of partitions, and a shared topic cannot be filtered by key on the broker, so devices would download and see everyone's commands. Per-device delivery with offline buffering is a device-messaging problem, solved by an MQTT broker with persistent sessions behind a gateway, or by a per-device inbox. Kafka fits behind the gateway for telemetry and for commands keyed by device id, and every command needs an expiry so it never runs dangerously late.

What to say in the interviewAbout 80 seconds aloud

Open with

Both proposals run into how Kafka works. A topic per device means two million topics and, with replication, six million partition replicas; every partition costs files, memory and metadata, so that is far beyond what a cluster can manage.

Then, if they want more

A shared topic keyed by device id does not help either, because a consumer reads whole partitions, not keys, so each device would download thousands of other devices' commands and filter them locally, which wastes bandwidth and is a security problem; devices also should not hold broker credentials.

Per-device delivery with offline buffering is a device-messaging problem, so I would put an MQTT broker behind a device gateway: each device holds one connection, subscribes to its own topic, and a persistent session with QoS 1 keeps commands until it reconnects, or the gateway keeps a per-device inbox in a database or Redis.

Kafka sits behind the gateway: telemetry flows from millions of devices into a few topics, and backend services publish commands to a topic keyed by device id that the gateway consumes and routes to connected devices or stores.

Finally, every command carries a deadline, because unlock the door must not run three days late, so the gateway and the device drop expired commands and report them.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Kafka documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A team proposes a Kafka topic per device, or a single topic that each device consumes...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/748b9233ec/