A real-time fraud detection microservice suite built with Python, featuring ML-ready rule engine, streaming architecture, and production-grade security.
FraudShield is a high-performance, event-driven fraud detection platform that processes payment transactions in real-time. Built with modern Python async patterns, it demonstrates production-ready microservice architecture with:
- FastAPI for high-performance HTTP API ingress
- Apache Kafka for scalable event streaming
- PostgreSQL for durable transactional storage
- Redis for real-time fraud rules (velocity checks, blacklists)
- Rule Engine for extensible fraud detection logic
✅ API Key Authentication - Secure payment endpoints
✅ Real-time Fraud Detection - IP blacklist & velocity rules
✅ Async Processing - Sub-10ms API response times
✅ Horizontal Scalability - Kafka-based partitioning by country
✅ Automatic Alerting - High-risk transaction alerts
✅ Production Ready - Docker Compose deployment included
- Architecture
- Quick Start
- Features
- API Reference
- Configuration
- Testing
- Production Deployment
- Troubleshooting
Client → FastAPI Detector → Kafka → Consumer (Fraud Engine) → PostgreSQL
↓
Redis (Rules Cache)
-
Detector Service (FastAPI)
- Validates incoming payments
- Creates pending payment records in PostgreSQL
- Publishes events to Kafka
raw_paymentstopic - Returns HTTP 202 (Accepted) immediately
-
Consumer Service (Async Worker)
- Subscribes to Kafka
raw_paymentstopic - Runs fraud detection via RuleEngine
- Updates payment status with fraud scores
- Creates alerts for high-risk transactions
- Publishes results to downstream topics
- Subscribes to Kafka
-
Rule Engine
- IP Blacklist Rule: Checks IP against Redis blacklist
- Velocity Rule: Tracks transaction count per user/hour
- Extensible architecture for custom rules
-
Data Stores
- PostgreSQL: Authoritative payment & alert storage
- Redis: Real-time rule caching and counters
- Kafka: Event streaming and replay capability
- Docker & Docker Compose
- Python 3.11+ (for local development)
git clone https://github.com/your-username/fraudshield.git
cd fraudshield
cp .env.example .env # Edit with your configurationdocker-compose up -dThis starts:
- PostgreSQL (port 5432)
- Zookeeper & Kafka (port 9092)
- Redis (port 6379)
- Detector API (port 8000)
- Fraud Consumer
docker exec fraudshield_detector alembic upgrade head# Create user
curl -X POST http://localhost:8000/api/v1/users/ \
-H "Content-Type: application/json" \
-d '{"email": "test@example.com", "user_name": "testuser"}'
# Create merchant
curl -X POST http://localhost:8000/api/v1/merchants/ \
-H "Content-Type: application/json" \
-d '{"merchant_name": "Test Merchant", "industry": "ecommerce"}'curl -X POST http://localhost:8000/api/v1/payments/ \
-H "X-API-Key: dev-secret-key" \
-H "Content-Type: application/json" \
-d '{
"user_id": "YOUR_USER_ID",
"merchant_id": "YOUR_MERCHANT_ID",
"amount": 100.00,
"currency": "USD",
"payment_method": "credit_card",
"transaction_type": "sale"
}'# View consumer processing
docker logs -f fraudshield_consumer
# View detector API
docker logs -f fraudshield_detector- API Key Authentication: Required
X-API-Keyheader for payment submissions - Input Validation: Pydantic schemas with strict type checking
- Environment Secrets: All credentials in
.envfile
- IP Blacklist Rule: Instant decline for blacklisted IPs (stored in Redis)
- Velocity Rule: Tracks transactions per user per time window
- Risk Scoring: Aggregated fraud score (0.0 - 10.0)
- Automatic Decisions: Approve (<0.5), Review (0.5-1.0), Decline (>1.0)
- Async/Await: Non-blocking I/O throughout
- Connection Pooling: Reusable DB and Kafka connections
- Kafka Partitioning: By country for ordered processing
- Redis Caching: Sub-millisecond rule lookups
- Structured Logging: JSON logs with correlation IDs
- Database Audit Trail: All payments and alerts persisted
- Kafka Replay: Reprocess events for debugging
All payment endpoints require API key authentication:
-H "X-API-Key: your-secret-key"POST /api/v1/payments/
Content-Type: application/json
X-API-Key: dev-secret-key
{
"user_id": "uuid",
"merchant_id": "uuid",
"amount": 100.00,
"currency": "USD",
"payment_method": "credit_card",
"transaction_type": "sale",
"ip_address": "192.168.1.1", // optional
"country": "US" // optional
}
Response: 202 Accepted
{
"message": "Payment received and sent to Kafka for processing",
"status": "accepted",
"payment_id": "uuid"
}GET /api/v1/payments/{payment_id}
Response: 200 OK
{
"payment_id": "uuid",
"status": "Approved",
"fraud_score": 0.15,
"fraud_flag": false,
"risk_level": "Low",
...
}GET /api/v1/alerts/payment/{payment_id}
Response: 200 OK
[
{
"alert_id": "uuid",
"alert_type": "Potential Fraud",
"description": "Flagged: decline. Score: 5.0. Rules: velocity_rule_1h",
"status": "New"
}
]Create a .env file in the project root:
# Database
DATABASE_URL=postgresql+asyncpg://fraudshield_user:your_password@postgres:5432/fraud_detection
# Kafka
KAFKA_BOOTSTRAP_SERVERS=kafka:9092
KAFKA_RAW_PAYMENTS_TOPIC=raw_payments
KAFKA_PROCESSED_PAYMENTS_TOPIC=processed_payments
KAFKA_FRAUD_ALERTS_TOPIC=fraud_alerts
KAFKA_CONSUMER_GROUP_ID=fraud_detector_group
KAFKA_TOPIC_PARTITIONS=3
# Redis
REDIS_URL=redis://redis:6379
# API Security
API_KEY=your-strong-secret-key # CHANGE THIS!
# FastAPI
API_HOST=0.0.0.0
API_PORT=8000Add IPs to Redis blacklist:
docker exec fraudshield-redis-1 redis-cli SADD global:ip_blacklist "203.0.113.45"Run the included load test script:
docker exec fraudshield_detector python src/services/producer_cli/load_test.py \
--total 100 \
--concurrency 10This generates:
- Normal transactions
- High-value payments
- Velocity bursts
- Multiple scenarios to test rules
Test different fraud scenarios:
# High-risk amount
curl -X POST http://localhost:8000/api/v1/payments/ \
-H "X-API-Key: dev-secret-key" \
-H "Content-Type: application/json" \
-d '{
"user_id": "...",
"merchant_id": "...",
"amount": 15000.00, # Very high amount
"currency": "USD",
"payment_method": "credit_card",
"transaction_type": "sale"
}'
# Check consumer logs for fraud score
docker logs fraudshield_consumer --tail 20Before deploying to production:
- Change
API_KEYto a strong random secret (32+ characters) - Update PostgreSQL password
- Enable TLS/SSL for all services
- Set up firewall rules (limit port access)
- Use secrets management (AWS Secrets Manager, Vault)
- Enable authentication for Kafka and Redis
Horizontal Scaling:
# Scale consumers for higher throughput
docker-compose up -d --scale consumer=3Kafka Partitions:
# Increase partitions for parallel processing
docker exec fraudshield-kafka-1 kafka-topics \
--bootstrap-server localhost:9092 \
--alter --topic raw_payments --partitions 10Recommended stack:
- Prometheus: Metrics collection
- Grafana: Dashboards and visualization
- ELK Stack: Log aggregation and analysis
- Deploy PostgreSQL with replication
- Use Kafka cluster (3+ brokers)
- Redis Cluster or Redis Sentinel
- Load balancer for detector API
Check Kafka connectivity:
docker exec fraudshield_consumer nc -zv kafka 9092View consumer group status:
docker exec fraudshield-kafka-1 kafka-consumer-groups \
--bootstrap-server localhost:9092 \
--describe --group fraud_detector_groupVerify Redis is running:
docker exec fraudshield-redis-1 redis-cli ping
# Should return: PONGCheck connection URL:
# In Docker, use service name not localhost
REDIS_URL=redis://redis:6379 # ✅ Correct
REDIS_URL=redis://localhost:6379 # ❌ WrongCheck API key:
# Ensure X-API-Key header matches API_KEY in .env
curl -v -H "X-API-Key: dev-secret-key" http://localhost:8000/api/v1/payments/fraudshield/
├── src/
│ ├── common/ # Shared utilities
│ │ ├── config.py # Configuration
│ │ ├── db.py # Database connection
│ │ ├── kafka_utils.py
│ │ └── redis_utils.py
│ ├── models/ # SQLAlchemy ORM models
│ ├── services/
│ │ ├── detector/ # FastAPI service
│ │ └── fraud_consumer/ # Kafka consumer
│ │ ├── rule_engine.py
│ │ └── rules/ # Fraud detection rules
├── alembic/ # Database migrations
├── docker-compose.yml
└── requirements.txt
- Create a new rule in
src/services/fraud_consumer/rules/:
from .base_rule import BaseRule
class CustomRule(BaseRule):
async def evaluate(self, event, redis_service):
# Your logic here
is_triggered = False # Your condition
details = "Explanation"
return is_triggered, details- Register in
rule_engine.py:
from .rules.custom_rule import CustomRule
engine = RuleEngine([
IPBlacklistRule(threshold=10.0, priority=1),
VelocityRule(threshold=5.0, priority=5),
CustomRule(threshold=3.0, priority=10), # Add here
])- Machine learning fraud models (Random Forest, XGBoost)
- GraphQL API support
- OpenTelemetry distributed tracing
- Kubernetes deployment manifests
- Real-time fraud dashboard (Streamlit/React)
- Device fingerprinting integration
- Multi-currency support with exchange rates
- Webhook notifications for alerts
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
For questions or support, please open an issue on GitHub.
Built with ❤️ using Python, FastAPI, Kafka, and PostgreSQL
