Watermarking for late data in Apache Spark - Time & Space Complexity
We want to understand how the time to process streaming data changes when we use watermarking for late data.
Specifically, how does handling late data affect the work Spark does as data grows?
Analyze the time complexity of the following Apache Spark Structured Streaming code snippet.
streamingDF
.withWatermark("eventTime", "10 minutes")
.groupBy(window(col("eventTime"), "5 minutes"), col("category"))
.count()
This code groups streaming data by time windows and category, using a watermark to handle late data up to 10 minutes late.
Look at what repeats as new data arrives:
- Primary operation: Grouping data by time windows and categories repeatedly for each batch.
- How many times: Once per micro-batch, processing all new data plus any late data within watermark.
As more data arrives, Spark processes more rows each batch, including late data within the watermark.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Processes about 10 rows per batch |
| 100 | Processes about 100 rows per batch |
| 1000 | Processes about 1000 rows per batch |
Pattern observation: The work grows roughly linearly with the number of rows processed each batch, including late data within the watermark.
Time Complexity: O(n)
This means the processing time grows roughly in direct proportion to the number of rows Spark processes each batch.
[X] Wrong: "Watermarking makes processing time constant no matter how much data arrives."
[OK] Correct: Watermarking limits how long late data is kept, but Spark still processes all data within that window, so time grows with data size.
Understanding how watermarking affects processing time helps you explain how streaming systems handle late data efficiently while scaling with input size.
What if we increased the watermark delay from 10 minutes to 1 hour? How would the time complexity change?
Practice
Solution
Step 1: Understand watermarking concept
Watermarking sets a time limit to wait for late data in streaming.Step 2: Identify purpose in streaming
It helps to ignore data that arrives too late to keep results timely.Final Answer:
To handle late data by setting a threshold time for event processing -> Option AQuick Check:
Watermarking = handle late data [OK]
- Thinking watermark speeds up jobs by skipping all data
- Confusing watermark with data storage
- Assuming watermark converts batch to streaming
df with event time column eventTime and delay threshold of 10 minutes?Solution
Step 1: Recall correct method signature
The method iswithWatermarkwith two string arguments: column name and delay duration.Step 2: Match correct syntax
df.withWatermark("eventTime", "10 minutes") uses correct method and string format for delay "10 minutes".Final Answer:
df.withWatermark("eventTime", "10 minutes") -> Option BQuick Check:
Correct syntax uses strings for both args [OK]
- Passing column name without quotes
- Using integer instead of string for delay
- Using wrong method name like watermark()
val streamingDF = spark.readStream.format("csv")
.option("header", "true")
.load("/data/events")
val withWatermarkDF = streamingDF.withWatermark("eventTime", "5 minutes")
withWatermarkDF.printSchema()Solution
Step 1: Understand watermark effect on schema
Watermark adds metadata to the event time column, which must be timestamp type.Step 2: Check expected schema output
Schema shows eventTime as timestamp with watermark metadata.Final Answer:
eventTime: timestamp (nullable = true) with watermark -> Option AQuick Check:
Watermark adds metadata to timestamp column [OK]
- Assuming eventTime remains string type
- Thinking watermark changes nullability
- Believing watermark does not affect schema
val df = spark.readStream.format("json")
.load("/stream/data")
val result = df.withWatermark("timestamp", "1 minute")
.groupBy(window(col("timestamp"), "30 seconds"))
.count()Solution
Step 1: Check watermark and window durations
Watermark delay must be shorter than window duration to ensure the query emits results.Step 2: Compare durations in code
Watermark is 1 minute but window is 30 seconds, watermark delay longer than window duration and causes error.Final Answer:
Watermark delay must be shorter than window duration -> Option DQuick Check:
Watermark < window duration [OK]
- Assuming any column name works
- Thinking window duration cannot be "30 seconds"
- Calling withWatermark after groupBy
Solution
Step 1: Understand watermark delay role
Watermark delay should cover the maximum expected lateness of data to include late events.Step 2: Match delay to late data arrival
Since late data can arrive up to 15 minutes late, watermark delay must be at least 15 minutes.Step 3: Avoid setting delay too short or too long
Setting delay less than 15 minutes risks dropping late data; too long delays output.Final Answer:
"15 minutes" -> Option CQuick Check:
Watermark delay = max late arrival [OK]
- Setting delay equal to window size instead of late arrival
- Using shorter delay than late data time
- Using unnecessarily long delay causing latency
Ready to prove it?
Pass the test and this pattern moves unknown
