Course outline

Apache Spark / Advanced: streaming and recoverable operation / Lesson 21

Stream joins and bounded event time

Design a time-bounded stream join and test late or unmatched events under explicit semantics.

About 3 min readContent reviewed
On this page

Lesson goals

  • Define a join key and event-time bound for two streams.
  • Test late and unmatched records without assuming a watermark retains all history.

Define the matching question

A streaming join correlates records arriving from two inputs, but the business contract must define the key, time relationship and treatment of unmatched events. State whether a scan can match more than one acceptance and which event-time clock applies. A key match alone can produce unintended combinations if several records share that key. Check supported join types and watermark requirements for the deployed Spark release.

Inspect an acceptance event

The first synthetic JSON record describes parcel P7 accepted at 10:00 UTC. It is a readable input fixture, not an executed Spark source. Assume for this lesson that a scan is eligible when it has the same parcel key and an event time from acceptance through ten minutes later, including both endpoints. Real business rules must specify timezone, boundaries and duplicate handling.

json Synthetic acceptance event
{"parcel":"P7","event":"accepted","event_time":"2026-11-05T10:00:00Z"}

The fixture supplies a key and event time for the stated matching rule.

Evaluate one candidate scan

The second event has the same key and occurs five minutes later, so it satisfies this exampleโ€™s time condition. That arithmetic does not prove the two records were processed by a real streaming query. Test another scan at the exact ten-minute boundary and one beyond it. If two eligible scans are possible, the application must define whether both are valid or how one is selected.

json Synthetic scan event
{"parcel":"P7","event":"scanned","event_time":"2026-11-05T10:05:00Z"}

10:05 is within the stated inclusive ten-minute window after 10:00.

Bound and observe state

Stream joins may retain state while waiting for a matching event. Watermarks and time constraints help determine when old state can be cleaned under the supported query semantics; they do not guarantee every arbitrarily late event is retained. Outer joins can have additional timing and watermark requirements. Inspect progress and state metrics, and distinguish a late arrival from a truly absent business event.

Exercise lateness and recovery

In an isolated query and sink, send synthetic matching, boundary, unmatched and late events using the configured event-time rules. Record the actual output and progress instead of assuming a particular batch schedule. Restart with the intended checkpoint and reconcile sink effects by stable identity. Compare results with independently defined expectations and document which lateness and recovery cases were not covered.

Common errors and fixes

One acceptance produces several unintended matches.

Why it happens
The key and time rule permit several scans.
How to fix it
Apply the documented business selection or multiplicity rule.
Prevent it next time
Test duplicate and multiple candidate events.

A very late event is assumed to be retained indefinitely.

Why it happens
A watermark was treated as an unlimited history guarantee.
How to fix it
Review the configured event-time and recovery contract.
Prevent it next time
Test late events and monitor state.

A restart duplicates a downstream effect.

Why it happens
Join output and external sink idempotency were conflated.
How to fix it
Reconcile stable identities at the sink.
Prevent it next time
Test interruption around sink publication.

2 practice exercises

1. Does the scan at 10:05 match the acceptance at 10:00 under the example rule?

Show worked solution

Yes. The keys match and five minutes lies within the stated inclusive ten-minute range. This is a calculation on the fixtures, not evidence of an executed Spark query.

2. What if a second eligible scan arrives for P7?

Show worked solution

The time and key rule permits another match. Consult the business multiplicity contract and apply its explicit selection or aggregation; do not assume Spark will infer the intended single event.

Key takeaways

  • Specify join keys, time bounds and multiplicity together.
  • Test watermarks, state and sink recovery under the deployed query semantics.

Official references