Stream Processing Fundamentals
Lesson opener
In one line: A window is a bucket of event time you define. The processor's clock is a different time.
Why it matters: Northline wants picks per SKU per minute. A pick that happened at 16:10 can arrive at 16:12. If the minute bucket closes on arrival time, the pick lands in the wrong minute and the 16:10 total is short. Figures are illustrative.
Before this lesson: Lessons 1 to 4. You can name an event time.
What you will be able to do
- Separate event time from processing time.
- Build a tumbling window and name a late event.
- Sum inside a window without double-counting a pick id.
- Refuse a join that matches on SKU alone when the times do not overlap.
Prerequisites
Spark's structured streaming guide is the implementation the next lesson uses (https://spark.apache.org/docs/4.0.1/structured-streaming-programming-guide.html). This lesson is the vocabulary. The examples are plain Python.
Time and buckets
Plain English: Event time is when the thing happened. Processing time is when your job saw it. Ingestion time is when the log stored it. They differ when the network stalls.
A tumbling window has a fixed length and no overlap. Minute 16:10 covers event times from 16:10:00 up to but not including 16:11:00. A hopping window overlaps. A session window closes after a gap you choose.
A watermark is how late you still accept. If you allow 2 minutes, an event time of 16:10 can arrive until your watermark passes 16:10. After that it is late. The allowed lateness is your number. This lesson uses 2 minutes only as the example.
An aggregation is a fold over the window, such as a sum of quantities. A join keeps two events when the keys match and the time condition you wrote is true. State is what you remember: applied pick ids, open windows. State has to fit the job. It is not free.
Teacher walkthrough: picks in the 16:10 minute
All figures are illustrative. Times are minutes after 16:00.
- Bucket event time 10 into the window that starts at 10 when the width is 1 minute. Use integers: event minute 10, width 1.
- A second delivery of the same pick id does not add again.
- An event minute of 10 that arrives at processing minute 13 is late if the watermark allows only 2 minutes (13 is more than 10 + 2).
- Validation: one pick of 2 in that window sums to 2. The late copy is dropped. A new pick id in the same window still counts.
def window_start(event_minute, width):
return (event_minute // width) * width
def accept(event_minute, arrival_minute, allowed_late):
return arrival_minute <= event_minute + allowed_late
def sum_window(events, allowed_late):
seen = set()
total = 0
for event in events:
if not accept(event["event_minute"], event["arrival_minute"], allowed_late):
continue
if event["id"] in seen:
continue
seen.add(event["id"])
total += event["qty"]
return total
events = [
{"id": "p-16", "qty": 2, "event_minute": 10, "arrival_minute": 10},
{"id": "p-16", "qty": 2, "event_minute": 10, "arrival_minute": 11},
{"id": "p-17", "qty": 1, "event_minute": 10, "arrival_minute": 13},
]
assert window_start(10, 1) == 10
assert sum_window(events, 2) == 2p-17 arrived 3 minutes late, past the 2-minute allowance, so it is not in the sum.
A second workplace case
A price event and a pick event join when the SKU matches and the pick's event minute is inside the price's window. A price for FX-200 must not price FX-100.
def joined(pick, price):
same_sku = pick["sku"] == price["sku"]
in_time = price["start"] <= pick["event_minute"] < price["end"]
return same_sku and in_time
pick = {"sku": "FX-100", "event_minute": 10}
assert joined(pick, {"sku": "FX-100", "start": 10, "end": 11}) is True
assert joined(pick, {"sku": "FX-200", "start": 10, "end": 11}) is False
assert joined(pick, {"sku": "FX-100", "start": 11, "end": 12}) is FalseFailure example: the minute followed the clock on the server
Symptom: The 16:10 dashboard shows no picks. The 16:12 dashboard shows the 16:10 pick.
Diagnosis: The window used processing time. The event time was 16:10 and the job saw it at 16:12. The root cause is the wrong clock, not a missing event.
Correction: Bucket by event time. Set the allowed lateness to a number you can explain, here 2 minutes. Drop or sideline events later than that. Do not silently file them in the arrival minute.
Validation: Event minute 10 with arrival 12 is accepted when allowed lateness is 2 (12 <= 10 + 2). Arrival 13 is not.
def accept(event_minute, arrival_minute, allowed_late):
return arrival_minute <= event_minute + allowed_late
assert accept(10, 12, 2) is True
assert accept(10, 13, 2) is FalseGuided practice: a five-minute tumble
Scenario: Window width is 5 minutes. A pick has event minute 7. Another has event minute 5. Allowed lateness is 2. The minute-7 pick arrives at minute 8. Figures are illustrative.
Hints:
- Integer division by the width gives the window start.
- Which start does minute 7 use?
- Is arrival 8 inside the allowance?
Complete solution:
Window starts are 5 for both minute 5 and minute 7 (5 // 5 = 1, 1 * 5 = 5; 7 // 5 = 1). Arrival 8 is on time for event minute 7 because 8 <= 7 + 2.
def window_start(event_minute, width):
return (event_minute // width) * width
assert window_start(7, 5) == 5
assert window_start(5, 5) == 5
assert 8 <= 7 + 2Independent practice
Pick one metric that should be "per period," such as picks per minute.
Acceptance criteria:
- You name event time and processing time for one example event.
- You choose a window kind and a width.
- You set an allowed lateness and show one event you would drop.
- You dedupe by an event id inside the window.
- You do not join two SKUs because they share a minute.
Common mistakes
- Windowing on processing time and calling it event time.
- Inventing a lateness of zero and then losing every delayed pick, or inventing "forever" and never closing the window.
- Summing a retried pick id twice.
- Joining on SKU with no time bound, so yesterday's price matches today's pick.
Quick check
- Which time is when the pick happened?
- Do tumbling windows overlap?
- What is a watermark's allowed lateness in this lesson?
- Why was
p-17left out of the sum? - What two tests does the price join use?
Answers:
- Event time.
- No.
- A limit you choose. The example uses 2 minutes.
- It arrived later than event time plus 2 minutes.
- Same SKU, and the pick minute inside the price window.
Quiz alignment
The quiz asks you to bucket event time, to drop a late event, and to keep a join from matching the wrong SKU.
Lesson summary
Event time is the business clock. Windows are buckets you define. State remembers ids you already counted. Late means later than the allowance you wrote down. Joins need a key and a time condition.
Bridge to the next lesson
The next lesson runs these ideas in Apache Spark Structured Streaming, including the checkpoint that lets a job resume. Bring the window and the allowed lateness.