Jump to content

Stream-stream join

From Open Lakehouse Wiki
Revision as of 00:53, 25 November 2026 by Open Lakehouse Wiki sync (talk | contribs) (Sync from open-lakehouse/wiki@7c9abaa)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Stream-stream join
Also known as Streaming join, interval join (Flink)
Field Stream processing
Introduced Apache Spark 2.3
Key parts Two streaming inputs, join keys, watermark on each input, event-time range or window condition, buffered state
Open source implementations Structured Streaming in Apache Spark, Apache Flink (interval join, regular join), Kafka Streams (JoinWindows)
Related Stream-static join, Watermark, State store, Stateful stream processing
Last verified 23 November 2026 against the Spark 4.3.0 Structured Streaming programming guide (join operations, support matrix, recovery semantics) and Real-time Mode guide, the Apache Flink 2.3 DataStream "Joining" and SQL "Joins" pages, and the Kafka Streams 4.4 DSL developer guide.

In Apache Spark Structured Streaming, a stream-stream join joins two streaming DataFrames, so that each new row from one stream is matched with rows that have arrived, or will arrive, on the other. Because a match can come later, Spark buffers past rows of both inputs as state; watermarks on both inputs and a time condition in the join let Spark drop buffered rows that can no longer match, so that state does not grow without limit. Spark added stream-stream joins in Spark 2.3.[1] The result of the join is the same as a batch join over the same data.[2]

Why does a stream-stream join need state?

At any moment, both sides of a stream-stream join are incomplete. A row received now on one input can match a row that the other input has not received yet. Spark therefore keeps past input from both streams in the state store, matches every new row against it, and handles late and out-of-order data the same way as for aggregations.[1]

Without limits, all past input must be kept, because any new row could match any old one.[3] In a long-running query, that state grows until the query fails or slows down.

How do watermarks and time constraints bound state?

To let Spark clear old rows, a join needs two things:[3]

  1. a watermark on both inputs, saying how late each input's data can be; and
  2. an event-time constraint between the inputs, so that Spark can tell when an old row of one input can no longer match the other. The constraint is either a time range, such as leftTime BETWEEN rightTime AND rightTime + INTERVAL 1 HOUR, or a join on event-time windows, such as leftTimeWindow = rightTimeWindow.

With both, the rows kept for each side are roughly those within the other side's watermark delay plus the width of the time range. The guarantee follows the watermark rule: with a 2-hour watermark, no row less than 2 hours late is dropped, but a row later than that may or may not be processed.[1]

Example

The Spark guide's example joins ad impressions with ad clicks. Impressions can be up to 2 hours late, clicks up to 3 hours late, and a click counts if it happens within 1 hour after its impression.[3]

from pyspark.sql.functions import expr

impressions = spark.readStream.table("main.ads.impressions")   # impressionAdId, impressionTime
clicks = spark.readStream.table("main.ads.clicks")             # clickAdId, clickTime

joined = (impressions.withWatermark("impressionTime", "2 hours")
    .join(
        clicks.withWatermark("clickTime", "3 hours"),
        expr("""
            clickAdId = impressionAdId AND
            clickTime >= impressionTime AND
            clickTime <= impressionTime + interval 1 hour
        """),
        "leftOuter",
    ))

Which join types are supported?

Inner joins are supported on any columns and conditions; watermarks and time constraints are optional, but without them state is unbounded. Outer and semi joins require them, because Spark must know when a row will never match in order to emit the outer row with nulls, or to evict an unmatched left row.[1] The support matrix in Spark 4.3.0:[4]

Join type Requirement
Inner Supported; watermarks on both sides and a time constraint are optional and enable state cleanup
Left outer Watermark on the right side and a time constraint required; left watermark optional, for full cleanup
Right outer Watermark on the left side and a time constraint required; right watermark optional
Full outer Watermark on one side and a time constraint required; the other watermark optional
Left semi Watermark on the right side and a time constraint required; left watermark optional

Other rules apply. Joins can be chained (df1.join(df2, ...).join(df3, ...)), and a query with a stream-stream join must use append output mode. mapGroupsWithState and flatMapGroupsWithState cannot be used before or after a join, but in append mode aggregations, deduplication and other joins can.[4] Changing the join type, the equi-join columns or the input schema is not allowed between restarts from the same streaming checkpoint.[5] Stream-stream joins are not supported in Real-time Mode as of Spark 4.3.0.[6]

Why are outer join results delayed?

An outer row with nulls is emitted only after Spark is sure no match will arrive, so its delay depends on the watermark delay and the time range. In the micro-batch engine, the watermark advances at the end of a batch and the next batch uses it to clean state and emit outer rows. Since a batch runs only when there is new data, outer output can be held back if either input stops receiving data for a while.[7]

Stream-stream joins in other engines

Apache Flink's DataStream API has an interval join: elements of stream B with the same key match an element of stream A when a.timestamp + lowerBound <= b.timestamp <= a.timestamp + upperBound. It supports only inner joins.[8]

Flink SQL distinguishes regular joins, which keep both inputs in state forever unless a state TTL is configured (which can affect correctness), from interval joins, which need at least one equi-join predicate and a condition that bounds time on both sides.[9] A Spark join with a time-range condition and watermarks corresponds to an interval join; a Spark inner join without them corresponds to a regular join without TTL.

Kafka Streams uses JoinWindows, a sliding window over record timestamps, for windowed stream-stream joins.[10]

Spark stream-stream join Flink DataStream interval join Flink SQL interval join Flink SQL regular join
Time bound Optional for inner, required for outer and semi[1] Required (lower and upper bound)[8] Required on both sides[9] None[9]
Can state grow without limit? Yes, for an inner join without watermarks and a time constraint[3] No; the time bounds are part of the join[8] No; the time condition is required[9] Yes, unless a state TTL is configured[9]

See also

References

  1. ↑ 1.0 1.1 1.2 1.3 1.4 Structured Streaming: Stream-stream Joins (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  2. ↑ Structured Streaming: Join Operations (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  3. ↑ 3.0 3.1 3.2 3.3 Structured Streaming: Inner Joins with optional Watermarking (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  4. ↑ 4.0 4.1 Structured Streaming: Support matrix for joins in streaming queries (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  5. ↑ Structured Streaming: Recovery Semantics after Changes in a Streaming Query (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  6. ↑ Real-time Mode (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  7. ↑ Structured Streaming: Outer Joins with Watermarking (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  8. ↑ Kafka Streams DSL (Apache Kafka 4.4). Apache Software Foundation.Retrieved 2026-11-23.