Apache Beam Windows: Fixed, Sliding, and Session Examples

Compare Beam's fixed, sliding, and session windows with one event stream, a runnable Python pipeline pinned to Beam 2.77.0, and runner-specific support notes.

published: reading time: 6 min read author: Geek Workbench updated: November 4, 2026
Quick Summary

One four-event stream produces three different windowed sums. Follow the calculations, run the Python example, and check runner-specific support before relying on a windowing feature.

Apache Beam Windows: Fixed, Sliding, and Session Examples

Fixed, sliding, and session windows group the same events in different ways. This walkthrough uses four values with event times measured in minutes from one shared zero, then calculates every nonempty window. The examples focus on aggregate membership, not when a streaming runner emits results.

The version check on 2026-11-04 found Beam 2.77.0 as the latest release, published on 2026-10-07. The example pins that Python SDK version. See the official Beam downloads page for the current release and install guidance.

Use one event stream

Event time from zero Value
6 minutes 5
11 minutes 3
16 minutes 4
23 minutes 2

The table gives each record an event time, not the time the runner processes it. Processing time is when the runner handles the record. Because this input comes from bounded Create, the example does not model delayed arrivals or processing-time triggers. Beam sources can set timestamps; this code assigns them with TimestampedValue. See the Beam Programming Guide and the Python WithTimestamps documentation for timestamp assignment and window transforms.

All intervals below use half-open endpoints: [start, end) includes the start and excludes the end. Fixed windows are 10 minutes long. Sliding windows are 10 minutes long, start every 5 minutes, and align to the common zero. The session window uses a 6-minute gap for one key. Beam’s guide describes fixed and sliding windows and per-key session merging in the same terms.

Compare the outputs

Window type Nonempty windows and values Sum per window
Fixed, 10 minutes [0,10): 5; [10,20): 3, 4; [20,30): 2 5; 7; 2
Sliding, 10 minutes every 5 [0,10): 5; [5,15): 5, 3; [10,20): 3, 4; [15,25): 4, 2; [20,30): 2 5; 8; 7; 6; 2
Session, 6-minute gap [6,22): 5, 3, 4; [23,29): 2 12; 2

For the sessions, the first three events are 5 minutes apart, so their 6-minute session intervals overlap and merge into [6,22). The gap from minute 16 to minute 23 is 7 minutes, so the last event starts a new session [23,29). Session windows are per key; the sample assigns every event to the same key.

Run the Python example

The code uses Create with a small, bounded list, assigns event timestamps, then applies each window and sums values under one key. It writes one text file for each window type.

Save this as window_demo.py:

import apache_beam as beam
from apache_beam import window
from apache_beam.options.pipeline_options import PipelineOptions

EVENTS = [(5, 6), (3, 11), (4, 16), (2, 23)]


class FormatWindowSum(beam.DoFn):
    def process(self, element, window=beam.DoFn.WindowParam):
        _, total = element
        start = window.start.micros // 60_000_000
        end = window.end.micros // 60_000_000
        yield f"[{start},{end}) -> {total}"


def main():
    windows = [
        ("fixed", window.FixedWindows(10 * 60)),
        ("sliding", window.SlidingWindows(10 * 60, 5 * 60, offset=0)),
        ("session", window.Sessions(6 * 60)),
    ]

    with beam.Pipeline(options=PipelineOptions()) as pipeline:
        events = (
            pipeline
            | "Create values and event minutes" >> beam.Create(EVENTS)
            | "Assign event timestamps" >> beam.Map(
                lambda item: window.TimestampedValue(item[0], item[1] * 60)
            )
            | "Key events for aggregation" >> beam.Map(lambda value: ("all", value))
        )

        for name, window_fn in windows:
            results = (
                events
                | f"{name} window" >> beam.WindowInto(window_fn)
                | f"{name} sum" >> beam.CombinePerKey(sum)
                | f"{name} format" >> beam.ParDo(FormatWindowSum())
            )
            results | f"{name} output" >> beam.io.WriteToText(
                f"beam-window-{name}", num_shards=1
            )


if __name__ == "__main__":
    main()

Beam 2.77.0 is installed from PyPI as apache-beam==2.77.0. Create an environment, install the pinned SDK, and run the bounded example locally with the bundle-based DirectRunner:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install "apache-beam==2.77.0"
python window_demo.py --runner=BundleBasedDirectRunner

The runner writes beam-window-fixed-*, beam-window-sliding-*, and beam-window-session-* files. Their lines should match the sums above; file order is not significant. The Beam 2.77.0 Python runner reference describes the local DirectRunner implementations. This is a small local example, not a production runner setup.

Check runner support before depending on it

Beam exposes a common model, but its runners do not implement every feature in the same way. The official capability matrix was checked on 2026-11-04 and was last updated on 2026-11-03.

Feature What the current Beam matrix reports What to verify
Fixed and sliding windows Supported by Flink and Spark’s RDD/DStream runner; Spark Structured Streaming is marked partial, with full support in batch mode. Confirm the exact SDK, runner, and streaming mode you will deploy.
Session windows Supported by Dataflow, Flink, and Spark RDD/DStream; Spark Structured Streaming is partial; Prism and Python Direct FnRunner are marked unsupported. The sample explicitly uses BundleBasedDirectRunner; do not assume another local runner path supports session merging.
Triggers and allowed lateness The matrix lists event-time and processing-time triggers as supported by Flink and Spark RDD/DStream, but partial for Spark Structured Streaming. It marks allowed lateness unsupported for both Spark rows. Check the matrix and runner docs before adding triggers, early/late firings, or a lateness policy.
State and timers Stateful processing is marked partial for Flink, Spark RDD/DStream, and Spark Structured Streaming. Timers are also marked partial for those runners. Validate the exact state/timer behavior on the chosen runner and version.
Connectors and operations This semantics matrix does not establish connector availability or deployment behavior. Check connector and runner docs, then validate checkpoints, recovery, and operational settings in the target environment.

The DirectRunner guide describes local execution as a development and testing tool and recommends validating on the target remote runner. For runner-specific setup, consult the official Flink Runner and Spark Runner pages. A bounded Create pipeline proves only the calculated grouping and sums here; it says nothing about watermarks, late arrivals, trigger timing, or operational behavior on a streaming deployment.

For native engine concepts, compare the Apache Flink guide and Apache Spark Streaming guide. Beam’s window model is useful when a shared pipeline API matters, but confirm the exact semantics and connectors against the runner you plan to operate.

Conclusion

Fixed windows put each event in one interval. Sliding windows can count an event in multiple intervals; sessions connect nearby events for the same key. Use the sums to check membership, then validate runner-specific features separately.

Category

Related Posts

Designing Production Alerts: Signals and Thresholds

Choose user-focused alert signals, set workload-specific thresholds, and use SLO burn rates without treating examples as universal defaults.

#data-engineering #alerting #monitoring

Audit Trails: Building Complete Data Accountability

Trace an order across API, outbox, payment, and shipping. Define event fields and a correlation query, then weigh access, integrity, retention, and deletion.

#data-engineering #audit #audit-trails

AWS Data Services: Kinesis, S3, Glue, Athena, Redshift

Follow an order event from Kinesis Data Streams through S3 to Athena or Redshift, adding Glue when transformation or shared catalog metadata helps.

#data-engineering #aws #kinesis