Aggregating an unbounded stream forever is rarely what a business question asks. "Orders per hour", "failed logins in the last five minutes", "did a payment arrive within 30 minutes of the order" all need a notion of time — and in a distributed system, time is the hardest part.
Three clocks
| Time | Meaning | Where it comes from |
|---|---|---|
| Event time | When the thing actually happened | The record's timestamp, set by the producer or extracted from the payload |
| Ingestion time | When the broker appended the record | Broker-assigned timestamp (a topic-level configuration choice) |
| Processing time | When your operator saw the record | The instance's wall clock |
Event time is the only one that gives stable, reproducible results. Reprocess yesterday's data with processing-time windows and every record lands in "now", producing completely different output. Reprocess with event-time windows and you get the same answer you got yesterday. Replayability requires event time.
Streams uses the record timestamp by default; a custom TimestampExtractor lets you pull the timestamp out of the payload when the producer's clock is not the authority. Beware the extractor that returns processing time on failure — it turns a data problem into a silently wrong result.
Stream time does not follow the wall clock
This one surprises everyone. Streams tracks stream time: the highest event timestamp it has observed so far for a task. Stream time advances only when records arrive.
Records stop arriving at 10:00 for a 1-hour tumbling window [09:00, 10:00)
wall clock moves to 11:30
stream time stays at 10:00
→ the window is not considered closed, final results are not emitted
The practical symptom is a low-volume or idle partition whose window results appear late or not at all, while high-volume partitions look fine. Understanding this saves hours: the fix is not "restart it", it is either accepting that results emit when the next record arrives, or driving progress explicitly (for example with the idle-partition handling and punctuation mechanisms the API provides).
Window types
TUMBLING (size 10, no overlap) — every record in exactly one window
|--------|--------|--------|
0 10 20 30
HOPPING (size 10, advance 5) — windows overlap, records counted more than once
|--------|
|--------|
|--------|
0 5 10 15 20
SESSION (inactivity gap 5) — window boundaries derived from the data
events: x x x x x x
windows: [-----] [---] [-]
gap > 5 closes the session
| Window | Defined by | Answers | Cost note |
|---|---|---|---|
| Tumbling | Fixed size | "Per hour", "per minute" | One window per record; cheapest |
| Hopping | Size plus advance | "Rolling 1-hour count, updated every 5 minutes" | A record belongs to size/advance windows, so state and output multiply |
| Sliding | Size, window per record pair difference | "Any two events within 10 minutes" | Precise, more windows |
| Session | Inactivity gap | "User activity bursts", "call sessions" | Windows merge as data arrives; variable size |
Hopping windows are where storage estimates go wrong. Size 1 hour advancing every minute means each record is in 60 windows.
Late records and grace
Records arrive out of order — a mobile client was offline, a producer retried, one partition was slow. A window therefore cannot close the instant stream time passes its end.
window [09:00, 10:00), grace 10 min
stream time 09:58 record at 09:30 → accepted, window updated
stream time 10:05 record at 09:45 → accepted (within grace)
stream time 10:15 record at 09:50 → window CLOSED, record dropped
Grace is a direct trade: longer grace tolerates more lateness but delays final results and keeps more state. Choose it from measured lateness in your data, not from a guess, and monitor the dropped-records metric — silently discarded late data is a favourite source of "the numbers do not reconcile".
Separately, window stores have their own retention. It must cover window size plus grace, otherwise you lose the window before it can be finalised.
Joins
Three shapes, and the differences are semantic, not syntactic.
| Join | Windowed? | Trigger | Mental model |
|---|---|---|---|
| Stream-stream | Required | A record on either side looks for matches on the other within the window | Correlating two event flows |
| Stream-table | No window | Only the stream record triggers output; the table is a lookup | Enrichment |
| Table-table | No window | Either side triggers; result is a table | Maintaining a joined view |
// stream-stream: did a payment follow the order within 30 minutes?
orders.join(
payments,
(order, payment) -> new Settled(order, payment),
JoinWindows.ofTimeDifferenceAndGrace(Duration.ofMinutes(30), Duration.ofMinutes(5)));
// stream-table: enrich each order with the customer's current profile
orders.join(customers, (order, customer) -> enrich(order, customer));
A stream-stream join is stateful in both directions: each side buffers records for the window duration so a later record on the other side can still match. Double the window, roughly double the state.
A stream-table join is asymmetric on purpose. Updating the table does not emit a joined record; only the stream side does. That is what you want for enrichment ("attach the profile as it was known when the order arrived") and what you do not want if your requirement is "re-emit everything when the profile changes" — that is a table-table join.
Left and outer variants exist for all three, and for stream-stream joins the non-matching side is only emitted once the window closes. If your outer join "produces nothing", stream time has probably not advanced past the window plus grace.
Co-partitioning: the requirement that fails your deploy
For any join except one against a GlobalKTable, matching keys must be in the same partition number on both sides. Streams assigns partition n of both inputs to the same task, so if a matching pair lands in different partitions, no task ever sees both records — the join silently produces nothing.
Three conditions must hold:
1. Same join key on both sides (re-key first if not)
2. Same number of partitions on both input topics
3. Same partitioning strategy (default partitioner on both sides,
or the same custom one)
Not co-partitioned:
orders (4 partitions): key "cust-9" → p1
customers (6 partitions): key "cust-9" → p3
task 1 owns orders-p1 + customers-p1 → holds the order, not the customer
task 3 owns orders-p3 + customers-p3 → holds the customer, not the order
result: no match, no output, no error from your code
Streams checks partition counts when it builds the topology and fails at startup rather than producing wrong results, which is the behaviour you want — a loud failure on deploy beats a quiet one in production. Condition 3 is the nastier case, because a custom partitioner on the producer side cannot be detected: the counts match, the topology starts, and the join just misses records.
How to satisfy it:
- Create topics that will be joined with the same partition count from the start. This is the real fix, and it is a design-time decision.
- Re-key and let Streams repartition, which writes to an internal topic with a matching partition count. Costs an extra write and read.
- Use a
GlobalKTableif the table side is small — full replication removes the constraint entirely.
Remember the partition-count trap from Module 2 applies here too: changing the partition count of one side of a join breaks the key-to-partition mapping and the co-partitioning agreement at the same time.
Exactly-once, restated for Streams
processing.guarantee=exactly_once_v2 uses Kafka transactions to make the output records, the state changelog updates, and the input offset commits atomic. It genuinely removes duplicate and lost internal processing effects, and costs some throughput and latency because results are committed in transactional batches.
It says nothing about external side effects. A foreach that calls a payment API can still run twice. The rule from Module 3 stands: Kafka-to-Kafka can be made effectively-once; anything leaving Kafka needs an idempotent sink.
Exercise: A fraud team joins card-transactions (12 partitions, keyed by card_id) with device-logins (12 partitions, keyed by user_id) using a 15-minute stream-stream join, and reports that the job runs cleanly but almost never emits a match. A second complaint is that on quiet overnight hours the hourly aggregation results appear hours late. Diagnose both, state which condition of co-partitioning is violated and how you would fix it, and explain the overnight behaviour in terms of stream time.