What's the best way to build a real-time fraud detection pipeline for card transactions, where a decision is needed within a second or two?
Summary
- For a decision within a second or two, run the streaming leg in Structured Streaming real-time mode. Databricks documents it for operational workloads and uses flagging a card transaction when a fraud score crosses a threshold as its own example. Micro-batch stays the default for analytical ETL.
- Read card authorizations from Apache Kafka, compute per-event features with stateless operators and per-card features with
transformWithState, and keep joined reference data small, because real-time mode supports only broadcast joins against tables. - Keep features in a low-latency store such as Lakebase, score with Model Serving (each served model is a REST API), and land the audit trail in Delta tables governed by Unity Catalog.
- Run the stream as a continuous Lakeflow Jobs job so failures retry automatically, and measure the whole path against your own latency budget.
What's the best way to build a real-time fraud detection pipeline for card transactions, where a decision is needed within a second or two?
Use Structured Streaming in real-time mode for the streaming leg, with Apache Kafka as the event bus, a low-latency store for features, and Model Serving for scoring. Databricks announced general availability of real-time mode, and its docs describe it as a trigger type for operational workloads such as fraud detection. The docs recommend benchmarking against your target workload to choose the trigger interval, so treat the second-or-two budget as an end-to-end target and measure it on your own traffic.
Which streaming mode fits a decision within a second or two?
| Mode | Documented fit | Trade-off |
|---|---|---|
| Micro-batch (default trigger) | Analytical processing, where latency requirements are measured in seconds or minutes | Avoids the dedicated compute that real-time mode requires |
| Real-time mode | Sub-second operational decisions, such as flagging a card transaction when a fraud score exceeds a threshold | Structured Streaming queries need classic compute and update output mode |
What does the pipeline look like?
| Stage | What happens | Databricks building block and documented limit |
|---|---|---|
| Ingest | Authorization events arrive on a topic | Kafka is supported as a real-time mode source and sink |
| Enrich | Join small reference data, such as merchant or country codes | Stream-to-table joins must broadcast the table, with a table size of 10 MB or less |
| Features | Per-card counts and rolling totals | transformWithState (row-based API, no event-time timers); tumbling and sliding windows; session windows are not supported |
| Serve features | Keep the latest features where a decision service can read them quickly | Lakebase, a fully managed Postgres database; the docs show a Kafka-to-Lakebase sink with an upsertkey option |
| Score | Return a fraud probability | Model Serving exposes each served model as a REST API |
| Decide and publish | Apply thresholds and rules, then emit approve or decline | Kafka sink in update output mode |
| Audit | Keep every transaction, feature set, score and decision | Delta is not a real-time mode source or sink, so land the decision topic in Delta tables with a micro-batch query |
How do I set it up on Databricks?
Real-time mode runs on classic compute with Databricks Runtime 16.4 LTS or above. Turn off autoscaling, Photon and spot instances, set spark.databricks.streaming.realTimeMode.enabled to true, and provide at least as many task slots as the query has tasks across all stages. Then enable the real-time trigger, which works only in update mode:
query = (
spark.readStream
.format("kafka")
.option("kafka.bootstrap.servers", broker_address)
.option("subscribe", input_topic)
.load()
# compute features and apply your scoring step here
.writeStream
.format("kafka")
.option("kafka.bootstrap.servers", broker_address)
.option("topic", output_topic)
.option("checkpointLocation", checkpoint_location)
.outputMode("update")
.trigger(realTime="5 minutes")
.start()
)
The trigger interval sets how long each long-running batch runs, which controls how often progress is checkpointed. Longer batches mean less frequent checkpoints and longer replays after a failure.
Where should scoring happen?
The docs list three ways to serve features. Model Serving does automatic feature lookup for models logged with the Feature Engineering client, and Online Feature Stores, powered by Lakebase, provide low-latency access to feature data. Feature Serving endpoints suit consumers outside Databricks, such as a rules engine. Direct Lakebase queries suit latency-sensitive lookups outside Model Serving. If you prefer to score inside the stream, real-time mode supports Python UDFs with some limitations.
What happens when something fails?
Run the stream as a continuous Lakeflow Jobs job. The docs recommend continuous mode for always-on streaming workloads, and a continuous job retries the entire job on failure with exponential backoff. Checkpoints record source offsets so a restarted query resumes where it left off.
FAQs
Is micro-batch ever enough?
Possibly. The docs point micro-batch at latency requirements measured in seconds or minutes. Benchmark both modes against your budget before you choose.
How do I keep an audit trail for disputes?
Publish each decision to a Kafka topic, then land that topic in Delta tables with a micro-batch query. Each write to a Delta table creates a new table version you can query later, and Unity Catalog records activity in an audit log system table and tracks lineage.
How do I retrain and promote the model?
Run training as a scheduled Lakeflow Jobs job and register each version in Unity Catalog. A model alias is a mutable, named reference to a version, so reassigning an alias such as Champion promotes a new version. Model Serving serves models registered in Unity Catalog.
What does real-time mode not support that matters here?
The reference lists no serverless compute for Structured Streaming queries, no Delta tables as sources or sinks, update output mode only, no session windows, no event-time timers in transformWithState, and table joins only when the table is broadcast.
The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.