Hybrid real-time fraud detection microservice Redis Velocity Check + LightGBM + A/B Testing Router โ production-ready architecture
| Endpoint | Link |
|---|---|
| ๐ API Root | fraud-detection-api-1-hf78.onrender.com |
| ๐ Swagger UI | fraud-detection-api-1-hf78.onrender.com/docs |
| โค๏ธ Health Check | fraud-detection-api-1-hf78.onrender.com/health |
| ๐ณ Test Cards | fraud-detection-api-1-hf78.onrender.com/cards |
Incoming Transaction (card_id + amount only)
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Layer 1: Redis Velocity Check โ
โ Checks transaction frequency โ
โ Protection against brute force โ
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Redis Feature Store โ
โ Client sends only card_id + amount โ
โ API fetches full historical profile โ
โ (V1-V28 features) from Redis โ
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ A/B Testing Router โ
โ 80% โ Model A (Champion) โ
โ 20% โ Model B (Challenger) โ
โ Safe testing of new hypotheses โ
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ LightGBM Inference โ
โ Class Imbalance handling โ
โ Fraud probability โ APPROVE / BLOCK โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| Component | Technology |
|---|---|
| Machine Learning | LightGBM (Class Imbalance handling) |
| Feature Store | Redis (Upstash โ in-memory profiles) |
| Backend | FastAPI, Pydantic |
| A/B Testing | Custom 80/20 Champion/Challenger Router |
| Containerization | Docker, Docker Compose |
| Deployment | Render |
| Dataset | MLG-ULB Credit Card Fraud (Kaggle) |
Client sends only card_id and amount โ the API fetches the full historical profile (V1-V28 features) from Redis automatically. This mirrors production anti-fraud systems at Visa and Mastercard:
# Client sends minimal data
{"card_id": "card_0001", "amount": 500.00}
# API enriches with Redis profile internally
features = cache.get(f"profile:{card_id}") # V1-V28 from Redis
features["Amount"] = amount # Add current transactionBuilt-in traffic splitter tests new model versions safely in production:
if random.random() < 0.20:
active_model = ml_models["model_b"] # Challenger โ 20%
else:
active_model = ml_models["model_a"] # Champion โ 80%No downtime, no risk โ new models proven before full rollout.
Credit card fraud datasets are heavily imbalanced (0.17% fraud). LightGBM trained with scale_pos_weight to properly detect rare fraud events.
{
"service": "Anti-Fraud Detection API",
"architecture": "Hybrid: Redis Feature Store + LightGBM",
"features": {
"ab_testing": "80/20 Champion/Challenger split",
"feature_store": "Redis in-memory profiles (Upstash)",
"model": "LightGBM with Class Imbalance handling"
}
}{
"status": "healthy",
"redis": "connected",
"models_loaded": ["model_a", "model_b"],
"ab_split": "80% Model A / 20% Model B"
}{
"available_cards": ["card_0001", "card_0002", "..."],
"total": 101,
"test_cards": {
"normal": "card_0001",
"fraud": "fraud_card_001"
}
}Normal transaction:
// Request
{
"card_id": "card_0001",
"amount": 500.00
}
// Response
{
"card_id": "card_0001",
"amount": 500.0,
"decision": "APPROVE",
"risk_level": "LOW",
"risk_probability": 0.0821,
"model_used": "Model A (Champion)",
"feature_store": "Redis (Upstash)"
}Fraud transaction:
// Request
{
"card_id": "fraud_card_001",
"amount": 150000.00
}
// Response
{
"card_id": "fraud_card_001",
"amount": 150000.0,
"decision": "BLOCK",
"risk_level": "HIGH",
"risk_probability": 0.8934,
"model_used": "Model A (Champion)",
"feature_store": "Redis (Upstash)"
}git clone https://github.com/RaNurbekov/fraud-detection-api.git
cd fraud-detection-apidocker-compose up --buildThis starts both Redis and FastAPI containers automatically.
# Normal transaction
curl -X POST "http://localhost:8000/scan" \
-H "Content-Type: application/json" \
-d '{"card_id": "card_0001", "amount": 500}'
# Fraud transaction
curl -X POST "http://localhost:8000/scan" \
-H "Content-Type: application/json" \
-d '{"card_id": "fraud_card_001", "amount": 150000}'fraud-detection-api/
โโโ api.py # FastAPI: /scan, /health, /cards
โโโ src/
โ โโโ database.py # SQLite audit logging
โโโ models/
โ โโโ lgbm_fraud.pkl # Champion model (Model A)
โ โโโ lgbm_fraud_b.pkl # Challenger model (Model B)
โโโ notebooks/ # Training & EDA
โโโ docker-compose.yml # Redis + FastAPI orchestration
โโโ Dockerfile
โโโ requirements.txt
โโโ README.md
| Feature | Description |
|---|---|
| Streamlit Dashboard | Visual A/B testing stats + fraud monitoring |
| MLflow Integration | Model versioning and experiment tracking |
| Evidently AI | Data drift monitoring in production |
| PostgreSQL | Replace SQLite with production-grade DB |
| Kafka Integration | Connect with kafka-fraud-streaming pipeline |
Part of a Fintech ML ecosystem:
- fraud-gnn โ Graph Neural Networks for collaborative fraud detection
- credit-risk-api โ Credit scoring with MLflow + SHAP + Evidently AI
- kafka-fraud-streaming โ Real-time Kafka streaming pipeline
๐ก Production vision: Kafka streams transactions โ this API scores them in real-time โ GNN catches collaborative fraud rings โ credit-risk-api assesses customer risk. That's the complete fintech ML stack.
Rashid Nurbekov โ ML Engineer | Fintech & Generative AI | Almaty, Kazakhstan ๐ฐ๐ฟ