Stream-Based Fraud Detection System (Java, Kafka, Postgres)
Fraud Detect is an end-to-end, event-driven fraud detection system built using Java and Spring Boot.
The system demonstrates how financial transactions can be:
- accepted via REST APIs,
- published to Kafka,
- processed using a rule-based fraud engine,
- persisted in PostgreSQL,
- and visualized through a simple UI.
The project is implemented as a multi-module Maven project and supports both local development and fully containerized execution using Docker Compose.
- Java 17
- Spring Boot 3
- Apache Kafka
- PostgreSQL
- Spring Data JPA
- Thymeleaf
- Docker & Docker Compose
- Client sends a transaction request to the Gateway API
- Gateway publishes a
TransactionEventto Kafka topictx.incoming - Stream Processor consumes events from Kafka and:
- maps them to domain entities
- evaluates fraud rules
- computes risk score and decision
- persists the transaction
- creates an alert if the transaction is risky
- Transactions and alerts are available through a UI
fraud-detect
├── gateway-api
│ ├── REST API
│ └── Kafka Producer
├── stream-processor
│ ├── Kafka Consumer
│ ├── Rule Engine
│ ├── Persistence Layer
│ └── UI (Thymeleaf)
├── docker-compose.yml
└── pom.xml (parent)
Responsibilities
- Accept incoming transactions
- Validate requests
- Publish events to Kafka
Key Components
TransactionRequest– input DTO with validationTransactionEvent– Kafka event payloadTransactionProducer– Kafka producerTransactionController– REST endpoint
Endpoint
POST /api/v1/transactionsResponsibilities
- Consume transactions from Kafka
- Apply fraud detection rules
- Persist transactions and alerts
- Expose query APIs and UI pages
Key Components
-
Entities:
TransactionEntity,AlertEntity -
DTOs:
TransactionDto,AlertDto -
Repositories:
TransactionRepository,AlertRepository -
Rule Engine:
RiskRuleRuleResultDecisionHighAmountRuleCardNotPresentRuleNightHighAmountRuleRuleEngineVelocityRule
-
Consumer:
TransactionConsumer -
REST Controllers:
TransactionQueryControllerAlertController
-
UI Controllers:
TransactionUiControllerAlertUiController
-
Templates:
dashboard.htmltransactions.htmlalerts.html
Each transaction is evaluated using multiple rules. Each rule contributes to a cumulative risk score.
| Score Range | Decision |
|---|---|
< 30 |
APPROVE |
30 – 59 |
REVIEW |
≥ 60 |
DECLINE |
Alerts are created for REVIEW and DECLINE decisions.
Available from the Stream Processor service:
-
Alerts Dashboard http://localhost:8082/ui/alerts
-
Transactions Dashboard http://localhost:8082/ui/transactions
There are two supported run modes. Choose one depending on your use case.
Use this mode when you want:
- One-command startup
- No Java or Maven on the host
- A production-like environment
- Docker
- Docker Compose
Go to the root of the project (fraud-detect)
docker compose up --buildThis starts:
- Zookeeper
- Kafka
- PostgreSQL
- Adminer
- Gateway API
- Stream Processor
- Gateway API: http://localhost:8080
- Alerts UI: http://localhost:8082/ui/alerts
- Transactions UI: http://localhost:8082/ui/transactions
- Adminer (DB UI): http://localhost:8081
❗ Do NOT run mvn spring-boot:run in this mode.
Use this mode when:
- Actively developing Java code
- Using breakpoints, debugger, hot reload
- Wanting faster feedback loops
| Component | Runs Where |
|---|---|
| Kafka | Docker |
| Postgres | Docker |
| Gateway API | Local (Maven) |
| Stream Processor | Local (Maven) |
- Java 17
- Maven
- Docker & Docker Compose
Use a compose file that contains only infra services (Kafka, Postgres, Adminer).
docker compose -f docker-compose.infra.yml up -dmvn clean installcd gateway-api
mvn spring-boot:runcd stream-processor
mvn spring-boot:runIn this mode:
- Kafka runs at
localhost:9092 - Postgres runs at
localhost:5432
POST http://localhost:8080/api/v1/transactions{
"accountId": "ACC-123",
"merchantId": "M-1",
"amount": 1500,
"currency": "USD",
"channel": "CARD_NOT_PRESENT",
"ip": "1.2.3.4",
"deviceId": "dev-1"
}- Event-driven architecture using Kafka
- Rule-based fraud detection
- Stream processing with Spring Kafka
- Multi-module Spring Boot project design
- Dockerized microservices
- Hybrid dev vs production runtime strategies
- Metrics and observability with Micrometer
- Server-side rendered UI with Thymeleaf
- The project uses Spring Boot executable (fat) jars
- Docker images are built using multi-stage builds
- Java version is fixed at Java 17