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.

Five clicks, in seconds of event time

Click happened

  1. The late click arrives at 8 s, in a tunnel
  2. A click arrives on time at 9 s

Arrived at the counter

  1. A click arrives on time at 9.1 s
Window 0–10 s count
1
Clicks that happened in it
2
  1. Windows of 10 seconds. A counter adds up ad clicks in 10-second windows of event time: 0–10 s, then 10–20 s.
  2. 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.
  3. The window closes. With no waiting, the click at 11 s moves the watermark past 10 s, and the 0–10 s window closes.
  4. Too late. The 8 s click arrives at 14 s, after its window closed, so it is dropped: the count stays 1.
  5. 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

  1. Every click is stamped with the moment it happened: its event time.
  2. Events can arrive out of order, and some arrive late.
  3. Once a window closes, a late event for it is dropped.
  4. Waiting longer counts more, but every answer comes later.

Go deeper in the Stream windows & late events lab →

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 . 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 , 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

Live simulationOpen the full lab →

Lab · Architecture patterns

Stream windows & late events

Phones send ad clicks, each stamped with the time it happened. A counter adds them up in 10-second windows. Make phones go offline, let retries send clicks twice, and choose how long the counter waits before a window's count is final.

Try it

Lab time 3.0 s
Phones, 4 clicks a secondPhones4 clicks a second10% offlineEvent stream, clicks, any orderEvent streamclicks, any orderWindow counter, windows of 10 sWindow counterwindows of 10 swatermark 0.7 sResults, 0 windowsResults0 windows
  • Phones — 4 clicks a second — healthy — 10% offline
  • Event stream — clicks, any order — healthy
  • Window counter — windows of 10 s — healthy — watermark 0.7 s
  • Results — 0 windows — healthy
  • A click (tagged late or again when it is)
  • A window's final count
Clicks arrived
3
Dropped late
0
Counted twice
0
Windows exact
0 of 0
Result delay
—

Numbers add up:3 arrived = 3 counted + 0 dropped late + 0 late retry copies + 0 duplicates removedThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

The counter
2 s

How long past a window's end the counter waits for stragglers.

Remember click ids and ignore a second copy.

The phones
10%

They send clicks 5 to 20 s late.

0%
4

Recent windows

WindowCountedReally happenedDropped lateStatus
0–10 s3…—open

Window times are event times: when the click happened, not when it arrived. "Really happened" is shown once a window closes.

What just happened

  1. 0.0sInfo: Windows of 10 s. Late clicks are allowed for 2 s.
What this model simplifies
  • Time is slowed so you can watch each click. Windows here are 10 seconds of event time.
  • The watermark only moves when a click arrives. Real streams also move it on a timer, so a quiet stream still closes windows.
  • A late click for a closed window is dropped. Real systems can instead send a corrected result, or route late data to a separate store.
  • Windows are tumbling: back to back, with no overlap. Sliding and session windows work the same way with lateness.
  • Here "allowed lateness" is how far the watermark trails the newest click (Flink calls it bounded out-of-orderness). In Flink, allowed lateness is a second wait after the watermark, during which a late click updates a result already sent.

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.

  1. 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.

  2. Put it in a window

    Pick a case to follow below

    With this case: By arrival time, the tunnel click landed in 10–20 s: 2 clicks and 2, both wrong.

    Phones, send clicksPhonessend clicksStream counter, 10-second windows, NOWStream counter10-second windows0–10 s: 2NOWPublished counts, final per windowPublished countsfinal per windowSet aside, late clicksSet asidelate 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 3

    Step 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 window
    2clicks
    10–20 s window
    0clicks

    Step 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 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.

  3. Move the watermark

    The watermark is the newest event time seen so far, minus the . 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.

  4. Close and publish

    Pick a case to follow below

    With this case: With no allowed lateness, the window closed at 13 s with 2 clicks, and the tunnel click was set aside.

    Phones, send clicksPhonessend clicksStream counter, 10-second windows, NOWStream counter10-second windowsmark 9 sNOWPublished counts, final per windowPublished countsfinal per windowSet aside, late clicksSet asidelate 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 3

    Step 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
    Watermark
    9 s
    newest stamp − 0 s
    0–10 s count
    2
    0–10 s published
    –
    Set aside as late
    0
    none

    Step 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.

  5. 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

What is a tumbling window?

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

When is a daily batch job a better choice than a stream?

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?

What does a longer allowed lateness buy, and what does it cost?

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

The allowed lateness is 5 seconds, and the newest event time seen is 23 s. Where is the watermark, and is the 10–20 s window closed?

Apply it

A phone sends a click event, loses the reply and sends it again. The counter gets the same click twice. What stops it being counted twice?

Judge a design

Advertisers pay per click, so the bill must be exact. They also want a live dashboard of clicks. Which design fits?

Next: analytics storage, why reports run on a column store and apps on a row store.

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.