Stage 5 · Architecture patterns · Hard · 25 min
Stream and batch processing
Process data as it arrives, in time windows, or in large batches later, and handle late and repeated events correctly.
What each interview level is expected to know
Learn first: Message queues and Kafka
After this lesson you can:
- Count events by the time they happened, using windows and watermarks.
- Decide how long to wait for late events, and what that wait costs.
- Avoid counting a retried event twice, and say when a batch recount helps.
Start here
See it in 60 seconds
A click that arrives late
A stream counts events by when they happened, so it must decide how long to wait for late ones before a window's count is final.
Scene 1 of 4 · Windows of 10 seconds
A counter adds up ad clicks in 10-second windows of event time: 0–10 s, then 10–20 s.
Click happened
- The late click arrives at 8 s, in a tunnel
- A click arrives on time at 9 s
Arrived at the counter
- A click arrives on time at 9.1 s
- Window 0–10 s count
- 1
- Clicks that happened in it
- 2
- Windows of 10 seconds. A counter adds up ad clicks in 10-second windows of event time: 0–10 s, then 10–20 s.
- A phone in a tunnel. One click happens at 8 s, but its phone is in a tunnel, so it arrives only at 14 s.
- The window closes. With no waiting, the click at 11 s moves the watermark past 10 s, and the 0–10 s window closes.
- Too late. The 8 s click arrives at 14 s, after its window closed, so it is dropped: the count stays 1.
- Now you try. Choose how long the counter waits past a window's end before closing it. For example: The late click is counted, so the 0–10 s window counts 2 of 2. Its count is final at 16.1 s. Waiting kept the window open long enough for the straggler, but its answer comes later.
Scene 1 of 4: Windows of 10 seconds. A counter adds up ad clicks in 10-second windows of event time: 0–10 s, then 10–20 s.
Key points
- Every click is stamped with the moment it happened: its event time.
- Events can arrive out of order, and some arrive late.
- Once a window closes, a late event for it is dropped.
- Waiting longer counts more, but every answer comes later.
In plain words
Say an ad system counts clicks in windows of 10 seconds. A user taps an ad at 8 s while their phone is in a tunnel, so the click reaches the counter at 14 s. It belongs in the 0–10 s window, and that window may already be closed. This is the hard part of stream processing: handling each event as it arrives, when events can arrive late or twice. Each event carries the time it happened, called its (The time an event really happened, stamped by the device that made it, as opposed to the time it reached the server.). Batch processing avoids the problem by running later, over stored data such as last night's logs.
A stream job handles that late click in steps. It puts each click in the window for its event time, not the time it arrived. It keeps a window open a little past its end, such as 5 seconds, for late events. After that it closes the window and writes the count. A click that comes even later is dropped, or sent to a job that fixes old counts, depending on the design.
The problem it solves
Events arrive out of order, late and sometimes twice, so counting them by arrival time gives wrong totals. Waiting for every straggler makes results late; not waiting drops them.
Why it matters
An ad network bills by the clicks in each window of time. A phone in a tunnel sends one click 6 seconds late. Is it counted, and in which window? Every system that counts events as they arrive must answer this.
A worked example
Windows are 10 seconds long, and the stream waits 5 seconds for stragglers. A click at 8 s from a phone in a tunnel arrives at 14 s. The newest click seen so far happened at 13 s. So the (In stream processing, the newest event time seen minus the allowed lateness. A window that ends before the watermark is closed, and its count is final.), the point the stream treats as done, is only at 13 − 5 = 8 s. The 0–10 s window is still open, and the click is counted.
Think of it like…
Think of counting votes. A batch count waits until the doors close, then counts every ballot. A stream count gives a running total each hour, and postal votes that arrive late miss their hour.
Where this analogy stops working
Waiting for everything, or answering early and missing some, is the useful part. But a ballot carries no time, and is counted once. An event carries the time it happened, and may be sent twice. So a stream sorts events by their own time, guesses when an hour is complete, and skips repeats.
Try it
See it working
Also try: Batch jobs: map, shuffle, reduce · Kafka: partitions, replication & delivery
Mechanics
How it works
The pictures follow this page's example: clicks counted in 10-second windows. One click comes from a phone in a tunnel: it is stamped 8 s and arrives at 14 s.
Stamp the event time
Each event carries the time it happened, set by the device or service that made it. The click in the tunnel is stamped 8 s, though it arrives at 14 s. Counting by event time puts it in the window where it belongs.
Put it in a window
Pick a case to follow belowWith this case: By arrival time, the tunnel click landed in 10–20 s: 2 clicks and 2, both wrong.
Phones, send clicks Phones send clicks Stream counter, 10-second windows, NOW Stream counter 10-second windows 0–10 s: 2 NOW Published counts, final per window Published counts final per window Set aside, late clicks Set aside late clicks - Phones — send clicks — healthy
- Stream counter — 10-second windows — healthy — NOW — 0–10 s: 2
- Published counts — final per window — healthy
- Set aside — late clicks — healthy
- Click
Step 1 of 3Step 1 of 3 · Stream counter
Done: Clicks stamped 3 s and 9 s arrive on time
- What happens
- Both arrive the moment they happen, so either clock puts them in 0–10 s.
- Why
- Each click carries its event time: when the user tapped.
- What changes
- The 0–10 s window holds 2 clicks.
- Next
- A click stamped 11 s arrives at 11 s
0–10 s window2clicks10–20 s window0clicksStep 1 of 3: Stream counter. Clicks stamped 3 s and 9 s arrive on time. Both arrive the moment they happen, so either clock puts them in 0–10 s.
The counter adds each event to the window that covers its event time. With 10-second windows, a click stamped 8 s goes into 0–10 s, and one stamped 11 s goes into 10–20 s. Windows like these, back to back with no gap or overlap, are called tumbling windows. Sliding windows overlap, and a (A window that holds one user's events and closes once that user has been quiet for a set time, such as 30 minutes.) ends when a user goes quiet.
In the picture, clicks stamped 3 s, 9 s and 11 s arrive on time. Counting by arrival time puts the tunnel click in 10–20 s, so both windows are wrong. Counting by event time puts it in 0–10 s, where it happened.
Move the watermark
The watermark is the newest event time seen so far, minus the (How long a stream waits past a window's end for late events, before treating the window as closed.). It marks the point up to which the stream treats its data as complete. With an allowed lateness of 5 seconds, a click stamped 13 s puts the watermark at 8 s.
Close and publish
Pick a case to follow belowWith this case: With no allowed lateness, the window closed at 13 s with 2 clicks, and the tunnel click was set aside.
Phones, send clicks Phones send clicks Stream counter, 10-second windows, NOW Stream counter 10-second windows mark 9 s NOW Published counts, final per window Published counts final per window Set aside, late clicks Set aside late clicks - Phones — send clicks — healthy
- Stream counter — 10-second windows — healthy — NOW — mark 9 s
- Published counts — final per window — healthy
- Set aside — late clicks — healthy
- Click
- Published count
- Late click
Step 1 of 3Step 1 of 3 · Stream counter
Done: Clicks stamped 3 s and 9 s arrive
- What happens
- Both go into 0–10 s. The newest stamp is 9 s, so the watermark is 9 − 0 = 9 s.
- Why
- The watermark says how far the stream treats its data as complete.
- What changes
- The window stays open while the watermark is below 10 s.
- Next
- A click stamped 13 s puts the watermark at 13 s
Watermark9 snewest stamp − 0 s0–10 s count20–10 s published–Set aside as late0noneStep 1 of 3: Stream counter. Clicks stamped 3 s and 9 s arrive. Both go into 0–10 s. The newest stamp is 9 s, so the watermark is 9 − 0 = 9 s.
What these pictures simplify. These pictures use a few stated clicks and whole seconds. Real streams handle many clicks a second. Some engines can also publish a correction when a late click arrives for a closed window. The stream windows lab above runs phones that go offline and retry.
Once the watermark passes the end of a window, that window's count is final and is published. An event for it that arrives afterwards is late. The stream drops it, or sets it aside for a later correction.
In the picture, the third click is stamped 13 s. With no allowed lateness, the watermark reaches 13 s, and the 0–10 s window publishes 2 at 13 s. The click stamped 8 s then arrives late and is set aside.
With 5 seconds of allowed lateness, the window waits and counts it. A click stamped 16 s then closes the window, which publishes 3 at 16 s. The count is right, and 3 seconds later.
Batch: count again later
A batch job runs later over all the stored events, such as once a night. By then the late clicks have arrived, so its count is complete. A team can use the stream for a fast answer and the batch for the final one.
Use it when
- Counting events per minute for a dashboard or an alert: a stream gives the number while it still matters.
- Fraud checks that must react within seconds: a batch job that runs at night would be hours too late.
- Reports that need every event, however late, such as a monthly bill: a batch job runs after the stragglers have arrived.
Skip it when
- One-off questions about old data: run a batch query instead.
- Small volumes: one database query each minute gives the same count with nothing new to run.
Check yourself
Judgement
Trade-offs and how it fails
What you give up
- Accuracy
- A longer allowed lateness counts more late events and drops fewer.
- Delay
- The same lateness makes every result arrive that much later.
- Memory
- Every open window is held in memory until it closes. A longer allowed lateness, or a window per user, means more windows open at once.
Failure modes
- A click from a phone that was offline arrives after its window closed, and is dropped.
- A retried event is counted twice, because the counter does not remember event ids.
- Counting by arrival time puts a late click in the wrong window.
In the real world
- Apache Flink's documentation describes watermarks, and an allowed lateness after which a window drops late events.
- Kafka Streams calls the same idea a grace period: how long a window still accepts late records.
- Google's paper "The Dataflow Model" (2015) set out this approach: windows by event time, watermarks, and a choice of how to treat late data. Source: Google: the Dataflow Model paper (2015) (checked 14 October 2026)
- Nathan Marz named the pairing of the two the Lambda architecture: a batch layer for complete results and a speed layer for recent ones.
Check yourself
Interview
What interviewers look for
- Ask whether counts must be exact, and how late data can arrive, before choosing a window design.
- If producers can retry, say how repeats are removed: "Each event has an id, and the counter skips an id it has already seen."
- Say which time you count by: "I window by event time, not arrival time, so a late click lands in the right window."
- State the trade in one line: "A longer allowed lateness drops fewer events, and every result arrives that much later."
Say it out loud: Windows are 10 seconds long and the allowed lateness is 0. A click at 8 s arrives at 14 s, after clicks at 11 s and 13 s. What happens to it?
Expected depth by interview level
Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.
- JuniorBeyond this level
- Not usually expected.
- MidUseful
- Batch against stream, and counting events in time windows.
- SeniorCore
- Event time, watermarks, late events and deduplication.
- StaffExpected
- Choosing stream, batch or both for a pipeline, and its cost in delay and accuracy.
Practice
Apply it
Three harder questions
The first two use the idea on a new case. The last asks you to judge a design.
Apply it
Apply it
Judge a design
Go deeper
Sources and further reading
First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Research paperMapReduce: Simplified Data Processing on Large Clusters (2004)Dean and Ghemawat, USENIX OSDIThe batch model: split the input, map, shuffle by key, reduce, and rerun failed tasks.
- Official docsTimely Stream ProcessingApache Flink documentationEvent time versus processing time, and watermarks for events that arrive late.