Skip to main content

Windowing & Time

Module Duration: 3-4 hours Focus: Time-based data partitioning and processing Prerequisites: Core Programming Model, basic streaming concepts

Overview

Windowing in Apache Beam allows you to partition unbounded (and bounded) data into logical windows for aggregation. This is essential for streaming analytics where you need to group events by time periods.

Key Concepts

  • Window: A logical grouping of data based on time or other criteria
  • Event Time: When the event actually occurred (embedded in data)
  • Processing Time: When the event is processed by the pipeline
  • Watermark: Estimate of event time progress
  • Window Assignment: Determining which window(s) each element belongs to

Understanding Time in Beam

Beam’s time model distinguishes between different notions of time for robust stream processing.

Event Time vs Processing Time

Event Time: The timestamp when the event actually occurred, embedded in the data itself.
  • More accurate for business logic
  • Accounts for late-arriving data
  • Requires watermark tracking
Processing Time: The timestamp when the pipeline processes the element.
  • Simpler to implement
  • No late data handling needed
  • Less accurate for business analysis

Watermarks

Watermarks are Beam’s mechanism for tracking progress in event time. A watermark is a guess that no more data with timestamps less than the watermark will arrive. Properties:
  • Monotonically increasing
  • Heuristic-based (not perfect)
  • Allows pipeline to make progress while handling late data
Impact:
  • Early watermark: May drop late data
  • Late watermark: May delay results

Window Types

Beam provides several built-in windowing strategies.

Fixed Windows

Divides time into fixed-size, non-overlapping intervals. Use Cases:
  • Hourly/daily reports
  • Regular interval aggregations
  • Time-series bucketing

Sliding Windows

Creates overlapping windows that slide by a specified period. Use Cases:
  • Moving averages
  • Rolling statistics
  • Overlapping time period analysis
Parameters:
  • Window size: Length of each window
  • Slide period: How often a new window starts

Session Windows

Groups elements into sessions based on gaps in activity. Use Cases:
  • User session analysis
  • Activity bursts detection
  • Click stream analysis
Parameters:
  • Gap duration: Minimum gap between sessions

Global Windows

Default window that spans all time. Useful for batch processing or when you don’t need time-based grouping.

Custom Windowing

You can create custom window functions for specialized use cases.

Window-Aware Transforms

Some transforms behave differently based on windowing.

GroupByKey with Windows

Combine with Windows


Working with Window Metadata

Access window information in your transforms.

Real-World Use Cases

Real-Time Analytics Dashboard

Click Stream Analysis

IoT Sensor Monitoring


Best Practices

Choosing the Right Window

Fixed Windows:
  • Regular reporting intervals
  • Time-series data bucketing
  • When you need non-overlapping periods
Sliding Windows:
  • Moving averages
  • Trend analysis
  • When you need overlapping perspectives
Session Windows:
  • User behavior analysis
  • Activity burst detection
  • Variable-length time periods
Global Windows:
  • Batch processing
  • When time doesn’t matter
  • Simple aggregations

Window Size Considerations

Memory Management


Summary

In this module, you learned:
  • Event time vs processing time concepts
  • Watermarks and their role in streaming
  • Fixed, sliding, session, and global windows
  • Custom windowing functions
  • Window-aware transforms
  • Real-world windowing use cases
  • Best practices for window selection

Key Takeaways

  1. Choose window type based on your use case
  2. Event time provides more accurate business logic
  3. Watermarks enable progress while handling late data
  4. Use CombinePerKey instead of GroupByKey for large windows
  5. Balance window size between latency and overhead
  6. Session windows are ideal for user behavior analysis

Next Steps

Continue to the next module on Triggers & Watermarks to learn how to control when results are materialized and how to handle late-arriving data effectively.

Additional Resources