1. Introduction
Streaming Design Patterns are specialized pipeline topologies used to process infinite, out-of-order event streams while producing accurate, real-time analytics with bounded memory usage.
2. Why This Concept Exists
Unlike batch data where dataset boundaries are known ahead of time, streaming data arrives continuously 24/7.
Key streaming challenges solved by design patterns:
- Infinite Datasets: Computations cannot wait for "all data" to arrive before calculating totals.
- Out-of-Order Events: Mobile app events arrive seconds or minutes late due to poor cellular network coverage.
- Moving Statistics: Applications require rolling updates (e.g. 5-minute moving average calculated every 10 seconds).
3. Key Terminology
- Sliding Window Pattern: Calculating rolling window metrics (e.g. size 10 minutes, sliding every 1 minute) where events belong to multiple overlapping windows.
- Session Window Pattern: Grouping user actions into dynamic windows based on periods of inactivity (e.g. user activity sessions with a 15-minute gap threshold).
- Late Data Routing: Diverting late-arriving events that arrive after a window's watermark has passed to a dedicated side output.
4. How It Works
- Timestamp Assignment: Parse event timestamps from raw payload metadata.
- Window Into: Apply
SlidingWindowsorSessionswindowing functions. - Watermark Tracking: The runner tracks time progress using watermarks. When the watermark passes a window's end time, the window triggers.
- Emit Panes: The triggered pane emits window totals downstream.
5. Visual Diagram
Core Streaming Patterns: Sliding vs Session Windows
1. Sliding Windows (Rolling Averages)
Overlapping fixed-duration windows evaluated at regular slide periods.
Window Size: 10m | Slide: 1m2. Session Windows (Activity Gaps)
Dynamic windows expanded per user; closes after inactivity gap threshold.
Inactivity Gap: 15m6. Code Example
Below is a complete Python pipeline calculating a rolling moving average over sliding event-time windows:
pythonimport apache_beam as beam from apache_beam.transforms.window import SlidingWindows import time # Create timestamped streaming elements now = time.time() events = [ beam.window.TimestampedValue(("sensor-1", 72.5), now), beam.window.TimestampedValue(("sensor-1", 75.0), now + 10), beam.window.TimestampedValue(("sensor-1", 78.2), now + 30) ] with beam.Pipeline() as p: (p | "CreateStream" >> beam.Create(events) # 10-minute window size, evaluated every 1 minute | "SlidingWindow" >> beam.WindowInto(SlidingWindows(size=600, period=60)) | "CalculateAvg" >> beam.CombinePerKey(lambda values: sum(values) / len(values)) | "PrintMovingAvg" >> beam.Map(print))
7. Code Explanation
SlidingWindows(size=600, period=60)creates a 10-minute window ($600$s) that slides every 1 minute ($60$s).- Each temperature reading is assigned to multiple overlapping 10-minute windows, recalculating moving averages dynamically.
8. Real Production Example
In a fraud-detection streaming application for credit card transactions:
- Transactions are evaluated using a 5-minute sliding window sliding every 5 seconds.
- If a card generates more than 5 transactions across different physical locations within the 5-minute sliding window, the pipeline triggers an immediate security lock alert.
9. Common Mistakes
- Setting tiny slide periods with massive window sizes: Creating a 24-hour window sliding every 1 second creates 86,400 overlapping windows per element, exhausting runner memory.
- Confusing Event Time with Processing Time: Calculating moving averages using system clock time (
Processing Time) causes incorrect calculations when processing late data.
10. Best Practices
- Use sliding windows for continuous dashboard metrics and trend monitoring.
- Use
without_defaults()when combining empty global windows to avoid emitting null values.
11. Summary
- Sliding windows calculate rolling metrics over overlapping time frames.
- Session windows group user events based on periods of inactivity.
- Event-time processing ensures correct calculations even when network delays cause late data arrival.
12. Interactive Challenges
13. Related Content
Related Apache Beam Topics & Lessons
Apache Beam IntroductionLearn the core concepts of unified batch and streaming data processing.
Beam Pipeline BasicsConstruct and execute your first Apache Beam data processing pipeline.
PCollection Data AbstractionMaster distributed data collections in Apache Beam.
ParDo & DoFn TransformationsApply custom element-wise transformations with ParDo and DoFn.
People Also Search For