Lesson 7 of 10 Data Pipeline Design Patterns

Handling Event Time, Windows and Late Data

This lesson explains event-time processing, windows and watermarks, which put each event in the right period when events reach a pipeline later than they happened and out of order.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish event time from processing time.
  • Distinguish tumbling, sliding and session windows.
  • Handle late data with watermarks, allowed lateness and side outputs.

Event time and processing time

Every event in a stream has two times. Event time is when something actually happened, such as when a user tapped a button, and is recorded in the event itself. Processing time is when the pipeline sees the event. In ideal conditions the two are close, but mobile devices that go offline and upload events later, retries after failures and network delays make them differ, sometimes by hours.

Aggregations should therefore group by event time. Grouping by processing time would count an event in the wrong period, for example counting purchases made just before midnight on a phone without signal in the next day's totals, and results would change depending on when the pipeline happened to run.

Windows

A stream never ends, so aggregations are computed over windows of time. A tumbling window has a fixed size and no overlap: each event belongs to exactly one window, as when counting orders per five minutes. A sliding, or hopping, window also has a fixed size, but a new window starts every step, so windows overlap and an event can belong to several; for example, the average over the last ten minutes, recalculated every minute. A session window has no fixed size: it groups a user's events and closes after a gap with no activity, such as 30 idle minutes. The following table summarises the window types.

WindowShapeExample
TumblingFixed size, no overlapOrders per 5 minutes
Sliding (hopping)Fixed size, overlapping, advancing by a stepAverage over the last 10 minutes, every minute
SessionCloses after a gap in activityUser sessions ending after 30 idle minutes
Sort into groups

Which window?

Exam skills 3.2.6 · 1.1.12

Choose the window type for each aggregation.

Log in to try this activity Interactive activities are free with an account.

Late data and watermarks

A watermark is the stream processor's estimate that no events older than a given time are still to come; a window's result is emitted when the watermark passes its end. Allowing lateness (for example 10 minutes) trades latency for completeness. Records later than that can be sent to a side output for separate handling. In Managed Service for Apache Flink these are explicit settings; in batch pipelines the equivalent is reprocessing recent partitions on each run (for example the last three days) so late files are picked up.

The following diagram compares the daily counts under the two approaches.

Counting orders by arrival date compared with event date. Twenty orders placed on October 6 arrive on October 8. Counted by arrival, they inflate October 8 and October 6 is never corrected. Counted by event date, with each run reprocessing the last three days, they land on October 6 and that day's total is corrected on the next run.
The counts are illustrative.
True or false

Watermarks and lateness

Exam skills 1.1.12 · 3.2.6

Decide whether each statement about late data is true.

Log in to try this activity Interactive activities are free with an account.
Worked example

A daily job counts orders by the date files arrive. Orders from a region with poor connectivity arrive up to two days late and are counted on the wrong day, and reports for past days never change.

Partition and aggregate by the order's event timestamp, not by arrival date, and reprocess a rolling window of the last three days on each run with idempotent partition overwrites. Late orders then land in the correct day, and that day's totals are corrected on the next run.

Check yourself

Should a stream be aggregated by event time or processing time?

Show answer

By event time, when the event happened, so delays in delivery do not move events into the wrong window.

Exam Tips

Remember for the exam

  • Aggregate by event time, not processing time.
  • Tumbling, sliding and session windows suit different questions.
  • Watermarks and allowed lateness trade latency for completeness in streams.
  • In batch, reprocess a rolling window of recent partitions to capture late data.
  • Send records later than the allowed lateness to a side output.