Quick revision
The short answer
Model the order lines as a KTable keyed by orderLineId and enrich them with two foreign-key joins, first to customers on customerId and then to products on productId, because only a table-table join re-emits a result when the customer changes. A foreign-key join repartitions through its own internal topics, so none of the three inputs has to be re-keyed or co-partitioned. Products may be a GlobalKTable if a product change need not update old results; customers, at 30 million rows, stay a partitioned KTable.
What to remember
In this answer, we saw that the requirement to follow tier changes rules out stream-table joins, because they never emit when the table changes. Modelling order lines as a KTable and using two foreign-key joins gives results that update on either side, without re-keying or co-partitioning, at the cost of subscription stores, internal topics and fan-out bursts. A GlobalKTable suits products only if old results may keep an old category, while customers are better partitioned. We also bounded the order-line state with tombstones and wrote contracts with the teams that own the inputs.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Model the order lines as a KTable keyed by orderLineId and enrich them with two foreign-key joins, first to customers on customerId and then to products on productId, because only a table-table join re-emits a result when the customer changes. A foreign-key join repartitions through its own internal topics, so none of the three inputs has to be re-keyed or co-partitioned. Products may be a GlobalKTable if a product change need not update old results; customers, at 30 million rows, stay a partitioned KTable.
Background to this question
Enrichment is the most common thing a Kafka Streams application does, and it is also where most designs quietly go wrong: the join looks right in a test, then a customer upgrades to gold and nothing downstream changes, or every instance runs out of disk because a table nobody sized keeps growing. The right answer depends on reading the requirement carefully (results must update when the customer changes) and knowing what each join type costs. In this answer, we will recap the join types Kafka Streams offers, see why the obvious stream-table join fails the requirement, build the topology with foreign-key joins, look inside a foreign-key join, decide where a GlobalKTable fits, size the state, and finish with the contracts we need from the teams that own the input topics.
1A Quick Recap of Streams, Tables and Joins
Kafka Streams gives us two main views of a topic. A KStream treats each record as an independent event: an order line was created. A KTable treats each record as the latest value for its key, an upsert, and a record with a null value (a tombstone) deletes the key. A GlobalKTable is a KTable whose full contents are copied to every application instance instead of being split by partition.
The joins between them behave differently. A KStream-KTable join is a lookup: when a stream record arrives, Streams looks up the current table value and emits a joined record. When the table changes, nothing is emitted. A KTable-KTable join is symmetric: a change on either side produces an updated result. Ordinary joins require co-partitioning, which means both inputs use the same key and the same number of partitions, so that matching keys sit in the same task. Our inputs are keyed by orderLineId, customerId and productId, so they are not co-partitioned on anything useful.
| Option | Emits when customer changes? | Cost |
|---|---|---|
Re-key lines by customerId, stream-table join | No | Repartition topic, lookup only |
| Lines as KTable, foreign-key join | Yes, fans out to every line | Two internal topics and stores per join |
| Stream join to GlobalKTable | No | Full copy of the table on every instance |
2Why a Stream-Table Join Misses Tier Changes
The first design most people sketch re-keys the order-line stream by customerId with selectKey, joins it to the customers table, re-keys again by productId and joins to products. That works at the moment the line arrives. But the requirement says the result must update when a customer's tier changes. With a stream-table join, a tier change only updates the table; no order line passes through the join again, so the enriched output keeps the old tier forever.
So the order lines must be on the table side. That is reasonable here: each line has a stable key, orderLineId, and a later record for the same line replaces the earlier one. Once order lines are a KTable, joining them to customers by a field inside the value is exactly what a foreign-key join is for.
3Building the Topology With Two Foreign-Key Joins
A foreign-key join takes the left table, the right table and a function that extracts the right table's key from the left value. Here is the whole topology.
Notice that the result is still keyed by orderLineId, so the output topic can be compacted and consumers can treat it as the latest enriched view of each line. Notice also that we never called selectKey or repartition. The 48-partition order-lines topic can be joined to a customers topic with, say, 24 partitions, because the foreign-key join handles the routing itself. If a line's customerId is null, an inner join drops it; leftJoin keeps it with a null tier, which is usually what we want for data that arrives out of order.
4What Happens Inside a Foreign-Key Join
Knowing the mechanics helps us size and operate it. When an order line arrives or changes, the left task extracts customerId and sends a subscription record to an internal subscription topic, keyed by customerId, so it lands on the task that owns that customer. The right side stores the subscription (customerId plus orderLineId) in a subscription store and answers with the current customer value through a response topic keyed by orderLineId, back to the task that owns the line. That task checks a hash of the line value it sent, so a stale response for an old version of the line is discarded, and then emits the joined result.
When a customer changes, the right task scans its subscription store for every orderLineId subscribed to that customerId and sends a response for each. This is the fan-out that makes results follow tier changes, and it is also the cost: a customer with 2 million order lines produces 2 million responses and 2 million output records. At a normal 20,000 records per second, that one update is about 100 seconds of extra traffic, so lag rises briefly and we must size for it.
7 messages in order. Play them, or step through them one at a time.
Subscriptions travel by the foreign key; responses travel back by the primary key.
5Where a GlobalKTable Fits
A GlobalKTable needs no repartitioning at all, because every instance has the whole table. Products, at 5 million rows of perhaps 300 bytes, are about 1.5 GB per instance, which is acceptable. But a join against a GlobalKTable is a lookup from a stream: Streams has no KTable-to-GlobalKTable join, so we would turn the customer-joined table into a stream with toStream() and join that stream to the global products table. A product change would then not update old results; the category would be picked up only the next time the line or its customer changes. If the business accepts that, the global table saves one foreign-key join and its two internal topics.
Customers are a different case. Thirty million rows at 300 bytes is about 9 GB, copied to every instance and read in full from the topic whenever an instance starts with an empty disk. And we need customer changes to re-trigger results, which a global table cannot do. A partitioned KTable lets each of, say, 12 instances hold about one twelfth of it.
6Sizing the State and Keeping It Bounded
The part teams forget is that order lines now live in a table, and a table keeps every key until it receives a tombstone. At 20,000 records per second, even if each line is updated three times, that is over 500 million new lines a day. Without a cut-off the store grows without limit. So we agree a business rule, such as removing a line 30 days after its order closes, and someone must send the tombstones: the order service, or a small job in our app that schedules them with a punctuator. A tombstone on the left side also removes the line's subscription on the right.
Each foreign-key join adds a subscription store with its changelog and two internal topics (subscription and response), so two joins mean four extra topics. Disk per instance is the left table share, the customers share and the subscription stores plus RocksDB overhead, and we keep standby replicas (num.standby.replicas=1) so a failed instance does not have to restore many gigabytes from changelogs before it can serve.
7Contracts With the Owning Teams
Our topology depends on topics other teams own, so the design is incomplete without agreements. The customers and products topics must keep stable keys and serdes, stay compacted so we can rebuild from them, and the owners must give us notice before changing partition counts or key formats, since a change of key format silently breaks every lookup. We should also prefer topics they publish for consumers, with a documented schema, over their raw CDC topics, whose shape follows their database tables and changes whenever they refactor.
8Follow-up questions
What happens to throughput when a customer with 2 million order lines changes tier?
The customer task scans its subscription store and emits 2 million response records, the line tasks emit 2 million enriched records, and the output topic receives a burst of the same size. At a normal rate of 20,000 records per second this is roughly 100 seconds of extra work concentrated on the tasks that own that customer and its lines, so end-to-end latency for unrelated lines rises during the burst. We size instances and output partitions for such bursts, monitor lag, and if a few customers have enormous fan-out we can consider whether the tier really belongs on every line or should be looked up at read time.
Do the customers and order-lines topics need the same number of partitions for a foreign-key join?
No. The subscription topic is partitioned to match the right table, so subscriptions reach the task that owns the customer, and the response topic is partitioned to match the left table. That is the main operational advantage over re-keying with a regular join, which requires both sides to have the same partition count.
9Conclusion
In this answer, we saw that the requirement to follow tier changes rules out stream-table joins, because they never emit when the table changes. Modelling order lines as a KTable and using two foreign-key joins gives results that update on either side, without re-keying or co-partitioning, at the cost of subscription stores, internal topics and fan-out bursts. A GlobalKTable suits products only if old results may keep an old category, while customers are better partitioned. We also bounded the order-line state with tombstones and wrote contracts with the teams that own the inputs.
Open with
I would start from the requirement that results must change when a customer's tier changes. A stream-table join only looks up the table when a stream record arrives, so a tier change never reaches the output.
Then, if they want more
That means order lines have to be a KTable keyed by orderLineId, and I would join them to customers with a foreign-key join, extracting customerId from the value, and then do a second foreign-key join to products on productId.
A foreign-key join routes subscription records through an internal topic keyed by the foreign key and sends responses back by the primary key, so I do not need to re-key anything or match partition counts, and a customer update fans out to every line that references it.
Products could be a GlobalKTable if a category change does not need to update old lines, but customers at 30 million rows I would keep partitioned so each instance holds only its share.
The big operational risk is state: order lines in a table live forever unless we tombstone them after a business cut-off, and each foreign-key join adds stores and two internal topics, so I would size disk per instance and use standby replicas.
Finally I would agree contracts with the owning teams on keys, serdes, compaction and notice before partition changes, and join against topics they publish for consumers rather than raw CDC.
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 "You must produce enriched order lines with the current customer tier and product...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-streams-connect/928ead41f1/