Joins in Stream Processing
How a stream processor joins data that never stops arriving: two event streams within a time window, a stream against a table kept up to date from its changelog, and two tables into a live view.
Overview
A batch join sees both inputs complete before it starts. A stream join never does. Events keep arriving, they can arrive late or out of order, and the other side of the join may not have arrived yet. So a stream processor must remember what it has seen (its state) and decide how long to keep it. Those two questions, what to remember and for how long, shape every kind of stream join.
There are three kinds of stream join, and each answers those questions differently.
- Stream-stream (window) join: both inputs are streams of events. The processor keeps the unmatched events from both sides, for the length of the window. Example: match each payment to its order within 1 hour.
- Stream-table join (enrichment): the left input is a stream of events, and the right is a table kept current from its changelog. The processor keeps a copy of the table. Example: add the customer's country to each purchase.
- Table-table join (view maintenance): both inputs are a table's changelog. The processor keeps the current rows of both tables. Example: keep a live 'orders with customer name' view.
Stream-stream join: match events within a window
Two event streams share a key, and you want to pair events that belong together: an ad impression with the click it caused, an order with its payment. Because either event can come first, the processor keeps each unmatched event in state. When the partner arrives, it emits the pair. The window bounds how long it waits: an interval join only pairs events whose timestamps are close enough.
Follow an orders stream and a payments stream through a join with a 1-hour window.
- Order 7 arrives, stamped 10:00. No payment has matched it yet, so the join operator keeps it in state.
- A payment for order 7 arrives, stamped 10:12. It falls inside the window, so the operator finds the match and emits the pair (order 7, payment).
- Order 8 arrives, stamped 10:05, and the operator keeps it.
- The watermark, the processor's clock for event time (explained below), passes 11:05 with no payment for order 8, so its window has passed.
- The operator drops order 8 from state, or emits it unmatched.
Each side waits in state for its partner, and dropping an order once its window has passed is what keeps the state bounded.
Flink SQL expresses this with a time condition on both sides. Its docs explain that, because the event times only move forward, Flink can remove old rows from state without changing the result.
Kafka Streams makes the same choice mandatory: a KStream-KStream join is always windowed, because otherwise its state store would grow forever. The cost of a window is memory: state grows with the event rate times the window length.
The same mechanics fit in a few lines of Python. A watermark (explained below) tells the operator when it is safe to forget.
Key concepts
- Stream-stream join
- Interval join
- Stream-table join
- Table-table join
- Temporal join
- Event time
- Watermark
- Co-partitioning
Interview questions
- Design a real-time pipeline that adds customer country and tier to every purchase event.
- How would you compute click-through rate by joining ad impressions with clicks in real time?
- Why use event time instead of processing time for stream joins?
- A streaming SQL join has been running for a month and its state keeps growing. Why, and what do you do?
Sign up free to read the full topic
More DDIA topics
- Fault Tolerance in Stream Processing
- Faults and Partial Failures
- Graph Data Models
- Implementing Serializability
- Isolation Levels
- Joins in Batch Processing
- Lambda and Kappa Architectures
- Leaderless Replication (Dynamo-Style)
- Linearizability
- LSM-Trees and SSTables
- MapReduce
- Modes of Dataflow: Databases, Services, Messages
Start free on Cariara · Coding problems · System design studio