A production-grade, stream-processing fraud detection system β containerised end-to-end and observable in real time.
Built with Kafka (Redpanda), Redis, XGBoost + SHAP, FastAPI, Streamlit, Prometheus, and Grafana.
Transactions flowing at ~5.2 TPS with a live fraud detection rate tracked per second.
All 8 containers running in sync. The detector and ML service communicate on every transaction.
βββββββββββββββββββ Kafka Topic ββββββββββββββββββββββββ
β generator.py β ββ(raw-transactions)βββΆ β detector.py β
β β β β
β Simulates 50 β β β’ Velocity check β
β user profiles β β β’ Amount anomaly β
β w/ fraud β β β’ Location mismatch β
β injection β β β’ ML inference call β
βββββββββββββββββββ ββββββββββ¬ββββββββββββββ
β HTTP POST /predict
ββββββββββββΌβββββββββββ
ββββββββββββββββββββββββ β ml_service.py β
β Redis βββββββββββΆβ β
β User profiles & β β XGBoost classifier β
β TX velocity state β β + SHAP explainabilityβ
ββββββββββββββββββββββββ ββββββββ¬βββββββββ¬βββββββ
β β HTTP POST /batch-analyze
ββββββββββββββΌββ βββββΌβββββββββββββββ
β Prometheus β β app.py β
β + Grafana β β Streamlit UI β
β (:8001 src) β β FraudOps Portal β
ββββββββββββββββ ββββββββββββββββββββ
| Service | File / Image | Port | Role |
|---|---|---|---|
| Generator | generator.py |
β | Produces synthetic transactions to Kafka |
| Detector | detector.py |
8001 (metrics) |
Consumes, applies rules + ML, emits verdicts |
| ML Service | ml_service.py |
8000 |
XGBoost inference + SHAP explanations |
| Frontend | app.py |
8501 |
Streamlit FraudOps Portal (live view + batch) |
| Redpanda | redpandadata/redpanda |
19092 |
Kafka-compatible message broker |
| Redis | redis:alpine |
6379 |
User profiles + velocity state store |
| Prometheus | prom/prometheus |
9090 |
Metrics scraper |
| Grafana | grafana/grafana |
3000 |
Dashboards |
The detector applies a layered defence strategy per transaction:
-
Rule-Based Checks (fast, synchronous)
- π Amount Anomaly β flags if amount > 5Γ the user's historical average
- π Velocity Anomaly β flags 5+ transactions within 60 seconds
- π Location Anomaly β flags transactions outside the user's home region
-
ML Inference (XGBoost via HTTP, 500 ms timeout)
- Features:
amount,user_avg_amount,amount_ratio,location_mismatch,is_international - Threshold: fraud probability > 80%
- SHAP values generate human-readable explanations for every positive prediction
- Features:
-
Cold-Start Handling β new/unknown users fall back to safe defaults so the system never crashes on missing profiles.
- Docker & Docker Compose
- Python 3.9+ (only for local runs / model retraining)
git clone <your-repo-url>
cd realtime-fraud-engine
docker compose up -dThis starts Redpanda, Redis, Prometheus, Grafana, the ML service, detector, generator, and the Streamlit frontend automatically.
β οΈ Important: If you have a local Redis instance running on port 6379, thesetup_redis.pyscript will hit it instead of the Docker Redis container. Use the command below to seed directly into the correct container.
# Recommended β seeds directly into the Docker Redis container (works regardless of local Redis)
bash -c '
LOCATIONS=("NY" "CA" "TX" "FL" "IL")
{ echo "FLUSHALL"
for i in $(seq -w 0 49); do
LOC="${LOCATIONS[$((RANDOM % 5))]}"
AVG=$(awk "BEGIN{printf \"%.2f\", 10 + rand() * 140}")
STD=$(awk "BEGIN{printf \"%.2f\", 2 + rand() * 13}")
echo "SET user_profile:usr_${i} {\"user_id\":\"usr_${i}\",\"base_location\":\"${LOC}\",\"avg_transaction_amount\":${AVG},\"std_dev_amount\":${STD}}"
done
} | docker exec -i redis redis-cli --pipe
'Creates 50 synthetic user profiles, each with a home location, average spend, and spend volatility.
docker compose up --buildOr run each service individually for development:
# Terminal 1 β ML inference API
uvicorn ml_service:app --reload --port 8000
# Terminal 2 β Fraud detector
python detector.py
# Terminal 3 β Transaction generator
python generator.py
# Terminal 4 β Streamlit FraudOps Portal
streamlit run app.py --server.port 8501python train_model.pyGenerates ~10,000 labelled transactions, trains an XGBoost classifier, fits a SHAP TreeExplainer, and saves both as pickle artifacts (fraud_model.pkl, shap_explainer.pkl).
Pre-trained artifacts are already committed β skip this step if you just want to run the engine.
realtime-fraud-engine/
βββ generator.py # Synthetic transaction producer (Kafka)
βββ detector.py # Core stream processor & fraud logic
βββ ml_service.py # FastAPI ML inference endpoint (/predict & /batch-analyze)
βββ app.py # Streamlit FraudOps Portal (live monitoring + batch analysis)
βββ train_model.py # XGBoost + SHAP model training script
βββ setup_redis.py # Seeds user profiles into Redis
βββ consumer.py # Lightweight debug consumer (print-only)
βββ test_batch.csv # Sample CSV for batch analysis testing
βββ fraud_model.pkl # Pre-trained XGBoost model artifact
βββ shap_explainer.pkl # Pre-trained SHAP TreeExplainer artifact
βββ requirements.txt # Python dependencies
βββ Dockerfile # Single image for all Python services
βββ docker-compose.yml # Full stack orchestration (8 services)
βββ prometheus.yml # Prometheus scrape config
βββ screenshots/ # Live system snapshots
βββ grafana_dashboard.png
βββ docker_services.png
All services read configuration from environment variables with sensible defaults:
| Variable | Default | Description |
|---|---|---|
KAFKA_BROKER |
localhost:19092 |
Kafka / Redpanda bootstrap servers |
KAFKA_TOPIC |
raw-transactions |
Input topic name |
OUT_TOPIC |
processed-transactions |
Output topic for verdicts |
REDIS_HOST |
localhost |
Redis hostname |
REDIS_PORT |
6379 |
Redis port |
ML_SERVICE_URL |
http://localhost:8000/predict |
ML inference endpoint (streaming) |
ML_SERVICE_URL_BATCH |
http://localhost:8000/batch-analyze |
ML batch analysis endpoint |
PROMETHEUS_URL |
http://localhost:9090 |
Prometheus base URL for Streamlit UI |
FRAUD_PROBABILITY |
0.05 |
Fraction of injected fraud events |
TRANSACTIONS_PER_SECOND |
5.0 |
Generator throughput rate |
When running with Docker Compose these are automatically wired to the correct service hostnames.
The detector exposes a Prometheus metrics endpoint on port 8001:
| Metric | Type | Description |
|---|---|---|
transactions_processed_total |
Counter | Total transactions consumed |
fraud_caught_total |
Counter | Total transactions flagged as fraud |
transaction_processing_seconds |
Histogram | Per-transaction processing latency |
| Tool | URL |
|---|---|
| FraudOps Portal | http://localhost:8501 (Streamlit live + batch) |
| Prometheus | http://localhost:9090 |
| Grafana | http://localhost:3000 (default: admin/admin) |
| ML API Docs | http://localhost:8000/docs (Swagger UI) |
| Raw Metrics | http://localhost:8001/metrics |
To visualise fraud metrics in Grafana, add Prometheus as a data source (http://prometheus:9090) and create panels using the metrics above.
// Request
{
"amount": 850.00,
"user_avg_amount": 45.00,
"location_mismatch": 1,
"is_international": 0
}
// Response β Fraud detected
{
"fraud_probability": 0.9995,
"is_fraud": true,
"explanation": [
"Transaction location does not match user's typical region.",
"Transaction amount is unusually high compared to user history."
]
}
// Response β Normal transaction
{
"fraud_probability": 0.0315,
"is_fraud": false,
"explanation": []
}Accepts a multipart CSV upload with columns: transaction_id, user_id, amount, location. Looks up each user's profile from Redis, engineers features, runs XGBoost inference + SHAP, and returns annotated results.
// Response
{
"analyzed_transactions": [
{
"transaction_id": "txn_001",
"user_id": "usr_12",
"amount": 1200.0,
"location": "TX",
"is_fraud": true,
"fraud_probability": 0.9812,
"reasons": "Amount unusually high vs history., Location mismatch."
}
]
}Use the FraudOps Portal at
http://localhost:8501for an interactive drag-and-drop batch analysis UI with a downloadable annotated report.
Interactive API docs available at /docs (Swagger UI).
| Layer | Technology |
|---|---|
| Message Broker | Redpanda (Kafka-compatible) |
| Stream Processor | Python + confluent-kafka |
| State Store | Redis |
| ML Model | XGBoost 1.7.6 |
| Explainability | SHAP (TreeExplainer) |
| Inference API | FastAPI + Uvicorn |
| Frontend | Streamlit |
| Observability | Prometheus + Grafana |
| Containerisation | Docker / Docker Compose |
MIT β feel free to use, modify, and distribute.

