Quick revision
The short answer
A table is the result of applying a changelog in order, and a changelog can be produced from a table by capturing every change; a compacted topic keyed by product id is a changelog that keeps at least the latest value per key. Kafka gives durable, replicated, ordered history per key, cheap fan-out, and lets any new reader build its own table by reading from the beginning. It lacks queries by key or anything other than offset, secondary indexes and ad hoc queries, and conditional writes, so it cannot enforce invariants without a single writer; compaction also removes history over time and deletes need tombstones, so the usual design is one validating writer with readers building local views.
What to remember
In this answer, we saw the stream-table duality: a table is the result of applying a changelog in order, and a compacted topic keyed by product id is a changelog that keeps the latest value per key. Kafka gives durable ordered history, cheap fan-out and the ability for any reader to build its own table. It lacks lookups, secondary indexes, ad hoc queries and conditional writes, compaction removes history and deletes need tombstones, so the usual design is a single validating writer, often backed by a database, with readers building local views.
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.
A table is the result of applying a changelog in order, and a changelog can be produced from a table by capturing every change; a compacted topic keyed by product id is a changelog that keeps at least the latest value per key. Kafka gives durable, replicated, ordered history per key, cheap fan-out, and lets any new reader build its own table by reading from the beginning. It lacks queries by key or anything other than offset, secondary indexes and ad hoc queries, and conditional writes, so it cannot enforce invariants without a single writer; compaction also removes history over time and deletes need tombstones, so the usual design is one validating writer with readers building local views.
Why this question is asked
Some teams look at Kafka's durability and replication and conclude that it can replace their database. The stream-table duality is the idea that makes that thought reasonable, and also the idea that shows its limits. Interviewers ask this question to see whether we understand both sides of the duality and can say exactly what a database does that a log does not. In this answer, we will explain the duality with a small example, see how compaction turns a topic into a changelog, list what Kafka gives a team that keeps prices there, and then look closely at what it lacks and the design that compensates.
1The Stream-Table Duality
A table holds the current state: one row per product with its current price. A changelog, or stream of changes, holds every change in order: product 7 was set to 10.00, then product 9 to 4.50, then product 7 to 12.00. The duality says these are two views of the same data. If we start with an empty table and apply every change in order, we get the table. If we watch a table and record every change, we get the changelog. Databases do this internally: their write-ahead log is a changelog, and their tables are what replaying it produces.
Notice that the order matters within a key: applying offset 0 after offset 2 would leave the wrong price. Kafka guarantees order within a partition, and keying by product id puts all changes to one product in the same partition, so every reader replays them in the right order.
2How Compaction Makes a Topic a Changelog
A normal topic deletes old segments by time or size. A compacted topic, with cleanup.policy=compact, instead keeps at least the latest record for every key, and a background cleaner removes older records for keys that have a newer value. After compaction, the topic above would keep offset 2 for key 7 and the tombstone at offset 3 for key 9, until the tombstone itself is removed after delete.retention.ms (one day by default).
So the topic size stays proportional to the number of products, not to the number of changes, and a reader that starts from the beginning ends up with the complete current table. That is what makes the idea of a topic as a source of truth plausible.
- Producer writes price changeskeyed by product id
- Partition keeps them in order
- Compaction keeps latest per keyolder values removed over time
- New reader starts at offset 0
- Applies each record to a local mapnull value removes the key
- Reader holds the current table
6 steps in order. Play them, or step through them one at a time.
3What Kafka Gives the Team
Kafka gives durable, replicated storage: with replication factor 3 and acks=all, an acknowledged price change survives a broker failure. It gives an ordered history per key, so every reader sees each product's changes in the same order. It gives cheap fan-out: the pricing API, the search index, the recommendation service and an analytics job can all read the same topic without adding load to a database, each at its own pace.
Most usefully, a new reader can build its own table at any time by reading from the beginning, in whatever shape it needs: the search service builds a search index, the cart service an in-memory map, the analytics job a warehouse table. With a database as the source, each of them would need its own export or change data capture.
4What Kafka Lacks Compared With a Database
Kafka can only read by position. There is no SELECT price WHERE product_id = 7; to answer that question, a reader must consume the topic into a local store and look it up there. There are no secondary indexes, so no query for all products in category shoes under 50.00, and no ad hoc queries for an analyst at all. Every query pattern requires building and maintaining a view.
Kafka also cannot do conditional writes. A database can update a price only if the row's version is still 41, which lets two editors avoid overwriting each other, or enforce a rule such as a discount may not reduce the price by more than 30 percent of the current price. Kafka appends whatever producers send. Two services that each read the current price and write a new one will both succeed, and the later write silently wins.
History is also limited. Compaction removes older values over time, so the topic is not an audit trail of every price ever set unless we keep a second, non-compacted topic. And deletes must be written as tombstones, records with a null value, which readers must handle and which disappear after delete.retention.ms; a reader that is offline longer than that can miss a delete.
| Capability | Compacted topic | Database |
|---|---|---|
| Durable, replicated storage | Yes | Yes |
| Fan-out to many readers | Cheap | Load on the database |
| Lookup by key | Only via a local view | Yes, indexed |
| Secondary indexes, ad hoc queries | No | Yes |
| Conditional write on version | No | Yes |
| Full history | Latest per key after compaction | Only if designed |
5The Usual Design: One Writer, Many Views
Because Kafka cannot check conditions on write, the team needs a single writer: one pricing service that receives price-change commands, validates them against the current state it holds, and publishes the accepted changes to the compacted topic. If commands are keyed by product id and consumed by one instance per partition, each product's changes are validated one at a time, which gives the effect of a conditional write. Every other service only reads the topic and builds its own view.
Many teams go one step further and keep the prices in a database, with the compacted topic fed from it through an outbox or change data capture. The database handles queries and conditional updates, and Kafka handles distribution. Whether Kafka alone is enough depends on whether the single writer is acceptable and whether the readers' query needs can be met by local views.
6Follow-up questions
Can a reader look up one key in a compacted topic without reading everything?
Not directly. It can compute the partition from the key, but it must still read that whole partition. Kafka Streams' interactive queries give key lookups by serving them from the state stores built from the topic, which is the local-view approach packaged for us.
What happens if two services write prices to the same topic?
Both writes are appended and the later offset wins for that key, regardless of what each writer saw. There is no conflict detection, which is why a single writer or a database in front is needed.
How long are tombstones kept?
At least delete.retention.ms after compaction reaches them, 24 hours by default. A reader that starts from the beginning after that will simply never see the key, which is correct, but a reader whose local view is older and that was offline longer could miss the delete.
7Conclusion
In this answer, we saw the stream-table duality: a table is the result of applying a changelog in order, and a compacted topic keyed by product id is a changelog that keeps the latest value per key. Kafka gives durable ordered history, cheap fan-out and the ability for any reader to build its own table. It lacks lookups, secondary indexes, ad hoc queries and conditional writes, compaction removes history and deletes need tombstones, so the usual design is a single validating writer, often backed by a database, with readers building local views.
Open with
The stream-table duality says that a table and a changelog are two views of the same data. Apply every change in order and you get the table; record every change to a table and you get the changelog.
Then, if they want more
A compacted topic keyed by product id is that changelog, and compaction keeps at least the latest price per key, so its size tracks the number of products, not the number of changes.
For prices, Kafka gives durable, replicated, ordered history per key, cheap fan-out to many readers, and the ability for any new reader to build its own table by reading from the beginning.
What it lacks compared with a database is querying: you can only read by offset, so a lookup by product id needs a local store built from the topic, and there are no secondary indexes or ad hoc queries.
It also has no conditional writes, so it cannot append only if the current version is still what the writer saw, which means it cannot enforce rules on price changes unless there is a single writer. And compaction removes history over time, while deletes need tombstones that are themselves removed later.
So I would design it with one pricing service that validates and publishes changes, or a database with an outbox feeding the topic, and readers building their own local views.
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 "Explain the stream-table duality: how a table and a changelog of the same data relate....": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/e869999690/