Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 78/ 160
04. Big Data Processing & Apache Spark

Watermarking, Event-Time vs Processing-Time & Stateful Streaming

Handle late data with watermarks and reason about event time versus processing time in stateful ops.

AdvancedWatermark · Event Time · State3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Parse event instant and source watermark. Next, event-to-arrival lateness distribution. Finally, window output, late side-channel, and state cleanup.

Think of it like…

Event time is the time printed on the train ticket; processing time is when the ticket reaches the office, and the watermark is a policy for how long to keep the late-ticket box open.

Where you’ve seen it

df.withWatermark('event_ts','10 minutes').groupBy(window('event_ts','5 minutes')) closes a 5-minute window after max seen event time minus 10 minutes, not based on the wall clock alone.

Words you’ll hear

Watermark
A timestamp asserting when the underlying action occurred and therefore the basis for business windows.
Event Time
An explicit bound max event time seen minus allowed lateness for deciding when state may be closed.
State
Window/aggregate/join data retained across batches by keyed event-time progress and checkpoint versions.
Operational boundary
A timestamp parsed as local wall time shifts windows, a too-small watermark drops mobile events, and a too-large watermark retains state and delays closed-window output without reducing source disorder. Normalize source event time, measure lateness distribution from event-to-ingest timestamps, set watermark to a business percentile plus margin, route dropped late records, and bound state timeout with the allowed correction window.

Visual diagram

Loading interactive diagram

Theory & deep dive

Watermark: the working mechanism

Event-time processing uses the timestamp carried by an event, processing time uses wall clock arrival, and a watermark says old events beyond a bounded delay may be ignored or handled separately.

Parse event instant and source watermark. Event-to-arrival lateness distribution, then watermark/state TTL and late policy; this exposes the state governed by State.

Watermarking, Event-Time vs Processing-Time & Stateful Streaming in one concrete design

df.withWatermark('event_ts','10 minutes').groupBy(window('event_ts','5 minutes')) closes a 5-minute window after max seen event time minus 10 minutes, not based on the wall clock alone.

Key takeaway: Event-time processing uses the timestamp carried by an event, processing time uses wall clock arrival, and a watermark says old events beyond a bounded delay may be ignored or handled separately

Watermarking, Event-Time vs Processing-Time & Stateful Streaming: failure mode and production response

A timestamp parsed as local wall time shifts windows, a too-small watermark drops mobile events, and a too-large watermark retains state and delays closed-window output without reducing source disorder.

Normalize source event time, measure lateness distribution from event-to-ingest timestamps, set watermark to a business percentile plus margin, route dropped late records, and bound state timeout with the allowed correction window. Track Lateness p99 as "sets minimum business watermark candidate", and make State TTL visible to the dataset owner.

Key takeaway: Normalize source event time, measure lateness distribution from event-to-ingest timestamps, set watermark to a business percentile plus margin, route dropped late records, and bound state timeout with the allowed correction window.

How it works, step by step

Step 1 of 4
Watermark

1. Extract

Parse event instant and source watermark. Event-time processing uses the timestamp carried by an event, processing time uses wall clock arrival, and a watermark says old events beyond a bounded delay may be ignored or handled separately.

parse event instant and source watermark
Key numbers at this step
Lateness p99
sets minimum business watermark candidate
Event Time
An explicit bound `max event time seen minus allowed lateness` for deciding when state may be closed.

When to use it & trade-offs

What it is

Event-time processing uses the timestamp carried by an event, processing time uses wall clock arrival, and a watermark says old events beyond a bounded delay may be ignored or handled separately. df.withWatermark('event_ts','10 minutes').groupBy(window('event_ts','5 minutes')) closes a 5-minute window after max seen event time minus 10 minutes, not based on the wall clock alone.

Why it matters: A timestamp parsed as local wall time shifts windows, a too-small watermark drops mobile events, and a too-large watermark retains state and delays closed-window output without reducing source disorder. Normalize source event time, measure lateness distribution from event-to-ingest timestamps, set watermark to a business percentile plus margin, route dropped late records, and bound state timeout with the allowed correction window.

When to reach for it

  • Handle late data with watermarks and reason about event time versus processing time in stateful ops.
  • Choose Watermark because it provides this capability: A timestamp asserting when the underlying action occurred and therefore the basis for business windows.
  • Keep evidence for Lateness p99 at "sets minimum business watermark candidate" so the last production stage-Close/publish - window output, late side-channel, and state cleanup-remains testable.

When to avoid or be careful

  • A timestamp parsed as local wall time shifts windows, a too-small watermark drops mobile events, and a too-large watermark retains state and delays closed-window output without reducing source disorder.
  • Do not conflate Event Time with Watermark. The second concept contributes different behavior: An explicit bound max event time seen minus allowed lateness for deciding when state may be closed.
  • Release only after this guardrail is in place: Normalize source event time, measure lateness distribution from event-to-ingest timestamps, set watermark to a business percentile plus margin, route dropped late records, and bound state timeout with the allowed correction window.

Watermark / Event Time / State - roles, limits, and operating cost

Watermark
Best for: A timestamp asserting when the underlying action occurred and therefore the basis for business windows.

Strengths: A timestamp asserting when the underlying action occurred and therefore the basis for business windows. Event-time processing uses the timestamp carried by an event, processing time uses wall clock arrival, and a watermark says old events beyond a bounded delay may be ignored or handled separately.

Watch out: A timestamp parsed as local wall time shifts windows, a too-small watermark drops mobile events, and a too-large watermark retains state and delays closed-window output without reducing source disorder.

Event Time
Best for: An explicit bound max event time seen minus allowed lateness for deciding when state may be closed.

Strengths: An explicit bound max event time seen minus allowed lateness for deciding when state may be closed. In this workflow it supports: Set bound - watermark/state TTL and late policy.

Watch out: The operating cost is real: cannot be shorter than required event matching or correction.

State
Best for: Window/aggregate/join data retained across batches by keyed event-time progress and checkpoint versions.

Strengths: Window/aggregate/join data retained across batches by keyed event-time progress and checkpoint versions. Its output can be checked with Watermark delay.

Watch out: Operating guardrail: Normalize source event time, measure lateness distribution from event-to-ingest timestamps, set watermark to a business percentile plus margin, route dropped late records, and bound state timeout with the allowed correction window.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for Watermark, and how would Lateness p99 reveal it?”
  • “Why is Event Time distinct from this behavior: window/aggregate/join data retained across batches by keyed event-time progress and checkpoint versions?”
  • Production clue: A timestamp parsed as local wall time shifts windows, a too-small watermark drops mobile events, and a too-large watermark retains state and delays closed-window output without reducing source disorder.
  • Design probe: Normalize source event time, measure lateness distribution from event-to-ingest timestamps, set watermark to a business percentile plus margin, route dropped late records, and bound state timeout with the allowed correction window.

Benchmarks & rules of thumb

Lateness p99 -
sets minimum business watermark candidate
State TTL -
cannot be shorter than required event matching or correction
Watermark delay -
allows lateness but delays final output

Common mistakes & follow-ups

Common interview mistakes

  • A timestamp parsed as local wall time shifts windows, a too-small watermark drops mobile events, and a too-large watermark retains state and delays closed-window output without reducing source disorder.
  • Reporting success from a job exit code without checking Lateness p99 and State TTL.
  • Treating this example as complete without replay, rollback, or access boundaries: df.withWatermark('event_ts','10 minutes').groupBy(window('event_ts','5 minutes')) closes a 5-minute window after max seen event time minus 10 minutes, not based on the wall clock alone.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: A timestamp asserting when the underlying action occurred and therefore the basis for business windows.
  • What does the recovery plan do when this occurs? A timestamp parsed as local wall time shifts windows, a too-small watermark drops mobile events, and a too-large watermark retains state and delays closed-window output without reducing source disorder.
  • When is Watermark insufficient for State TTL, and what does adding Event Time cost in Lateness p99?
Interview tip: Lead with the data path and failure boundary: Event-time processing uses the timestamp carried by an event, processing time uses wall clock arrival, and a watermark says old events beyond a bounded delay may be ignored or handled separately. Then make the answer operational: Normalize source event time, measure lateness distribution from event-to-ingest timestamps, set watermark to a business percentile plus margin, route dropped late records, and bound state timeout with the allowed correction window.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Watermarking, Event-Time vs Processing-Time & Stateful Streaming with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Structured Streaming with PySpark (Micro-Batching, Triggers, Checkpointing)
Next topic
Running Spark on Cloud Platforms (Databricks, AWS EMR, GCP Dataproc)