00:00

Kafka Scenario-Based Interview Questions

Last updated:

Scenario questions test whether you can apply Kafka knowledge to a real production problem. Interviewers for senior developer and tech lead roles care less about definitions here and more about how you reason: what you check first, which trade-offs you weigh, and how you prevent the problem from returning.

Each scenario below has a short situation and a structured answer: diagnose, fix, and prevent. The answers assume Kafka 4.x and Java clients. For the basics behind them, start with Kafka Interview Questions and Answers.

How to answer: State your assumptions, ask what the guarantees must be (loss, duplicates, ordering, latency), then propose a fix and a way to verify it with metrics.

1. Throughput and Lag

1.1 A sale event multiplies order traffic by ten and consumer lag keeps growing. What do you do?

Diagnose: check lag per partition (kafka-consumer-groups.sh --describe), consumer CPU and the time spent per record, and whether the bottleneck is the consumer or a downstream database. Fix:

  • If there are fewer consumers than partitions, scale the consumer group out.
  • If every partition already has a consumer, increase processing speed: batch database writes, use max.poll.records with bulk operations, or process records in a bounded worker pool per partition.
  • Add partitions only if needed, knowing that existing keys may map to new partitions.

Prevent: size partitions for peak load, load-test before the event, and alert on lag growth, not only on absolute lag.

1.2 One partition receives far more data than the others. How do you handle a hot key?

Diagnose: compare bytes-in per partition and find the dominant key, for example one large merchant. Fix: if per-key ordering is not needed for that key, add a suffix (such as merchant-42#3) to spread it over several partitions and merge results downstream. If ordering is needed, give the hot key dedicated capacity, for example its own topic and consumer. Prevent: choose keys with high cardinality and review key distribution when onboarding large customers.

1.3 You must choose a partition count for a new topic. How do you decide?

Estimate the peak throughput and divide by what one consumer can process (and what one partition can absorb on your hardware). Add headroom for growth, because increasing partitions later changes the key-to-partition mapping. Avoid extremes: too few limits parallelism; too many increases metadata, open files and recovery time. Typical values range from 6 to a few dozen per topic.

2. Delivery Guarantees

2.1 Customers receive the same notification email twice after each deployment. Why, and how do you fix it?

Diagnose: a deployment stops consumers; records processed but not yet committed are redelivered to the new instances. This is normal at-least-once behaviour. Fix: make the handler idempotent, for example by storing the notification ID with a unique constraint and skipping duplicates. Commit offsets on partition revocation and shut down gracefully so that fewer records are replayed. Prevent: use static membership or cooperative rebalancing for rolling deploys, and design every side effect to be idempotent.

2.2 During broker maintenance, producers start timing out and broker logs show NotEnoughReplicasException. What happened?

The topic probably uses replication factor 3 with min.insync.replicas=2, and with one broker stopped for maintenance, another replica was slow or offline, so the ISR dropped to one. The leader then rejects acks=all writes with NotEnoughReplicasException; the client treats it as retriable, keeps retrying, and reports a TimeoutException when delivery.timeout.ms expires. Check kafka-topics.sh --describe --under-min-isr-partitions, restore the missing replica, and next time take only one broker down at a time after confirming that no partitions are under-replicated.

2.3 Payment events from Kafka must update PostgreSQL exactly once. How would you design it?

Kafka transactions do not cover PostgreSQL, so make the database the source of truth. In one database transaction, apply the payment and store the consumed topic, partition and offset (or the event ID) in a table with a unique constraint. On startup, seek to the stored offsets, or skip events already recorded. This gives effectively-once results with at-least-once delivery.

3. Ordering

3.1 Account events must stay in order, but throughput must be high. How do you achieve both?

Use the account ID as the key, so each account's events stay in one partition, while different accounts are processed in parallel across many partitions. Keep the idempotent producer enabled to preserve order with retries. On the consumer side, process records of the same key sequentially (for example with a per-key queue inside a worker pool), and avoid non-blocking retry topics for these events because they break ordering.

4. Data and Schemas

4.1 You need to send 8 MB documents through Kafka. What are your options?

Kafka's default maximum record size is about 1 MB. Prefer the claim-check pattern: store the document in object storage and send a small event with its location. If the data must go through Kafka, raise max.request.size on the producer, max.message.bytes on the topic and the consumer's max.partition.fetch.bytes, enable compression, and test the impact on memory and replication.

4.2 A producer team changed the event format and several consumers started failing. How do you prevent this?

Introduce schemas with compatibility rules, for example Avro or Protobuf with a schema registry set to backward compatibility, so incompatible changes are rejected at registration time. Treat event schemas as contracts with versioning, add fields as optional, and never change the meaning of an existing field. Meanwhile, protect consumers with an error-handling deserializer and a dead-letter topic.

4.3 A bug corrupted three days of computed results. How do you reprocess the data?

Confirm the input topic still retains those three days. Deploy the fixed consumer, stop the group, and reset its offsets to the start time with kafka-consumer-groups.sh --reset-offsets --to-datetime ... --execute, or run a new group from that time. Make sure the output is idempotent (upserts) so reprocessing overwrites wrong results instead of duplicating them. See Kafka Tools and CLI Commands.

4.4 A user requests deletion of their personal data from a compacted topic. What do you do?

Produce a tombstone, a record with the user's key and a null value. Compaction removes earlier records for that key, and the tombstone itself is removed after delete.retention.ms (1 day by default). For non-compacted topics, rely on retention, or avoid storing personal data directly, for example by encrypting it with a per-user key that can be destroyed.

5. Operations and Architecture

5.1 Consumers on Kubernetes rebalance constantly during rolling deployments. How do you stabilise them?

Set a stable group.instance.id per pod (for example from a StatefulSet name) so restarts within the session timeout do not trigger rebalances, use the cooperative sticky assignor or group.protocol=consumer, and make shutdown graceful so pods leave cleanly. Also check that processing per poll stays below max.poll.interval.ms.

5.2 Your company is still on Kafka 3.x with ZooKeeper and wants Kafka 4.x. What is the plan?

Kafka 4.0 cannot run with ZooKeeper, so migrate to KRaft first. Upgrade to a 3.x release that supports migration (3.9 is the last and recommended bridge release), deploy KRaft controllers, run the documented ZooKeeper-to-KRaft migration, finalise it, and then upgrade to 4.x. Check client and broker compatibility, removed features such as MirrorMaker 1, and the Java version: Kafka 4.x brokers need Java 17. See ZooKeeper vs KRaft.

5.3 After enabling TLS on the cluster, broker CPU rose sharply and producer throughput dropped. Why, and what do you do?

Diagnose: with plaintext listeners, brokers send log data to consumers and followers with zero-copy transfer; with TLS, every byte must be encrypted in the JVM, so CPU becomes the bottleneck. Check broker CPU, network and request handler idle ratios, and whether clients now negotiate a costly cipher suite.

Fix: keep encryption, but reduce the work per byte: enable compression (lz4 or zstd) so less data is encrypted, increase batching (linger.ms, batch.size), use a modern JDK with hardware-accelerated AES-GCM, and add broker capacity if needed. Keep replication traffic on a separate, secured listener so it can be sized independently.

Prevent: load-test security changes before production and include encryption overhead in capacity planning. See Kafka Security.

6. Conclusion

Good scenario answers follow the same pattern: clarify the required guarantees, diagnose with metrics and tools, fix the immediate problem, and change design or configuration so it does not return. Most Kafka problems in production come down to partitioning, offset commits, idempotence and configuration of replication, so be ready to explain those trade-offs with numbers.

7. References