Designing systems with Kafka
34 questions in 7 subtopics.
Designing with Kafka means deciding what events a system publishes, how they are keyed and partitioned, how failures are retried, and where Kafka is the right tool at all. These decisions outlive any single service: a poor key limits scaling for years, a leaked internal table becomes a public contract, and a retry design can quietly break ordering. Teams also have to decide whether Kafka can be a source of truth and how consumers keep up when traffic spikes or a backlog builds. The questions cover event modeling and topic design, partitioning for ordering and scale, retry topics and poison messages, choosing between Kafka and other brokers, event-driven architecture across many teams, Kafka as a system of record, and scaling consumers under backpressure, from first designs to company-wide strategy.
Event modeling and topic design
Event modeling decides what each event means, what it carries and which topic it goes to. Understand the difference between notifications, state-carrying events and commands, when several event types should share a topic, how keys and envelopes are chosen, and why exposing internal tables or creating topics per tenant causes trouble at scale.
A customer service publishes CustomerUpdated events that carry only the customer id. Eight consuming services each call the customer REST API on every event to fetch the new data, about 2 million events a day.
An order service emits OrderPlaced, OrderPaid and OrderShipped, and a consumer needs all three in order per order.
A team exposes its Debezium topics for the internal tables orders and order_items directly to nine other services as its public event stream.
When the team splits order_items into two tables, all nine consumers break, and two of them have been showing half-written orders because they see the orders row before its items. What went wrong, and how would you redesign the published events?An order lifecycle is the textbook event-driven domain: many services react to the same orders, and each cares about different events.
Design the topics and event contract for an e-commerce order lifecycle at about 3,000 orders a minute at peak, with roughly six events per order, consumed by fulfilment, billing, notifications, fraud, search and analytics. Cover topic names, keys, partitions, payload style, the event envelope, retention and how a new consumer gets the current state of every open order.A SaaS platform created one topic per tenant per event type: 2,500 tenants x 8 event types x 6 partitions, about 120,000 partitions with replication factor 3.
Broker restarts take 25 minutes, onboarding a tenant means creating 8 topics, and consumers subscribe with a regex across thousands of topics. Two enterprise tenants have contracts that require isolated storage. How do you redesign the topic layout and migrate?Enterprise architecture proposes a canonical data model for the whole company: one Customer, Order and Product schema that every domain must use in its Kafka events, plus a mandatory envelope.
Twelve domains and 70 teams would have to comply. How do you respond, and what would you standardise instead?Partitioning for ordering and scale
Kafka orders records only within a partition, so the key decides both what stays in order and how far a topic can scale. Understand how to choose keys for the ordering the business really needs, why ordering does not hold across topics, how skewed keys waste consumers, and how different consumers can get different orderings.
A chat service publishes messages to a chat-messages topic.
Should the record key be the sender id, the conversation id or the message id? Explain what ordering each choice gives readers of a conversation, and list what can still reorder messages in the pipeline even with the right key.An inventory topic has 48 partitions and is keyed by warehouse id. The company has 20 warehouses.
The consumer group has 48 consumers, but monitoring shows only 16 partitions receiving data, two of them at three times the rate of the others, and 32 consumers idle. Explain why, and what you would change.An orders topic and a payments topic both have 12 partitions and are keyed by order id. A shipping service subscribes to both and sometimes receives PaymentCaptured before OrderCreated for the same order, then fails because the order is unknown.
A wallet system publishes money transfers between accounts, about 4,000 transfers per second. Each account's balance changes must be applied in order, and no account may go negative.
A transfer touches two accounts that usually hash to different partitions. Which key do you use, and how do you design the flow so both sides stay ordered and correct?A logistics platform publishes shipment events keyed by shipment id on 64 partitions, about 30,000 events per second. Invoicing now needs all events for a customer in order, a warehouse dashboard needs events per warehouse, and both teams ask the shipment team to change the key.
Design how different consumers get different ordering keys, and say what ordering each one can really rely on.Retry topics and poison messages
A record that a consumer cannot process can block a partition, be retried later or be parked in a dead-letter topic. Understand blocking and non-blocking retries in Spring Kafka, how retries interact with ordering, how to tell transient failures from bad data, and how to clean up and redrive failed records safely at scale.
Sooner or later every consumer meets a record it cannot process, and the Spring Kafka error handler decides what happens to that partition next.
What is a poison message? A Spring Kafka listener consumes 12 partitions with three consumers, and the record at offset 1,204 on partition 3 has a field the code cannot parse. The container uses aDefaultErrorHandler with FixedBackOff(1000, 9). Describe what happens to partition 3, to the other partitions, and after the retries run out, and how you would treat such a record instead.Spring Kafka can retry a failed record later through extra topics instead of holding up the partition it came from.
Configure non-blocking retries for a Spring Kafka listener on the topic payments: four attempts in total with delays of 2, 10 and 50 seconds, no retries for aValidationException, and a handler for records that end in the dead-letter topic. Show the annotations, name the topics that get created, and explain how Spring delays a record without blocking the main topic.A ledger consumer applies account events where later events depend on earlier ones (Deposit, then Withdrawal). With retry topics, a Deposit that failed on a timeout was retried 40 seconds later, after the Withdrawal for the same account had already been rejected for insufficient funds.
Design error handling that keeps per-account order while not blocking other accounts.A producer release published OrderPlaced events with an empty currency field for three hours, about 2.4 million records.
About 300 consumer groups across 60 teams use non-blocking retries, creating roughly 1,500 retry and dead-letter topics with inconsistent naming, retention and access. Dead-letter records include personal data, and nobody can say how many failures are open.
A proposal is to build a central failure service that collects all failed records into a database with a UI and a redrive API. How do you decide?Choosing Kafka or another broker
Kafka is a durable, partitioned log, which makes it strong for high-volume streams and replay but awkward for request-reply, per-message delays, millions of tiny queues or per-message acknowledgement. Understand where other tools such as traditional queues, MQTT brokers, schedulers or Pulsar fit better, and how Kafka-compatible engines change the choice.
A checkout service needs a stock check from the inventory service within 50 ms. A developer proposes doing it over Kafka with ReplyingKafkaTemplate, sending to an inventory-requests topic and waiting for an answer on a reply topic, so that the services stay decoupled.
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.An order must be cancelled if it is not paid within 30 minutes, and a reminder email goes out 24 hours after an abandoned cart; about 2 million orders a day. Kafka has no per-message delay.
Design the delayed actions, compare the options, and explain how a timer is cancelled when the order is paid.A new integration platform must host 30,000 customer-specific streams, give some consumers queue-style per-message acknowledgement, replicate selected streams between two regions, and keep 5 years of history cheaply. A team proposes Apache Pulsar instead of the company's Kafka 4.x platform.
How do you evaluate the choice, and what would you test before deciding?Several engines and managed services now speak the Kafka protocol with different storage designs and prices.
An architect proposes that the company standardise on the Kafka client API and protocol as its event interface, and choose the engine per workload: a low-latency cluster for payments, an object-storage engine for logs and analytics. How do you evaluate this as a long-term bet?Event-driven architecture at scale
Events reduce some coupling between services but add others: shared schemas, hidden consumers, eventual consistency and harder debugging. Understand what event-driven design really decouples, how event loops and cascading failures arise, how domain boundaries and published contracts keep many teams independent, and when synchronous calls remain the better choice.
A team says that replacing REST calls between their services with Kafka events will make the services decoupled.
Explain which kinds of coupling events remove, which remain, and which new problems the team takes on.Ten minutes after a release, the rate on the customer-events topic rises from 300 to 45,000 records per second and keeps climbing. The top keys are the same few thousand customer ids repeating.
The release made the preferences service write a marketing flag back to the customer service whenever it receivesCustomerUpdated. What is happening, how do you stop it now, and how do you prevent event loops?One shared Kafka cluster holds 9,000 topics for 70 teams in four business domains. Teams read each other's internal topics, so any schema change in one domain breaks consumers in others, and one domain's load test slowed the whole company.
Design an event architecture with clear domain boundaries, and explain what each team gains and pays.Four years into an event-driven programme, the company has 1,100 topics. For about 60 percent nobody can name all the consumers, changing an event takes three months of coordination, and incident reviews trace failures through 12 services.
Some executives want to go back to synchronous APIs. As the senior technical leader, how do you assess this and what do you change?Kafka as a source of truth
A compacted or long-retained topic can hold the authoritative history of a domain, with databases and caches built as projections from it. Understand the stream-table duality, what Kafka lacks compared with a database such as conditional writes and queries, how to bootstrap state from a topic, and how to rebuild projections from the log.
Databases and Kafka both store data, but they are built for different jobs, and some teams want Kafka itself to hold their system of record.
Explain the stream-table duality: how a table and a changelog of the same data relate. Then a team wants a compacted Kafka topic, rather than a database, to be the source of truth for its product prices. What does Kafka give them, and what does it lack compared with a database?A pricing service keeps an in-memory map of 3 million prices loaded from the compacted topic prices (24 partitions) and must not serve traffic until the map is complete.
Sketch the Java bootstrap with the Kafka consumer API, explain how it knows it has caught up, and how it tells Kubernetes it is ready.An event-sourced account service writes AccountDebited and AccountCredited events to Kafka and treats the topic as its source of truth. Two instances handled withdrawals for the same account at the same moment; both read a balance of 100 and both appended a debit of 80, leaving -60.
A reporting projection of 2 TB in PostgreSQL is built from three years of order events kept in Kafka with tiered storage, 48 partitions.
A bug corrupted part of the projection, and the business wants it rebuilt from the log within 4 hours while the current projection keeps serving reads. Plan the rebuild and estimate whether 4 hours is realistic.Scaling consumers and handling backpressure
Consumers pull at their own pace, which gives Kafka natural backpressure, but a slow downstream system, an autoscaler or a huge backlog can still overwhelm a design. Understand how to slow consumers deliberately, share rate limits, scale without rebalance storms, and plan how long a backlog takes to drain without starving fresh data.
Kafka consumers pull data instead of having it pushed to them.
Explain why this gives natural backpressure, how a Spring Kafka listener can slow itself down deliberately, and where a consumer can still run out of memory when it cannot keep up.A consumer deployment on Kubernetes autoscales with KEDA on consumer lag for a 24-partition topic, between 4 and 24 pods.
During the evening peak the replica count swings between 6 and 24 every few minutes, lag sawtooths between 50,000 and 900,000 records, and logs show a rebalance on every scale event. Explain the feedback loop and how you would stabilise it.Three consumer services read different topics and all call the same partner API, which allows 2,000 requests per second in total and returns HTTP 429 above it. Each service runs 6 to 12 pods.
During peaks the partner rejects 30 percent of calls, and the services' retries make it worse. Design how the consumers share the limit and slow down without losing records.A consumer group that loads orders into a database was down for 6 hours and has 400 million records of backlog.
Live traffic is 20,000 records per second, the consumers could process 60,000 per second, but the database sustains only 30,000 writes per second, and fraud checks downstream need fresh events within a minute. Plan the recovery and estimate how long it takes.A seasonal sales event brings eight times normal event volume for 48 hours across 90 consumer services. Last year several services fell hours behind, fraud checks ran on stale data, and teams scaled up in a panic.
As the senior engineering leader, how do you prepare the organisation and the event platform for this year?