In the financial services sector, the gap between a fraudulent transaction occurring and its detection is where losses happen. Traditional architectures—where data is ETL’d from a warehouse to a separate data science environment (like Python/Spark), processed, and sent back—introduce unavoidable latency. In the age of instant payments (FedNow, SEPA Instant), a 15-minute lag is 14 minutes and 59 seconds too long.
Furthermore, the operational overhead of maintaining separate ML infrastructure creates security vulnerabilities and compliance nightmares.
BigQuery ML (BQML) fundamentally shifts this paradigm by bringing the compute to the data. By operationalizing machine learning directly within the data warehouse, we are helping clients build “Zero-Copy” architectures that reduce latency, lower TCO, and simplify governance.
The Strategic Advantage: Why In-Warehouse ML?
It is not just about writing SQL instead of Python; it is about Data Gravity.
When you decouple storage and compute for ML, you pay a “tax” in egress costs, serialization time, and security audits. BigQuery ML allows us to treat the model as just another database object (like a table or view).
For our FinTech and Banking clients, this translates to:
- Compliance by Default: Sensitive PII (Personally Identifiable Information) never leaves the encrypted, governed environment of BigQuery.
- Democratized Data Science: SQL-savvy data analysts can contribute to feature engineering and initial modeling, bridging the gap between domain experts and ML engineers.
- Real-Time Scalability: BigQuery’s serverless architecture handles the scaling. whether you are scoring 100 transactions or 100 million, the infrastructure adapts automatically.
Architecting the Pipeline
To build a production-grade fraud engine, we look beyond simple model training. We implement a modern ELT (Extract, Load, Transform) architecture.
1. Ingestion and Feature Engineering
Real-time fraud detection requires real-time data. We typically recommend an architecture using Pub/Sub and Dataflow to stream transaction logs directly into BigQuery.
However, raw data isn’t enough. Fraud is often context-dependent. A $500 purchase is normal for one user but suspicious for another.
Strategic Insight: The Power of Window Functions In BigQuery, we can generate complex behavioral features using standard SQL window functions, which are highly optimized for distributed processing.
CREATE OR REPLACE TABLE analytics.fraud_features AS
SELECT
t.transaction_id,
t.customer_id,
t.amount,
t.merchant_category,
-- Velocity: Is the user transacting faster than usual?
COUNT(*) OVER (
PARTITION BY t.customer_id
ORDER BY t.transaction_time
RANGE BETWEEN INTERVAL 1 HOUR PRECEDING AND CURRENT ROW
) as txn_velocity_1h,
-- Deviation: Is this amount an outlier for this specific user?
(t.amount - AVG(t.amount) OVER (
PARTITION BY t.customer_id
ORDER BY t.transaction_time
ROWS BETWEEN 100 PRECEDING AND 1 PRECEDING
)) / NULLIF(STDDEV(t.amount) OVER (
PARTITION BY t.customer_id
ORDER BY t.transaction_time
ROWS BETWEEN 100 PRECEDING AND 1 PRECEDING
), 0) as z_score_amount,
t.is_fraud
FROM raw_data.transactions t;
2. Handling Class Imbalance ( The “Fraud Problem”)
One of the biggest challenges in fraud detection is class imbalance. Legitimate transactions vastly outnumber fraudulent ones (often 99.9% vs 0.1%). A standard model will simply guess “legitimate” every time and achieve 99.9% accuracy while catching zero fraud.
BigQuery ML solves this with the auto_class_weights option. This internally re-weights the loss function, forcing the model to pay more attention to the minority (fraud) class during training.
CREATE OR REPLACE MODEL models.fraud_classifier_v1
OPTIONS(
model_type='BOOSTED_TREE_CLASSIFIER', -- XGboost equivalent
input_label_cols=['is_fraud'],
auto_class_weights=TRUE, -- Critical for imbalanced datasets
data_split_method='SEQ', -- Prevent time-travel leakage
data_split_col='transaction_time',
max_iterations=50,
enable_global_explain=TRUE
) AS
SELECT * EXCEPT(transaction_id, transaction_time)
FROM analytics.fraud_features
WHERE transaction_time < '2025-01-01';
Note: We use data_split_method='SEQ' rather than random splitting. In fraud, you must train on the past and test on the future to simulate real-world conditions.
3. Real-Time Inference at Scale
Once the model is trained, inference happens via the ML.PREDICT function. This can be integrated into a dashboard for analysts or, more importantly, exposed via an API for real-time transaction blocking.
SELECT
transaction_id,
predicted_is_fraud,
probs.prob as fraud_probability,
-- Flag for manual review if probability is borderline (e.g., 0.6 - 0.8)
CASE
WHEN probs.prob > 0.8 THEN 'BLOCK'
WHEN probs.prob > 0.6 THEN 'MANUAL_REVIEW'
ELSE 'APPROVE'
END as action_recommendation
FROM ML.PREDICT(MODEL models.fraud_classifier_v1,
(SELECT * FROM streaming.incoming_transactions)
),
UNNEST(predicted_is_fraud_probs) as probs
WHERE probs.label = 1;
Production Considerations: The MLOps Layer
Writing the SQL is the easy part. Operationalizing it is where we add value.
Continuous Evaluation and Retraining
Fraud patterns evolve. A model trained on 2024 data will likely fail to detect 2025 attack vectors. We implement Vertex AI Pipelines to orchestrate:
- Drift Detection: Comparing the distribution of live data vs. training data.
- Automated Retraining: Triggering a new
CREATE OR REPLACE MODELjob when performance dips below a threshold (e.g., Recall drops below 0.85).
Regulatory Explainability (XAI)
“Black box” models are unacceptable in regulated finance. If you block a transaction, you must explain why to the customer and the auditor.
BigQuery ML integrates Shapley Additive exPlanations (SHAP). By adding enable_global_explain=TRUE to the model options, we can query feature importance directly.
SELECT
feature,
importance_weight,
importance_gain
FROM ML.GLOBAL_EXPLAIN(MODEL models.fraud_classifier_v1)
ORDER BY importance_gain DESC
LIMIT 5;
This output allows our clients to generate automated narratives: “Transaction blocked because the user is in a new location AND the transaction velocity is 400% higher than average.”
Real-World Impact
In a recent engagement with a Tier-2 regional bank, we migrated a legacy on-premise SAS solution to BigQuery ML. The results were transformative:
- Latency: Scoring time reduced from T+1 day to <200 milliseconds.
- Accuracy: A 40% reduction in false positives, significantly reducing the workload on the manual fraud review team.
- Cost: Infrastructure costs dropped by 60% by eliminating idle VM clusters and dedicated ML hardware.
The Future is Integrated
BigQuery ML is no longer just for experimentation; it is a robust production engine. By integrating it with Vertex AI for MLOps and Looker for visualization, we are building closed-loop systems that learn and adapt in real-time.
Are you ready to modernize your financial crime architecture?
Fraud detection is an arms race. Don’t fight it with yesterday’s weapons. Contact our Data Engineering Practice to discuss a Proof of Value (PoV) for your organization.