Scrivio Interview library
CodingIntermediate5 min read · updated 13 November 2026

Matching each payment to its order within a time limit is a classic streaming join problem, with the added need to report orders that were never paid.

Write a Kafka Streams join that matches each payment on topic payments (keyed by orderId) with its order on topic orders (keyed by orderId) when the payment arrives within 15 minutes after the order, tolerating records up to 2 minutes out of order. Orders without a payment must also be emitted so another service can chase unpaid orders. What does Streams store, and when is an unpaid order emitted?

We write orders.leftJoin(payments, joiner, JoinWindows.ofTimeDifferenceAndGrace(15 minutes, 2 minutes).before(Duration.ZERO), StreamJoined with a store name), so a payment matches only if it comes at or after the order and within 15 minutes. Streams keeps both sides in changelog-backed window stores; a match is emitted as soon as the second side arrives. Since Kafka 3.1 an unpaid order, (order, null), is emitted only after the window closes, when stream time passes the order time plus 15 plus 2 minutes, so it is delayed on quiet partitions.

Why this question is asked

Matching payments to orders within a time limit, and catching the orders that were never paid, is a textbook stream-stream join. It also shows several subtle points about how Streams decides when a left join's empty result is final. Interviewers ask it to see whether we can write the join correctly and explain what Streams stores and when results come out. In this answer, we will write the join, look at the window definition closely, see what is stored, follow a paid and an unpaid order through time, and finish with partitioning and the downstream contract.

1Writing the Join

Orders are the left side, because every order must produce an output, paid or not. Payments are the right side. Both topics are keyed by orderId, so equal keys already meet in the same task.

Java
KStream<String, Order> orders = builder.stream("orders", Consumed.with(Serdes.String(), orderSerde));
KStream<String, Payment> payments = builder.stream("payments", Consumed.with(Serdes.String(), paymentSerde));
orders.leftJoin(payments,
(order, payment) -> new OrderPayment(order, payment), // payment is null if unpaid
JoinWindows.ofTimeDifferenceAndGrace(Duration.ofMinutes(15), Duration.ofMinutes(2))
.before(Duration.ZERO), // payment not before the order
StreamJoined.with(Serdes.String(), orderSerde, paymentSerde)
.withStoreName("order-payment-join"))
.to("order-payments", Produced.with(Serdes.String(), orderPaymentSerde));

2Reading the Window Definition

ofTimeDifferenceAndGrace(15 minutes, 2 minutes) creates a symmetric window: a payment matches if its timestamp is within 15 minutes before or after the order's. The requirement says the payment must come after the order, so before(Duration.ZERO) removes the before part: now a payment matches only if its timestamp is between the order's timestamp and 15 minutes later.

The grace of 2 minutes is about arrival order, not about the business rule. It means Streams keeps accepting records for a window for 2 more minutes of stream time after the window ends, so a payment that arrives slightly out of order is still matched.

Which payments match an order at 10:00
Payment timeMatches?Why
09:59NoBefore the order; before(ZERO) excludes it
10:00YesSame time is allowed
10:14YesWithin 15 minutes after
10:16NoMore than 15 minutes after

3What Streams Stores

A stream-stream join needs memory of both sides, because the matching record may arrive later. Streams keeps each side in a window store, RocksDB by default, holding records for the window plus grace. Each store is backed by a changelog topic so it can be restored on another instance; with our store name these are named after order-payment-join, which keeps them stable across topology changes.

When an order arrives, Streams writes it to the order store and looks in the payment store for payments within its window. When a payment arrives, it writes it to the payment store and looks in the order store. So a match is emitted as soon as the second of the two records arrives, whichever side that is. For the left join there is also a third store that tracks orders still without a match, which is how Streams knows what to emit as unpaid.

4When an Unpaid Order Is Emitted

Before Kafka 3.1, a left join emitted (order, null) immediately when the order arrived, and then (order, payment) again if a payment came later, so consumers saw a spurious unpaid result for almost every order. Since 3.1, Streams holds the order in the outstanding-records store and emits (order, null) only once the window has closed without a match: when stream time passes the order timestamp plus 15 minutes plus the 2-minute grace.

Step throughA paid order and an unpaid order
OrdersJoin taskPaymentsorder-payments
o-1 at 10:00 (stored, outstanding)
o-2 at 10:01 (stored, outstanding)
payment for o-1 at 10:06
(o-1, payment) emitted at once
later records move stream time past 10:18
(o-2, null) emitted: window closed unpaid

6 messages in order. Play them, or step through them one at a time.

o-2's window ends at 10:16 and grace at 10:18 of stream time; only then is it known to be unpaid.

5Partitioning and the Downstream Contract

The join requires both topics to be co-partitioned on orderId: same key, same partition count, same partitioner. If payments were keyed by paymentId, we would re-key them with selectKey((id, p) -> p.orderId()), and Streams would insert a repartition topic before the join.

Two properties matter for the chasing service. First, the unpaid signal depends on stream time, so on a quiet partition it is delayed until newer records arrive; the service should tolerate that, or a scheduled check against the order database should complement it for orders older than, say, 30 minutes. Second, duplicate payments, or a redelivered payment after a failure in at-least-once mode, produce duplicate join results, so downstream processing must be idempotent, for example keyed by order id.

6Follow-up questions

What changes if the business also wants payments that arrive without an order?

We switch to an outerJoin. Then a payment that finds no order within the window produces (null, payment) after the window closes, and the joiner must handle a null order as well as a null payment. Because the window is asymmetric, we must think about the payment's view of it: with before(ZERO) on the order side, a payment matches orders up to 15 minutes before it, so its own unmatched result is emitted when stream time passes its timestamp plus the grace.

Why not use a KStream-KTable join with orders as a table?

A stream-table join is triggered only by stream records and has no notion of an order waiting for a payment, so it cannot emit unpaid orders. It also ignores the 15-minute rule unless we code it ourselves.

How large do the window stores get?

About 17 minutes of orders and 17 minutes of payments per task, plus store retention overhead. At 1,000 orders per second that is around a million records on each side, which is fine for RocksDB.

7Conclusion

In this answer, we wrote a left join from orders to payments with a 15-minute window, a 2-minute grace and before(ZERO) so payments must come after the order. Streams keeps both sides in changelog-backed window stores and emits a match as soon as the second side arrives. Since Kafka 3.1, unpaid orders are emitted as (order, null) only after stream time passes the order time plus 17 minutes, which delays them on quiet partitions. Both topics must be co-partitioned on orderId, and downstream must be idempotent.

What to say in the interviewAbout 90 seconds aloud

Open with

I would make orders the left side so every order produces output: orders.leftJoin(payments, a joiner building OrderPayment with a possibly null payment, JoinWindows.ofTimeDifferenceAndGrace with fifteen minutes and two minutes, then before Duration.ZERO, and StreamJoined with the serdes and a store name).

Then, if they want more

The window is symmetric by default, so before ZERO restricts it to payments at or after the order, up to fifteen minutes later, and the grace keeps windows open two more minutes of stream time for out-of-order records.

Streams stores both sides in window stores backed by changelog topics, kept for the window plus grace; each order looks for buffered payments and each payment for buffered orders, so a match comes out as soon as the second side arrives.

Since Kafka 3.1, the left join does not emit order with null immediately; it tracks outstanding orders in an extra store and emits the unpaid result only when stream time passes the order timestamp plus fifteen minutes plus two.

Both topics must be co-partitioned on orderId; if payments were keyed by paymentId I would selectKey and Streams would add a repartition topic. Because the unpaid signal follows stream time, it is late on quiet partitions, so the chasing service should tolerate that or add a scheduled check, and duplicate payments mean downstream must be idempotent.

About this answer

Last updated
13 November 2026
Versions it names
Kafka 3.1
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Kafka documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Write a Kafka Streams join that matches each payment on topic payments (keyed by...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-streams-connect/3bd8af1f33/