Kafka Streams has exactly one big idea. Everything else is API surface. The idea: a stream and a table are two readings of the same log, and you can convert between them without losing information.
The duality
A stream of keyed changes, read from the beginning and applied in order, produces a table. A table, observed as it changes, produces a stream.
STREAM (facts, append-only) TABLE (state, latest per key)
k=A v=1 A -> 1
k=B v=1 ── replay/apply ──► A -> 1, B -> 1
k=A v=2 A -> 2, B -> 1
k=B v=null A -> 2 (B deleted)
◄── observe changes ───
This is not an analogy invented for teaching. It is literally how Streams stores state: the table lives in a local store, and its durable form is a Kafka topic of the changes.
The three abstractions
| Abstraction | Reads each record as | Keyed lookups | Partitioning | Typical source |
|---|---|---|---|---|
KStream | An independent fact ("order placed") | No | One instance owns some partitions | Event topic, cleanup.policy=delete |
KTable | An upsert for that key; null value means delete | Yes, but only for keys in the partitions this instance owns | Sharded across instances | Changelog topic, cleanup.policy=compact |
GlobalKTable | An upsert, replicated in full to every instance | Yes, for every key | Every instance holds all partitions | Small, slow-changing reference data |
The distinction between KStream and KTable is not about the topic; it is about the meaning you assign to a record with a repeated key.
Two records: k=account-7 v=100 then k=account-7 v=70
As a KStream: two separate facts. Sum = 170.
As a KTable: the second replaces the first. Value = 70.
Choose wrong and your numbers are wrong, with no error anywhere. "We double-counted" and "we lost history" are the two symptoms.
Why a table must be backed by a compacted topic
A table has to be rebuildable. When an instance restarts or a partition moves to another instance, the new owner reconstructs the table by replaying its backing topic from offset 0.
That only works if the topic still contains the latest value for every key that has ever existed. Time-based retention breaks exactly this:
retention.ms = 1 day, cleanup.policy = delete
day 1: k=sku-1 v=inStock ← segment aged out and deleted
day 5: k=sku-2 v=inStock
rebuild on day 6 → sku-1 is simply missing from the table
Compaction is the fix: it removes superseded values rather than old values, so the newest record per key survives indefinitely, however long ago it was written.
compacted changelog
k=sku-1 v=inStock ← 4 years old, still retained (no newer value exists)
k=sku-2 v=outOfStock
k=sku-3 v=null ← tombstone: delete, retained for delete.retention.ms
Two practical consequences:
- A
KTableover a non-compacted topic is a latent correctness bug. It looks fine until the first restart after the retention window passes. - Tombstones are how deletion is expressed. A
nullvalue on a table source removes the key. Producers writingnullto mean "no data yet" will silently delete rows.
Internal state stores that Streams creates for you are compacted automatically. The trap is with topics you create and then read with builder.table(...).
Reading a KTable at scale: it is sharded
A KTable is partitioned the same way its source topic is. Instance 1 holds the keys in the partitions it owns and knows nothing about the others.
customer-profiles, 6 partitions, 3 instances
instance 1: p0 p1 ← holds only these keys
instance 2: p2 p3
instance 3: p4 p5
For a join this is fine, because the stream side is partitioned identically (see the co-partitioning rule later in this module). For serving external lookups it means a request may arrive at an instance that does not own the key, which is what interactive queries plus a metadata lookup are for.
GlobalKTable: convenience with a bill attached
A GlobalKTable loads all partitions into every instance. That buys you two things: lookups by any key, and joins that do not require co-partitioning — so you can join a stream keyed by orderId against reference data keyed by countryCode.
The costs are real:
- Every instance stores the whole dataset. Ten instances means ten full copies of local disk and memory.
- It is loaded by a dedicated consumer that is not part of the task's time-driven processing, so updates are applied when they arrive rather than being aligned with the stream's event time. Do not use a
GlobalKTablewhere the join result depends on which version of the reference row was current at event time.
Rule of thumb: GlobalKTable for small, slow-moving lookup data (currency codes, feature flags, product categories). KTable for anything that is large, high-cardinality, or where matching the right version in time matters.
Choosing between them
| Requirement | Use | Why |
|---|---|---|
| Every occurrence matters (clicks, payments, sensor readings) | KStream | Repeated keys are separate facts, not corrections |
| Latest value per entity (profile, inventory level, config) | KTable | Repeated keys are upserts; needs a compacted source |
| Aggregate result you will keep querying | KTable (via aggregation) | Aggregations naturally produce latest-value-per-key |
| Small reference data joined on a non-matching key | GlobalKTable | Full replication removes the co-partitioning constraint |
| Large dimension table joined on the same key | KTable | Sharded storage; full copies would not fit |
| Audit history you must never collapse | KStream on a delete topic with long retention | Compaction would destroy the history |
The mistake to avoid
Turning an event stream into a compacted table because "the topic was getting big". Compaction assumes later records supersede earlier ones. For events they do not: Deposited(50) does not replace Withdrawn(20). If storage is the problem, shorten retention or archive to object storage. Compaction is for state, not for events.
Exercise: A team models shopping-cart activity as a topic cart-events keyed by cart_id, where each record is a full snapshot of the cart, with cleanup.policy=delete and 3 days of retention. One job reads it as a KStream to count "carts updated per hour"; another reads it as a KTable to serve "current cart contents". Say which job is modelling the data correctly, what will go wrong with the other one and when it will first be noticed, and what topic configuration would let both jobs be correct.