Quick revision
The short answer
builder.stream() reads the topic as a KStream, where every record is an independent event: groupByKey().count() counts all three updates for customer 42, and the record with a null value is simply skipped by the aggregation. builder.table() reads it as a KTable, a changelog where each record replaces the previous value, so the table holds only the latest profile, and the null value is a tombstone that deletes key 42 and sends a delete downstream, so a table aggregation stops counting that customer.
What to remember
In this answer, we saw that builder.stream() reads each record as an independent event, so a stream count includes all three updates and skips the null value, while builder.table() reads the topic as a changelog, keeps only the latest profile per key in a state store and treats the null value as a tombstone that deletes the key and subtracts it from table aggregations. We choose by meaning, use compacted source topics for tables, and can let Streams reuse the source topic as the changelog with topology optimization.
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.
builder.stream() reads the topic as a KStream, where every record is an independent event: groupByKey().count() counts all three updates for customer 42, and the record with a null value is simply skipped by the aggregation. builder.table() reads it as a KTable, a changelog where each record replaces the previous value, so the table holds only the latest profile, and the null value is a tombstone that deletes key 42 and sends a delete downstream, so a table aggregation stops counting that customer.
Why this question is asked
The same topic can mean two different things depending on how we read it, and that choice silently changes every number downstream. This is the most important idea in Kafka Streams, and interviewers use exactly this kind of question to check whether we understand the stream-table duality rather than just the API. In this answer, we will recap events versus state, follow the four records through a KStream and through a KTable, look closely at what the null value does in each, decide when to use which, and finish with compaction and how a GlobalKTable reads the same topic.
1A Quick Recap: Facts and State
A Kafka topic is just a log of records. Kafka Streams lets us interpret that log in two ways. A KStream treats each record as a fact that happened: three updates are three events, like three payments. A KTable treats the log as a changelog: each record says what the current value for its key is now, replacing anything earlier, like a database table being updated row by row.
Neither view is more correct. They answer different questions, and the developer has to pick the one that matches the meaning of the data.
2Following the Records Through a KStream
Here are the two versions of the code side by side, with the records for customer 42: three updates and then a null value.
In the KStream version, each update is counted, so key 42 reaches 3. Aggregations on a KGroupedStream ignore records with a null key or a null value, so the fourth record is skipped: the count stays at 3 and nothing marks the customer as gone. A KStream has no idea that null means delete; it is just an event without a payload.
3Following the Same Records Through a KTable
In the KTable version, the first record inserts customer 42, the second and third replace the profile, and the table always holds exactly one row for 42 with its latest values. Streams keeps that table in a state store, a local key-value database, so it can look up the old value whenever a new one arrives.
The null value is a tombstone: it deletes key 42 from the table and forwards a delete to downstream operators. That is why a table aggregation behaves differently from a stream aggregation. When a row changes, Streams sends a subtract for the old value and an add for the new one, so the count per region reflects current rows. If 42 moves from EU to US, EU goes down by one and US up by one. When the tombstone arrives, only the subtract happens, and customer 42 is no longer counted anywhere.
| Record | KStream count for 42 | KTable row for 42 | KTable region count |
|---|---|---|---|
| update 1 (EU) | 1 | profile v1 | EU +1 |
| update 2 (EU) | 2 | profile v2 | EU -1 then +1 |
| update 3 (US) | 3 | profile v3 | EU -1, US +1 |
| null value | 3 (record skipped) | deleted | US -1 |
4Choosing Between Them
We choose by meaning. Use a KStream when every record matters as a fact: clicks, payments, sensor readings, where three records really are three things that happened. Use a KTable when only the latest value per key matters: customer profiles, product prices, account settings, where the third update makes the first two irrelevant. Counting profile updates with a KStream is fine if the question is how often people edit their profile; it is wrong if the question is how many customers we have.
The same topic can serve both readings at once in different applications, which is one of Kafka's strengths.
5Compaction and the Changelog
A KTable can be rebuilt by reading its source topic from the start. If that topic kept every update forever, rebuilding would read the full history. So the source topic of a KTable should be compacted: Kafka keeps at least the latest record per key and eventually removes tombstones, so a rebuild reads roughly one record per customer.
Normally Streams would also create its own changelog topic for the table's state store. With topology.optimization=all (or the specific reuse.ktable.source.topics optimization), Streams reuses the source topic as the changelog instead, which saves a full copy of the data. That is safe only if the source topic is compacted and its records are not transformed before reaching the table.
6Follow-up questions
How does a GlobalKTable read the same topic differently?
A GlobalKTable also treats the topic as a changelog with tombstones as deletes, but every application instance reads all partitions of it into a full local copy, using a dedicated global thread, instead of each task holding only its own partitions. That means joins against it need no co-partitioning and can look up any key, but each instance stores the whole table and must load it fully before processing starts. It is meant for small reference data, and updates to it do not trigger join results.
Can I turn a KStream into a KTable?
Yes, with toTable(), or by aggregating it, for example reduce((old, latest) -> latest). Both create a state store and interpret later records as updates for their key.
Why are null keys a problem for a KTable?
A table is indexed by key, so a record without a key has no row to update; Streams drops such records when building a table and records them in a dropped-records metric.
7Conclusion
In this answer, we saw that builder.stream() reads each record as an independent event, so a stream count includes all three updates and skips the null value, while builder.table() reads the topic as a changelog, keeps only the latest profile per key in a state store and treats the null value as a tombstone that deletes the key and subtracts it from table aggregations. We choose by meaning, use compacted source topics for tables, and can let Streams reuse the source topic as the changelog with topology optimization.
Open with
The difference is how each one interprets the same records. With builder.stream I get a KStream, and every record is an independent event, so groupByKey().count() counts the three updates for customer 42.
Then, if they want more
KGroupedStream aggregations ignore records with a null key or a null value, so the fourth record is skipped and the count stays at three; the stream does not understand it as a delete.
With builder.table I get a KTable, and the topic is read as a changelog: each record replaces the previous value for its key, and Streams keeps only the latest profile for 42 in a state store. There the null value is a tombstone. It deletes 42 from the table and forwards a delete downstream.
A table aggregation, for example counting customers per region, sends a subtract for the old value and an add for the new one on every update, so customer 42 counts once while it exists and then stops being counted. I choose by meaning: KStream for facts like payments or clicks, KTable for state like profiles or prices.
The source topic of a table should be compacted, and with topology optimization Streams can reuse it as the table's changelog instead of creating another topic.
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 developer reads the topic once with builder.stream("customer-profiles") and once with...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-streams-connect/cd51f9db21/