Jump to content

Session window

From Open Lakehouse Wiki
Session window
Also known as Gap-based window, sessionization
Field Stream processing
Introduced Spark 3.2.0 (session_window)
Key parts Grouping key, event-time column, gap duration (static or per record), watermark
Open source implementations Structured Streaming (session_window), Apache Flink (session window assigners, SESSION TVF), Kafka Streams (SessionWindows)
Related Windowing (stream processing), Stateful stream processing, transformWithState
Last verified 23 November 2026 against the Spark 4.3.0 Structured Streaming programming guide and Real-time Mode guide, python/pyspark/sql/functions/builtin.py and SQLConf.scala at tag v4.3.0 of apache/spark, SPARK-10816, the Apache Flink 2.3 DataStream "Windows" and SQL "Windowing TVF" pages, and the Kafka Streams 4.4 DSL developer guide.

In stream processing, a session window is a window whose length is set by the data: it opens with a record for a key, extends each time another record for that key arrives within a gap duration, and closes when no record arrives for the length of the gap. Session windows group bursts of activity, such as one user's clicks during a visit, without fixing the window size in advance. In Apache Spark Structured Streaming, session windows are built with the session_window function, added in Spark 3.2.0.[1][2]

How is a session window different from a tumbling or sliding window?

Tumbling and sliding windows have a fixed size and fixed boundaries, so the engine can compute a record's window from its timestamp alone. A session window has neither: its start is the first record of the session and its end moves later with every new record. Spark's guide describes it as having a dynamic size that depends on the inputs.[3] See Windowing (stream processing) for the other window types.

Because a later record can join two earlier sessions into one, engines implement session windows by merging. Apache Flink's documentation states that its session window operator creates a new window for every record and merges windows that are closer to each other than the gap.[4]

How does session_window work in Spark?

session_window(timeColumn, gapDuration) returns a struct column named session_window with start and end fields. The end of a session is the timestamp of its latest record plus the gap, so it grows as records join the session.[1]

The gap can be a static string such as "5 minutes" or a column expression evaluated per row, for example a different gap per user type. With a dynamic gap, a session's range is the union of the ranges of its events (each event's time plus its own gap), and rows whose gap evaluates to zero or a negative value are filtered out of the aggregation.[3][1]

A streaming session window query has these restrictions in Spark 4.3.0:[3]

  • Update output mode is not supported; use append (sessions are emitted when the watermark passes their end) or complete.
  • The grouping key must contain at least one column besides session_window. A global session window, with session_window as the only key, is allowed only in batch queries.

By default Spark does not pre-aggregate sessions before the shuffle, because doing so needs an extra local sort. Setting spark.sql.streaming.sessionWindow.merge.sessions.in.local.partition (default false, since 3.2.0) to true sorts and merges sessions within each partition first, which helps when many rows of a batch belong to the same session.[3][5]

Session-window aggregation is not supported in Real-time Mode as of Spark 4.3.0.[6] Sessions with custom rules, such as closing a session on a logout event, need arbitrary stateful processing with transformWithState.

Example

This PySpark query counts page views per user session, where a session ends after 30 minutes without activity, and uses a shorter gap for one class of users:

from pyspark.sql import functions as F

views = spark.readStream.table("main.web.page_views")   # columns: ts, user_id, user_type

gap = F.when(F.col("user_type") == "bot", "1 minute").otherwise("30 minutes")

sessions = (views
    .withWatermark("ts", "1 hour")
    .groupBy(F.session_window("ts", gap), "user_id")
    .agg(F.count("*").alias("views")))

(sessions.writeStream
    .outputMode("append")
    .option("checkpointLocation", "/checkpoints/sessions")
    .toTable("main.web.user_sessions"))

In Spark SQL the same grouping is GROUP BY session_window(ts, '30 minutes'), user_id.

Spark session_window compared with Flink

Spark session_window Flink DataStream session assigners Flink SQL SESSION TVF
Gap Static string or per-row column expression[1] Static gap, or a SessionWindowTimeGapExtractor for dynamic gaps[4] Static gap only[7]
Key At least one grouping column besides the window in streaming[3] Keyed (keyBy) or non-keyed streams[4] Optional PARTITION BY key columns[7]
Time Event time; processing time by windowing on current_timestamp[1] Event-time and processing-time variants[4] Event or processing time attribute[7]

The two engines differ in batch support and in late data. Spark runs session_window in batch queries too, including a global session window, while Flink's SESSION TVF is not supported in batch mode.[3][7] In Spark, a record that arrives after the watermark has passed its session's end is not added to the session; Flink's DataStream session windows drop such records by default, and allowedLateness keeps windows open longer.[3][4]

Kafka Streams also has session windows: sessions are periods of activity separated by an inactivity gap, and the process is called sessionization.[8]

See also

References

  1. ↑ 1.0 1.1 1.2 1.3 1.4 python/pyspark/sql/functions/builtin.py (Apache Spark v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  2. ↑ SPARK-10816: EventTime based sessionization (session window). Apache Software Foundation JIRA.Retrieved 2026-11-23.
  3. ↑ 3.0 3.1 3.2 3.3 3.4 3.5 3.6 Structured Streaming: Types of time windows (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  4. ↑ SQLConf.scala (Apache Spark v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  5. ↑ Real-time Mode (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  6. ↑ Kafka Streams DSL (Apache Kafka 4.4). Apache Software Foundation.Retrieved 2026-11-23.