Fraud detection with Spark: streaming rules and monthly scores.
Rules on the stream of card transactions as they arrive, and a score per customer from 90 days of history, checked against what happened. Spark jobs on a Kubernetes cluster you operate, the alerts a Delta table your fraud team queries in SQL.
94 flagged at 0.23.
48 disputed.
The 8,073 merchants by their latest fraud_risk_score of 1 Nov 2026, the share disputed in amber
How to tell whether a fraud model works
A fraud score is only as good as the disputes that follow it. Fraud Detection Effectiveness bands each merchant’s latest score and sets the bands against the disputes of the last 90 days.
8,073 flagged of 8,073 merchants
3.3% of the flagged were disputed
100.0% of the disputed are flagged
- disputed, not flagged
- 0
- flagged, never disputed
- 7,808
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.00 times as often as all of them
5,454 flagged of 8,073 merchants
3.9% of the flagged were disputed
79.6% of the disputed are flagged
- disputed, not flagged
- 54
- flagged, never disputed
- 5,243
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.18 times as often as all of them
5,454 flagged of 8,073 merchants
3.9% of the flagged were disputed
79.6% of the disputed are flagged
- disputed, not flagged
- 54
- flagged, never disputed
- 5,243
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.18 times as often as all of them
5,454 flagged of 8,073 merchants
3.9% of the flagged were disputed
79.6% of the disputed are flagged
- disputed, not flagged
- 54
- flagged, never disputed
- 5,243
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.18 times as often as all of them
5,454 flagged of 8,073 merchants
3.9% of the flagged were disputed
79.6% of the disputed are flagged
- disputed, not flagged
- 54
- flagged, never disputed
- 5,243
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.18 times as often as all of them
5,454 flagged of 8,073 merchants
3.9% of the flagged were disputed
79.6% of the disputed are flagged
- disputed, not flagged
- 54
- flagged, never disputed
- 5,243
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.18 times as often as all of them
5,454 flagged of 8,073 merchants
3.9% of the flagged were disputed
79.6% of the disputed are flagged
- disputed, not flagged
- 54
- flagged, never disputed
- 5,243
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.18 times as often as all of them
2,344 flagged of 8,073 merchants
4.7% of the flagged were disputed
41.5% of the disputed are flagged
- disputed, not flagged
- 155
- flagged, never disputed
- 2,234
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.43 times as often as all of them
2,344 flagged of 8,073 merchants
4.7% of the flagged were disputed
41.5% of the disputed are flagged
- disputed, not flagged
- 155
- flagged, never disputed
- 2,234
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.43 times as often as all of them
2,314 flagged of 8,073 merchants
4.6% of the flagged were disputed
40.4% of the disputed are flagged
- disputed, not flagged
- 158
- flagged, never disputed
- 2,207
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.41 times as often as all of them
2,314 flagged of 8,073 merchants
4.6% of the flagged were disputed
40.4% of the disputed are flagged
- disputed, not flagged
- 158
- flagged, never disputed
- 2,207
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.41 times as often as all of them
1,324 flagged of 8,073 merchants
6.2% of the flagged were disputed
30.9% of the disputed are flagged
- disputed, not flagged
- 183
- flagged, never disputed
- 1,242
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.89 times as often as all of them
1,324 flagged of 8,073 merchants
6.2% of the flagged were disputed
30.9% of the disputed are flagged
- disputed, not flagged
- 183
- flagged, never disputed
- 1,242
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.89 times as often as all of them
1,323 flagged of 8,073 merchants
6.2% of the flagged were disputed
30.9% of the disputed are flagged
- disputed, not flagged
- 183
- flagged, never disputed
- 1,241
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.89 times as often as all of them
1,323 flagged of 8,073 merchants
6.2% of the flagged were disputed
30.9% of the disputed are flagged
- disputed, not flagged
- 183
- flagged, never disputed
- 1,241
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.89 times as often as all of them
1,280 flagged of 8,073 merchants
5.9% of the flagged were disputed
28.7% of the disputed are flagged
- disputed, not flagged
- 189
- flagged, never disputed
- 1,204
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.81 times as often as all of them
1,280 flagged of 8,073 merchants
5.9% of the flagged were disputed
28.7% of the disputed are flagged
- disputed, not flagged
- 189
- flagged, never disputed
- 1,204
- disputed among all merchants
- 3.3%
flagged merchants are disputed 1.81 times as often as all of them
94 flagged of 8,073 merchants
51.1% of the flagged were disputed
18.1% of the disputed are flagged
- disputed, not flagged
- 217
- flagged, never disputed
- 46
- disputed among all merchants
- 3.3%
flagged merchants are disputed 15.56 times as often as all of them
94 flagged of 8,073 merchants
51.1% of the flagged were disputed
18.1% of the disputed are flagged
- disputed, not flagged
- 217
- flagged, never disputed
- 46
- disputed among all merchants
- 3.3%
flagged merchants are disputed 15.56 times as often as all of them
62 flagged of 8,073 merchants
58.1% of the flagged were disputed
13.6% of the disputed are flagged
- disputed, not flagged
- 229
- flagged, never disputed
- 26
- disputed among all merchants
- 3.3%
flagged merchants are disputed 17.69 times as often as all of them
62 flagged of 8,073 merchants
58.1% of the flagged were disputed
13.6% of the disputed are flagged
- disputed, not flagged
- 229
- flagged, never disputed
- 26
- disputed among all merchants
- 3.3%
flagged merchants are disputed 17.69 times as often as all of them
58 flagged of 8,073 merchants
58.6% of the flagged were disputed
12.8% of the disputed are flagged
- disputed, not flagged
- 231
- flagged, never disputed
- 24
- disputed among all merchants
- 3.3%
flagged merchants are disputed 17.86 times as often as all of them
58 flagged of 8,073 merchants
58.6% of the flagged were disputed
12.8% of the disputed are flagged
- disputed, not flagged
- 231
- flagged, never disputed
- 24
- disputed among all merchants
- 3.3%
flagged merchants are disputed 17.86 times as often as all of them
58 flagged of 8,073 merchants
58.6% of the flagged were disputed
12.8% of the disputed are flagged
- disputed, not flagged
- 231
- flagged, never disputed
- 24
- disputed among all merchants
- 3.3%
flagged merchants are disputed 17.86 times as often as all of them
58 flagged of 8,073 merchants
58.6% of the flagged were disputed
12.8% of the disputed are flagged
- disputed, not flagged
- 231
- flagged, never disputed
- 24
- disputed among all merchants
- 3.3%
flagged merchants are disputed 17.86 times as often as all of them
13 flagged of 8,073 merchants
84.6% of the flagged were disputed
4.2% of the disputed are flagged
- disputed, not flagged
- 254
- flagged, never disputed
- 2
- disputed among all merchants
- 3.3%
flagged merchants are disputed 25.78 times as often as all of them
13 flagged of 8,073 merchants
84.6% of the flagged were disputed
4.2% of the disputed are flagged
- disputed, not flagged
- 254
- flagged, never disputed
- 2
- disputed among all merchants
- 3.3%
flagged merchants are disputed 25.78 times as often as all of them
13 flagged of 8,073 merchants
84.6% of the flagged were disputed
4.2% of the disputed are flagged
- disputed, not flagged
- 254
- flagged, never disputed
- 2
- disputed among all merchants
- 3.3%
flagged merchants are disputed 25.78 times as often as all of them
13 flagged of 8,073 merchants
84.6% of the flagged were disputed
4.2% of the disputed are flagged
- disputed, not flagged
- 254
- flagged, never disputed
- 2
- disputed among all merchants
- 3.3%
flagged merchants are disputed 25.78 times as often as all of them
13 flagged of 8,073 merchants
84.6% of the flagged were disputed
4.2% of the disputed are flagged
- disputed, not flagged
- 254
- flagged, never disputed
- 2
- disputed among all merchants
- 3.3%
flagged merchants are disputed 25.78 times as often as all of them
13 flagged of 8,073 merchants
84.6% of the flagged were disputed
4.2% of the disputed are flagged
- disputed, not flagged
- 254
- flagged, never disputed
- 2
- disputed among all merchants
- 3.3%
flagged merchants are disputed 25.78 times as often as all of them
0 flagged of 8,073 merchants
0.0% of the flagged were disputed
0.0% of the disputed are flagged
- disputed, not flagged
- 265
- flagged, never disputed
- 0
- disputed among all merchants
- 3.3%
flagged merchants are disputed 0 times as often as all of them
The query’s verdict per band
| fraud_risk_band | merchants_in_band | pct_with_disputes | model_assessment |
|---|---|---|---|
| HIGH (0.30-0.59) | 13 | 84.62% | CALIBRATED |
| MEDIUM (0.15-0.29) | 2,301 | 4.17% | CALIBRATED |
| LOW (0.05-0.14) | 5,759 | 2.74% | CALIBRATED |
OVER_FLAGGING: a HIGH or CRITICAL band with under 20% disputed. UNDER_FLAGGING: a MINIMAL or LOW band with over 10% disputed.
The query
-- name: Payments/Compliance/Fraud Detection Effectiveness
-- engine: SPARK_SQL
-- maxRows: 1000
-- description: Assess fraud model quality by comparing risk scores against actual dispute outcomes per risk band
WITH latest_risk AS (
SELECT
merchant_id,
chargeback_risk_score,
fraud_risk_score,
txn_count_90d,
chargeback_count_90d,
chargeback_ratio_90d,
CASE
WHEN fraud_risk_score >= 0.60 THEN 'CRITICAL (0.60-1.00)'
WHEN fraud_risk_score >= 0.30 THEN 'HIGH (0.30-0.59)'
WHEN fraud_risk_score >= 0.15 THEN 'MEDIUM (0.15-0.29)'
WHEN fraud_risk_score >= 0.05 THEN 'LOW (0.05-0.14)'
ELSE 'MINIMAL (0.00-0.04)'
END AS fraud_risk_band,
CASE
WHEN chargeback_risk_score >= 0.60 THEN 'CRITICAL (0.60-1.00)'
WHEN chargeback_risk_score >= 0.30 THEN 'HIGH (0.30-0.59)'
WHEN chargeback_risk_score >= 0.15 THEN 'MEDIUM (0.15-0.29)'
WHEN chargeback_risk_score >= 0.05 THEN 'LOW (0.05-0.14)'
ELSE 'MINIMAL (0.00-0.04)'
END AS chargeback_risk_band,
ROW_NUMBER() OVER (
PARTITION BY merchant_id
ORDER BY score_date DESC
) AS rn
FROM payment_processing.merchant_risk_scores
),
scored_merchants AS (
SELECT * FROM latest_risk WHERE rn = 1
),
merchant_disputes AS (
SELECT
d.merchant_id,
COUNT(*) AS total_disputes,
SUM(CASE WHEN d.status = 'merchant_lost' THEN 1 ELSE 0 END) AS disputes_lost,
SUM(CASE WHEN d.reason_code IN ('10.4', '10.1', '10.5') THEN 1 ELSE 0 END) AS fraud_disputes,
ROUND(SUM(d.dispute_amount), 2) AS total_dispute_amount
FROM payment_processing.disputes d
WHERE d.filed_date >= DATE_ADD(CURRENT_DATE, -90)
GROUP BY d.merchant_id
),
combined AS (
SELECT
sm.merchant_id,
sm.fraud_risk_band,
sm.chargeback_risk_band,
sm.fraud_risk_score,
sm.chargeback_risk_score,
sm.txn_count_90d,
COALESCE(md.total_disputes, 0) AS actual_disputes,
COALESCE(md.disputes_lost, 0) AS disputes_lost,
COALESCE(md.fraud_disputes, 0) AS fraud_disputes,
COALESCE(md.total_dispute_amount, 0) AS dispute_amount,
CASE WHEN md.total_disputes > 0 THEN 1 ELSE 0 END AS had_disputes
FROM scored_merchants sm
LEFT JOIN merchant_disputes md
ON sm.merchant_id = md.merchant_id
)
SELECT
fraud_risk_band,
COUNT(*) AS merchants_in_band,
SUM(had_disputes) AS merchants_with_disputes,
ROUND(SUM(had_disputes) * 100.0 / COUNT(*), 2) AS pct_with_disputes,
ROUND(AVG(fraud_risk_score), 1) AS avg_fraud_score,
ROUND(AVG(chargeback_risk_score), 1) AS avg_chargeback_score,
SUM(actual_disputes) AS total_disputes,
SUM(fraud_disputes) AS total_fraud_disputes,
ROUND(SUM(dispute_amount), 2) AS total_dispute_amount,
ROUND(AVG(CASE WHEN had_disputes = 1 THEN fraud_risk_score ELSE NULL END), 1) AS avg_score_true_positives,
ROUND(AVG(CASE WHEN had_disputes = 0 THEN fraud_risk_score ELSE NULL END), 1) AS avg_score_false_positives,
CASE
WHEN fraud_risk_band IN ('CRITICAL (0.60-1.00)', 'HIGH (0.30-0.59)')
AND SUM(had_disputes) * 100.0 / COUNT(*) < 20
THEN 'OVER_FLAGGING'
WHEN fraud_risk_band IN ('MINIMAL (0.00-0.04)', 'LOW (0.05-0.14)')
AND SUM(had_disputes) * 100.0 / COUNT(*) > 10
THEN 'UNDER_FLAGGING'
ELSE 'CALIBRATED'
END AS model_assessment
FROM combined
GROUP BY fraud_risk_band
ORDER BY
CASE fraud_risk_band
WHEN 'CRITICAL (0.60-1.00)' THEN 1
WHEN 'HIGH (0.30-0.59)' THEN 2
WHEN 'MEDIUM (0.15-0.29)' THEN 3
WHEN 'LOW (0.05-0.14)' THEN 4
WHEN 'MINIMAL (0.00-0.04)' THEN 5
END
Real-time fraud rules with Spark Structured Streaming
Rules on the stream judge each transaction as it arrives, and a monthly job scores each customer from 90 days of history. The stream writes only the rows its rules flag.
is_large_transactionfalseis_off_hoursfalseis_high_amount_cardfalse
NONE
not written: no rule fired
is_large_transactionfalseis_off_hoursfalseis_high_amount_cardfalse
NONE
not written: no rule fired
is_large_transactionfalseis_off_hourstrueis_high_amount_cardfalse
MEDIUM: Off-hours activity
appended to fraud_alerts
is_large_transactionfalseis_off_hourstrueis_high_amount_cardfalse
MEDIUM: Off-hours activity
appended to fraud_alerts
is_large_transactionfalseis_off_hoursfalseis_high_amount_cardfalse
NONE
not written: no rule fired
is_large_transactionfalseis_off_hoursfalseis_high_amount_cardtrue
MEDIUM: High-value card purchase
appended to fraud_alerts
is_large_transactionfalseis_off_hourstrueis_high_amount_cardfalse
MEDIUM: Off-hours activity
appended to fraud_alerts
is_large_transactionfalseis_off_hourstrueis_high_amount_cardtrue
MEDIUM: Off-hours activity
appended to fraud_alerts
is_large_transactiontrueis_off_hoursfalseis_high_amount_cardfalse
HIGH: Large transaction >15K EUR
appended to fraud_alerts
is_large_transactiontrueis_off_hoursfalseis_high_amount_cardtrue
HIGH: Large transaction >15K EUR
appended to fraud_alerts
is_large_transactiontrueis_off_hourstrueis_high_amount_cardfalse
CRITICAL: Large off-hours transaction
appended to fraud_alerts
is_large_transactiontrueis_off_hourstrueis_high_amount_cardtrue
CRITICAL: Large off-hours transaction
appended to fraud_alerts
The jobs and the tables
alerts = (enriched
.withColumn("is_large_transaction",
F.when(F.col("amount") > 15000, F.lit(True)).otherwise(F.lit(False)))
.withColumn("is_off_hours",
F.when((F.col("hour_of_day") < 5) | (F.col("hour_of_day") >= 23),
F.lit(True)).otherwise(F.lit(False)))
.withColumn("is_high_amount_card",
F.when((F.col("transaction_type") == "card_purchase") & (F.col("amount") > 5000),
F.lit(True)).otherwise(F.lit(False)))
.withColumn("is_suspicious",
F.col("is_large_transaction") | F.col("is_off_hours") | F.col("is_high_amount_card"))
.withColumn("alert_type",
F.when(F.col("is_large_transaction") & F.col("is_off_hours"),
F.lit("CRITICAL: Large off-hours transaction"))
.when(F.col("is_large_transaction"),
F.lit("HIGH: Large transaction >15K EUR"))
.when(F.col("is_off_hours"),
F.lit("MEDIUM: Off-hours activity"))
.when(F.col("is_high_amount_card"),
F.lit("MEDIUM: High-value card purchase"))
.otherwise(F.lit("NONE")))
.withColumn("risk_score",
F.round(
F.when(F.col("is_large_transaction") & F.col("is_off_hours"), F.lit(0.95))
.when(F.col("is_large_transaction"), F.lit(0.75))
.when(F.col("is_off_hours"), F.lit(0.45))
.when(F.col("is_high_amount_card"), F.lit(0.40))
.otherwise(F.lit(0.05)),
2))
# Only write alerts (not every transaction)
.filter(F.col("is_suspicious") == True)
.select(
"transaction_id", "customer_id", "account_id",
"country", "currency", "channel", "transaction_type",
"amount", "event_timestamp", "alert_type", "risk_score",
"is_large_transaction", "is_off_hours", "is_high_amount_card")
)txn_features = (read_table(spark, "transactions")
.filter((F.col("event_date") >= lookback) & (F.col("event_date") < score_month))
.withColumn("rate_date", F.trunc("event_date", "month"))
.join(F.broadcast(rates), ["currency", "rate_date"], "left")
.withColumn("amount_eur", F.col("amount") * F.col("inverse_rate"))
.join(read_table(spark, "accounts").select("account_id", "customer_id"), "account_id")
.groupBy("customer_id")
.agg(
F.count("*").alias("txn_count_90d"),
F.sum("amount_eur").alias("txn_total_90d"),
F.max("amount_eur").alias("txn_max_90d"),
F.sum(F.when(F.col("status") == "failed", 1).otherwise(0)).alias("failed_txn_count_90d"),
F.sum(F.when(F.col("status") == "reversed", 1).otherwise(0)).alias("reversed_txn_count_90d"),
F.countDistinct("country").alias("unique_countries_90d"),
F.sum(F.when((F.hour("event_timestamp") < 6) | (F.hour("event_timestamp") > 22), 1).otherwise(0)).alias("offhours_txn_count_90d"),
))core_banking.fraud_alertsWritten by the streaming job: one row per suspicious transaction.Appended every 10 seconds.transaction_id | The transaction |
|---|---|
customer_id | Its customer |
account_id | Its account |
channel | Where it came from |
transaction_type | Payment, transfer, withdrawal, deposit or card purchase |
amount | Its amount |
event_timestamp | When it happened |
alert_type | The worst rule that fired, as text |
risk_score | 0.95, 0.75, 0.45 or 0.40 |
is_large_transaction | Over 15,000 |
is_off_hours | From 23:00 to 04:59 |
is_high_amount_card | A card purchase over 5,000 |
core_banking.customer_risk_scoresWritten by banking-monthly-risk on the 1st: one row per active customer and month.Partitioned by score_date.customer_id | The customer scored |
|---|---|
score_date | The 1st of the month scored |
credit_risk_score | Between 0 and 1 |
fraud_risk_score | Between 0 and 1 |
model_version | The version of the scoring |
txn_count_90d | Transactions in the 90 days before |
offhours_txn_count_90d | Of them, before 06:00 or after 22:00 |
| Streaming rules | Monthly scoring | |
|---|---|---|
| Runs | without end, a micro-batch every 10 seconds | 0 6 1 * *, at 06:00 on the 1st |
| Reads | each transaction as it arrives | 90 days of transactions per customer |
| Decides on | the amount, the hour, the type | counts and ratios of the 90 days |
| Writes | fraud_alerts, suspicious rows only | customer_risk_scores, every active customer |
How to run a fraud detection pipeline on Ilum, step by step
Six steps from an empty cluster to a stream of alerts, monthly scores and a check of the model. Each uses a part of Ilum that comes with the default install, MLflow aside.
An empty cluster
A stream of alerts, monthly scores
- Helm chart
Add the chart repository and install it on any CNCF-compliant Kubernetes. Spark, the SQL editor, Jupyter, the Hive Metastore and MinIO come with it.
helm repo add ilum https://charts.ilum.cloud helm install ilum ilum/ilum - Spark
Write the transactions, accounts and customers of your core system into Delta tables of
core_bankingwith a Spark job that reads its exports from S3, GCS, Azure Blob or HDFS. - Spark
Submit the streaming job as a Spark job. Point its
readStreamat your payment feed and leave it running. It writesfraud_alertsevery ten seconds. - Schedules
Upload the scoring job and give it a schedule with the cron
0 6 1 * *and three retries. Ilum runs it as a Kubernetes CronJob. How a schedule fires. - SQL editor
Put the week’s alerts, the scores and the effectiveness query in one notebook in the SQL editor, with Markdown for what each number means.
- MLflow
Install MLflow as a module with one Helm flag, train in Jupyter on the same tables, and log each model version there. Model development on Ilum.
Questions and answers
How do I build a real-time fraud detection pipeline with Spark?
Read the transactions as a stream with Structured Streaming, turn each rule into a boolean column, keep the rows where any rule fires and append them to a Delta table in micro-batches with a checkpoint. On Ilum that is a Spark job on Kubernetes that runs until stopped.
Rules or a model for fraud detection?
Both. Rules on the stream catch what a single transaction shows at once, a large amount at night. A score from 90 days of a customer’s history catches what builds up, reversals and countries. Then check the score against the disputes that followed.
How do you measure whether a fraud model works?
Band the scores and count, per band, how many scored accounts were then disputed. If the top bands are disputed no more often than the rest, the model does not separate them. Fraud Detection Effectiveness calls a top band under 20% OVER_FLAGGING and a bottom band over 10% UNDER_FLAGGING. The disputes, and the ratio the card schemes hold a merchant to, are the subject of chargeback monitoring.
Where should a fraud score threshold go?
Where the merchants it flags are still mostly the ones disputed. On the scored merchants a line at 0.23 flags 94, half of them disputed, and one step lower at 0.21 adds 1,186 more, of whom 2.4% were disputed: the extra work buys almost nothing.
Where should streaming fraud alerts be written?
To a table the fraud team can query, not a log: a Delta table appended every few seconds, with a checkpoint so a restart neither loses nor repeats rows, and with the rules that fired kept as columns.
Can fraud detection run on premises?
Yes. Ilum installs with Helm on any CNCF-compliant Kubernetes, bare metal, OpenShift and Rancher included, and keeps the tables in the object storage you give it, MinIO by default.
Is Ilum open source?
No. Ilum Community is free to run under a license from Ilum. It is built on open-source engines such as Apache Spark, Trino and DuckDB.
Contact
Tell us about your cluster.
Name one workload you want to move.
Tell us where it runs today.
We answer with a plan, not a deck.
We reply within two working days.
