Stream-Stream Joins

Concepts covered: Structured Streaming

Joining 2 streams means neither side is ever complete. A ride request in batch 12 may meet its driver's acceptance in that same batch or 2 batches later in batch 14, and some requests never meet one. So Spark buffers both inputs: each new request is matched against every stored acceptance with the same key and then stored itself, and each new acceptance does the same against stored requests. One join keeps 4 state stores per shuffle partition, 2 per side: one counts the rows under each key and one holds the rows.

Within a micro-batch, Spark hash-shuffles both sides on the join key, which puts a request and its acceptance on the same task. The animation plays that step as a batch join with broadcasting turned off and the 10-minute time range used below, on times written as minutes after midnight.

spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
matched = requests.join(
    accepts,
    (requests.request_id == accepts.req_ref)
    & (accepts.acc_min >= requests.req_min)
    & (accepts.acc_min <= requests.req_min + 10),
)
matched.collect()
requests
request_idreq_min
1600
2602
3605
4610
5611
6615
accepts
req_refacc_min
1603
2615
3606
5611
6640

Request 2's acceptance reached the right task and still produced nothing: it came 13 minutes after the request, outside the range. Request 6's came 25 minutes after. In a stream, every one of the 6 requests would stay in state whether it had matched or not, because another acceptance with the same key could still arrive; the 3 even-numbered requests have only produced no output yet.

State that only grows

Spark accepts an inner stream-stream join without any watermark. Every request and every acceptance then stays in state for the life of the query, because Spark cannot rule out a future match. At 3,000 requests per second, the request side alone adds 10.8 million rows an hour.

Two watermarks and a time range

from pyspark.sql import functions as F

requests_wm = requests.withWatermark("request_time", "5 minutes")
accepts_wm = accepts.withWatermark("accept_time", "15 minutes")

matched = requests_wm.join(
    accepts_wm,
    F.expr(
        """
        req_ref = request_id
        AND accept_time >= request_time
        AND accept_time <= request_time + INTERVAL 10 MINUTES
        """
    ),
)

The equality finds the partner, and the range tells Spark when a stored row can no longer find one. A query with 2 watermarked inputs has a single global watermark, by default the minimum of the 2; call it G. Spark treats any acceptance still to come as carrying an accept_time after G, so a stored request is deleted once G passes request_time plus 10 minutes. Any request still to come carries a request_time after G, so a stored acceptance is deleted once G passes its own accept_time.

QuantityValueWhy
Request watermarknewest request minus 5 minDelay set on requests
Acceptance watermarknewest acceptance minus 15 minDelay set on acceptances
G15 min behindThe lower of the 2, streams in step
A request is held25 minUntil G passes request_time + 10 min
An acceptance is held15 minUntil G passes accept_time
Request rows held4.5 million3,000 a second for 1,500 seconds

The slower stream sets G for both sides. Raise the acceptance delay from 15 to 30 minutes and each request stays 40 minutes, even though nothing about requests changed. When a join's state is bigger than expected, start with its largest delay.

Requests carry a 5-minute delay, acceptances a 15-minute delay, and someone deletes the upper bound so the join keeps only accept_time >= request_time. What happens to state?

Outer joins wait on the watermark

A left outer join from requests to acceptances must eventually emit each unanswered request with NULLs, and it can only do so once no match is possible, the moment the request leaves state. Spark therefore refuses a left outer stream-stream join unless the right side has a watermark and the condition has a time range. With the settings above, an unanswered request's NULL row appears around 25 minutes after the request, and later if no new data arrives to move the watermark.

Do
  • Watermark both inputs, even on an inner join
  • Bound the partner's time from both sides in the condition
  • Set each delay from measured lateness, then work out state lifetime
  • Watch numRowsTotal for the join after every change
Avoid
  • Ship an inner stream-stream join with no watermarks
  • Write a one-sided range, which frees only 1 side's state
  • Pad one stream's delay far past the other's
  • Expect outer-join NULL rows before the watermark passes

About this lesson

This section is part of the Structured Streaming: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.