Skip to content
SlateGitOrgPublic

About

An event-time interval join of checkout orders to payment authorisations with unpaid-order timeouts, equal to a DuckDB batch join under any arrival order, with bounded state.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

joinlapse

CI

joinlapse

joinlapse is an event-time interval join of checkout orders to payment authorisations. It emits one Joined record per distinct (order, authorisation) pair inside a 30 minute window and an Unpaid signal when no approved authorisation arrived in time, trimming its buffers with watermarks so state stays bounded. Its output is checked for exact equality with a DuckDB batch join over the same events, across many arrival interleavings. A processing-time join, the common hand-written version, runs on the same input so its false unpaid signals can be counted against ground truth. All data is synthetic.

The problem

An online retailer's order service and payment service publish to separate streams. Operations wants the authorisation latency of each order in real time, and an "unpaid order" signal when no approved authorisation arrives within 30 minutes of the order in event time, so that held stock can be released.

Hand-written joins fail in two ways. Some keep every order in memory forever. Others time orders out by wall-clock arrival, which during a payment-service backlog fires thousands of false "unpaid" signals and releases stock that was in fact paid for. Retried authorisations, redelivered messages and authorisations that arrive before their order make both worse.

How it works

flowchart LR
    O[Order stream] --> OW[Order watermark<br/>max ts - 5 s]
    P[Payment stream] --> PW[Auth watermark<br/>max ts - 15 s]
    OW --> J[Keyed interval join<br/>order and auth buffers per order_id]
    PW --> J
    J --> R[Joined pairs]
    J --> U[Unpaid timeouts]
    J --> L[Late side output]
    PW -. evicts orders past ts + 30 min .-> J
    OW -. evicts auths behind order time .-> J
    O --> D[DuckDB batch interval join]
    P --> D
    R --> C{Set equality}
    U --> C
    D --> C
Loading

Every arrival probes the buffer of the other side, so the result does not depend on which side arrives first. An authorisation matches an order when order.ts <= auth.ts <= order.ts + 30 min, both ends inclusive.

  • An order is evicted when the auth watermark is strictly greater than order.ts + 30 min. Every later authorisation is at or above that watermark, so none can match. If no APPROVED authorisation matched, Unpaid is emitted at that moment.
  • An authorisation is evicted when the order watermark is strictly greater than its timestamp, for the symmetric reason.
  • An event below its own stream's watermark is late. It goes to a side output with reason behind_watermark and is otherwise ignored.
  • Redelivered authorisations are absorbed: each buffered order remembers the auth ids it has already matched, so a pair is emitted once.
  • close() moves both watermarks to infinity and flushes every pending decision.

After every event the run asserts that no buffered order has ts + 30 min behind the auth watermark and no buffered auth is behind the order watermark.

Quick start

python3 -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"

joinlapse run --seed 5               # event-time join summary
joinlapse run --seed 5 --baseline    # processing-time join summary
joinlapse report --seed 5            # both joins side by side
joinlapse verify --seed 5            # stream versus DuckDB, exit 1 on mismatch
joinlapse verify --seed 5 --inject-bug evict-early

pytest -q

Example output

The seeded workload has 20,000 orders over four simulated hours with a lunchtime peak. 89% are approved after a lognormal delay (median 40 s) and 1% are slow approvals at 25 to 30 minutes, still inside the interval (together the 90% of orders that are paid in time without a retry). 3% are declined and approved on retry, 2% are approved after more than 30 minutes, 5% are never authorised, and 4% of auth messages are redelivered. The payment service publishes nothing for auths with event times from 01:40 to 02:25 and then drains its queue, so those auths arrive up to 45 minutes late.

joinlapse report --seed 5:

seed 5: 20000 orders, 20419 auth messages (redeliveries included)
ground truth: 1401 unpaid orders (DuckDB)

                                  event-time   processing-time
joined records                         19255             16836
unpaid signals                          1401              3738
false unpaid (paid in time)                0              2337
  of which backlog window                  0              2335
truly unpaid, not signalled                0                 0
state high-water, orders                9980              5131
state high-water, auths                  139              6668
auth latency p50                       40.9s             40.0s
auth latency p95                      323.1s            302.6s

The "of which backlog window" row counts orders placed from 01:10 to 02:25: the 01:40 to 02:25 backlog span extended back by the 30 minute interval, because an order placed in that extra half hour can have its authorisation held in the queue.

The processing-time baseline raises 2,337 unpaid signals for orders that were paid inside the interval, almost all of them in the backlog window. The event-time join raises none and misses none. The baseline holds fewer orders because it drops them early, which is the same behaviour that produces the false signals.

joinlapse verify --seed 5:

seed 5: 20000 orders, 20419 auth messages (redeliveries included)
stream joined   : 19255   duckdb joined: 19255
stream unpaid   : 1401   duckdb unpaid: 1401
late excluded   : 0
state bound     : held after every event (max 9980 orders, 139 auths)
MATCH

joinlapse verify --seed 5 --inject-bug evict-early evicts orders 60 seconds early, and the verifier catches it:

seed 5: 20000 orders, 20419 auth messages (redeliveries included)
injected bug    : orders are evicted 60 s before it is safe
stream joined   : 19215   duckdb joined: 19255
stream unpaid   : 1441   duckdb unpaid: 1401
late excluded   : 0
state bound     : held after every event (max 9830 orders, 139 auths)
MISMATCH: 80 differences (joined missing 40, extra 0; unpaid missing 0, extra 40)

Design decisions

  • Watermark per stream, eviction on the opposite watermark. Event-time state is bounded by the other stream's progress, not by a clock. The cost is that one stalled stream stalls the other's cleanup: during the seeded backlog buffered orders rise from 5,055 (the high-water mark in the 30 minutes before it) to 9,980, and fall back below 5,055 as soon as the queue drains.
  • A decision is final. Unpaid needs the auth watermark to pass the interval, and any later authorisation inside the interval would then be late and diverted. No APPROVED Joined can follow an Unpaid for the same order. The price is that a genuinely late payment never updates the decision; it lands in the side output.
  • Left join means "no APPROVED match". An order with only a DECLINED authorisation is unpaid. The Joined records for declined attempts are still emitted, so latency to a decline is observable.
  • Idempotence by remembered pair. Redeliveries are handled with a set of matched auth ids per buffered order instead of a global dedupe table, so the dedupe state is evicted together with the order.
  • Oracle excludes late events one copy at a time. A late redelivery of an on-time authorisation must not delete the on-time copy, so the oracle removes one delivered instance per side-output record before its SELECT DISTINCT. Late orders are removed from the order input the same way as late authorisations from the auth input.
  • Orders are assumed to have unique ids. A second order with a buffered id is ignored, with no side-output record.

Testing

pytest -q

69 tests pass in about 27 seconds on a loaded shared machine (9 to 11 seconds when idle), including a hypothesis property test (50 examples) that permutes arrival order within each stream's bound under several inter-stream lags and requires the output sets to equal the DuckDB result. Other tests cover a hand-computed fixture of boundary cases, the state bound after every event, the baseline's false unpaid signals, redeliveries, an injected late authorisation, finality of unpaid decisions, the 40 s latency median and the CLI.

Project layout

src/joinlapse/
  events.py     Order, Auth, Joined, Unpaid, Late and StreamWatermark
  join.py       IntervalJoin: keyed buffers, probing, eviction, side output
  baseline.py   ProcessingTimeJoin: the wall-clock timeout join
  oracle.py     DuckDB batch interval join and late-event exclusion
  state.py      state samples, the bound check, backlog recovery helpers
  replay.py     bound-respecting arrival interleavings and stream merging
  gen.py        seeded checkout and payment generator with the backlog
  pipeline.py   drives the joins, compares with the oracle, derived metrics
  cli.py        typer commands: run, verify, report
tests/          unit, property and seeded-run tests; tests/fixtures/hand_fixture.json

License

MIT. See LICENSE.

About

An event-time interval join of checkout orders to payment authorisations with unpaid-order timeouts, equal to a DuckDB batch join under any arrival order, with bounded state.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages