Stream state, windows, and the exactly-once claim
Lesson 7.03 gave you the choice between batch and stream and told you when nightly is the correct answer. This one is about what is actually inside the stream engine when you pick the other branch, because "we'll process it in real time" hides three problems that batch never had to solve.
State is the dividing line
Filtering, reshaping and enriching one event at a time is easy — a worker with no memory, scaled horizontally, restarted freely. The moment you count per key, join two streams, or detect an absence, the engine has to remember things between events. That memory is the system now. It has a size, it lives somewhere, it must survive a restart, and it is the reason stream processing has an operational character that stateless consumers do not.
Engines keep this state locally — in memory, spilling to an embedded store on the worker's own disk — because a network round trip per event to a shared database would set the throughput ceiling at the database. Local state is fast and it is on a machine that can vanish, which is what checkpointing exists to solve: periodically, the engine writes the state and its input positions to durable storage, together.
Two clocks, and windows built on the wrong one
Every event carries two times: when it happened (event time, stamped by the producer) and when your engine saw it (processing time). Lesson 6.06 already established that these disagree; here the disagreement has teeth, because windows are defined in one of them and you have to choose which.
A phone in a tunnel buffers forty minutes of events and delivers them at once. Windowed on processing time, all forty minutes land in one bucket at 09:15, and your hourly counts are wrong in a way no amount of scale fixes. Windowed on event time they land where they belong — but that window closed thirty minutes ago and you already emitted its result.
That is the actual problem, and it does not have a clean solution. It has a watermark: the engine's running assertion that it believes it has seen everything up to time T. Windows fire when the watermark passes their end. A watermark is a heuristic derived from what has arrived so far, not a fact about what was sent, so it is sometimes wrong and the engine has to be told what to do about that — drop late events, route them to a side output for reconciliation, or reopen the window and emit a correction downstream systems must be able to absorb.
The watermark delay is the dial the whole design turns on. Wait longer and results are more complete and less timely; wait less and they are fast and provisional. Nobody derives this number from first principles. You measure the lateness distribution you actually have, pick a percentile the business can defend, and accept that the tail past it needs a written policy rather than a shrug.
What exactly-once actually claims
Lesson 6.04 states the theorem: exactly-once delivery across a network boundary is not available. Stream engines advertise exactly-once anyway, and they are not lying — they are claiming something narrower and genuinely useful.
Inside the engine, the checkpoint commits the operator state and the input offsets atomically. Recovery rewinds both together, so the state reflects each input record exactly once no matter how many times the record is replayed. That is exactly-once processing, and it is a real property of a closed system that owns both halves.
The boundary is the sink. Writing results out is a network call to something the engine does not own, which puts you back in Lesson 6.04's world — unless the sink participates in the checkpoint through a transaction, or the write is idempotent because it is keyed (Lesson 6.07). Say the sentence carefully in a design review, because the difference between "the counts are correct" and "the counts are correct and each downstream row was written once" is where the argument actually is.
Reprocessing is a first-class operation
The logic will be wrong at some point, and fixing it means running the new code over old input. This is where the event log's retention stops being a storage setting and becomes a design parameter: you can only replay as far back as you kept. Seven days of retention means a bug older than a week can never be corrected from source, which is a decision worth making deliberately rather than inheriting from a default.
Keyed state with no expiry. A counter per user looks bounded until the key space is every user who ever visited, including the ninety percent who came once from a link in 2023 — and the state grows monotonically, on local disks, until checkpoints take longer than the checkpoint interval and the job enters a spiral it cannot recover from. Session windows on a key that never goes idle do the same thing faster. Every piece of keyed state needs a TTL decided when it is written, not when the disk fills, because by then the only remedies are dropping state you cannot rebuild or replaying a log you may not have kept.
The tradeoff sheet
- Problem solved
- Aggregations and joins that must be continuously current, where a nightly batch would answer a question hours after it stopped mattering.
- Rejected, and why
- Batch is simpler, cheaper, easier to reason about and correct for most reporting. Micro-batching every few minutes covers a large share of what people mean by real time.
- The trade
- Distributed state to operate, event-time semantics to get right, and a completeness-versus-latency dial that has no defensible default.
- First to break at 10x
- State size. At 10x the key space, checkpoints outgrow the interval between them and the job enters a spiral that only state expiry would have prevented.
- What it does to the bill
- Always-on compute sized for peak, plus checkpoint storage. Meaningfully more expensive than batch for the same volume, which is the trade being bought.
- Day-2 operations
- Watch watermark lag and checkpoint duration. Reprocessing needs the source log to still hold the data, which makes retention an operational decision rather than a default.
Going deeper
- The Dataflow Model Akidau et al., VLDB 2015
Where event time, watermarks and the completeness-versus-latency trade were set out as one framework. Every stream engine since is a variation on its vocabulary.