← Projects Data engineering Personal project · 2026
When the fraud backtest lies
A streaming fraud-scoring pipeline built to answer one question: does the model see the same features in serving as in training? On 400,903 simulated payments, every one of 10.8 million served feature values matched its offline rebuild, and two deliberately leaky joins showed what happens when they don't.
- 0 of 10.8M
- Served vs offline feature mismatches
- 0.76 → 0.32
- Leaky join AP, backtest → online
- 0.925
- Point-in-time model AP online
- 0.16 ms
- In-process scoring p50
Architecture
- 01 Simulated stream 400,903 payments, 70 days
- 02 Online features Redis-style store, 27 features
- 03 Scoring service LightGBM via FastAPI + MLflow
- 04 Offline rebuild DuckDB point-in-time joins
- 05 Skew test zero-tolerance compare
- 06 Celery rescoring shadow scores per model
The problem
Fraud models at a payments company live on two code paths. In serving, velocity features are computed in milliseconds from an event stream and an online store. In training, the same features are rebuilt offline from history. Those two paths can quietly drift apart, and the offline metrics won’t tell you.
Labels make it worse. Fraud is confirmed by a chargeback that arrives days after the payment. If the training table lets a chargeback show up earlier than it really would, the model learns from information production never has.
I wanted to measure both effects on a stream where I knew the ground truth. All of the numbers below describe a simulation, not real fraud rates.
What I built
The data is a seeded synthetic stream: 400,903 payments over 70 days, with three injected fraud patterns (card testing, account takeover, merchant collusion) and chargebacks delayed by a median of six days. The first version was too easy, with the streaming model at AP 0.98, so I added online purchases, rounded amounts, device rotation and slower takeovers before the final run.
There are 27 features. Every windowed one looks strictly before the payment. The online path reads them from a Redis-style store of sorted sets, scores the payment with LightGBM, then writes the payment into state. The offline path rebuilds the same features with DuckDB window functions. The model is served by FastAPI from an MLflow registry, and Celery rescores logged traffic in batches.
The skew test compares every feature vector the service actually used with the offline row for the same payment, with zero tolerance. A test that always passes proves nothing, so I also ran it against two known defects.
What the numbers show
Online equals offline. All 400,903 payments and all 27 features matched exactly, across micro-batch, single-payment and HTTP serving paths.
The test catches real defects. Updating state before reading it gave every payment at least one wrong value. Delivering events with a random lag (mean 60 s) changed 733 of 80,158 payments, mostly in the merchant features.
Leaky joins look better offline and do worse online. Two pipelines leaked chargeback labels: one dated chargebacks at the fraud’s own timestamp, one joined the latest snapshot. Both had higher validation AP than the point-in-time pipeline (0.71 and 0.79 vs 0.65). Their backtests promised 0.87 and 0.76. Online they reached 0.69 and 0.32. The point-in-time model’s backtest equalled its online result, 0.925.
The leaky models had learned to lean on chargeback features, which carried 29% and 63% of their total |SHAP| against 5% for the point-in-time model. For fresh fraud those features are zero in production.
Streaming features carry the signal. The same LightGBM on five transaction fields alone reached AP 0.366, and a four-rule engine 0.317. At a 0.46% review rate the point-in-time model flagged 90% of fraud payments at 72% precision.
Account takeover is the hard case. Card testing and collusion were found almost completely. Takeover AP was 0.18: nine of the 10 cases got an alert, but only 51% of their payments did.
Serving took 0.16 ms per payment at the median in process and 1.17 ms over local HTTP.
Limits
- Simulated data. The absolute AP values say nothing about real fraud. What should transfer is the mechanism: point-in-time features match the served ones, and early labels inflate offline metrics.
- The rules know the generator. I wrote them with knowledge of the injected patterns, which favours them.
- Few cases. The test window holds 10 takeover cases and 2 colluding merchants.
- Infrastructure partly exercised. The Kafka adapter never talked to a broker and the Redis
adapter only ran on
fakeredis. The Docker Compose stack was provided but not run. - Laptop latency. One process, no network, no concurrent load. Real p99 will be dominated by network and store round trips.
What I’d do differently
Scale past one partition. The consumer needs every event in timestamp order, so the topic has one partition. Scaling out means re-keying topics per entity and merging their features.
Add watermarks. The 60 s lag scenario shows what late events do without them.
Expire novelty keys. First-seen markers for card and device pairs are kept forever. A real store would expire them, and that changes the “new device” features.