The brief
The bank in the project runs its fraud checks as a batch job, so a bad transaction is only noticed hours later. The new system has to analyse up to 1,000 transactions per second, show the high-risk ones to fraud analysts right away, and keep every transaction so it can be replayed for compliance.
I did not build this system. The project was to design it and defend the design in a document, which is linked at the bottom.
This is a design, not a running system. The numbers on this page are requirements from the brief and targets I proposed. I have not measured any of them.
One transaction, three stages
The easiest way to explain the design is to follow one transaction through it.
The API Gateway checks the request. The Transaction Service saves the transaction in PostgreSQL, publishes TransactionReceived to Kafka, and returns a transaction ID. This reply only means the transaction was accepted. The fraud result comes later.
The API Gateway checks the request. The Transaction Service saves the transaction in PostgreSQL, publishes TransactionReceived to Kafka, and returns a transaction ID. This reply only means the transaction was accepted. The fraud result comes later.The Fraud Detection Service, written in Python, reads the event, gets customer features from Redis, runs the model, and publishes FraudAnalysisCompleted. Kafka keeps the events so they can be replayed.Alerting and reporting each read the result on their own. High-risk alerts go to the analyst over a WebSocket, reporting stores results for searching, and an archive consumer copies events to S3.- Accept. The API Gateway checks who is calling. The Transaction Service validates the transaction, saves it in PostgreSQL, publishes a
TransactionReceivedevent to Kafka, and returns a transaction ID. - Analyse. The Fraud Detection Service reads the event, looks up recent customer features in Redis, runs the model, and publishes
FraudAnalysisCompletedwith the risk score and the model version. - Deliver. Three services read that result on their own. The Alert Service pushes high-risk results to analysts over a WebSocket. The Reporting Service stores results so analysts can search them. An archive consumer copies every event to S3.
Why I split it this way
My first idea was simpler: the API calls the model and waits for the answer. The problem is that the API is then only as fast as the model. If the model slows down, the bank stops accepting transactions.
So I put Kafka in the middle. The API only has to save the transaction and publish an event, and it can answer straight away. The model does its work a moment later. The price is that for a short time a transaction is accepted but has no fraud result yet. I decided that was fine here, because the brief asks for fast alerts to analysts, not for blocking the payment.
Kafka also solved the replay requirement almost for free. It keeps the events, so a service that was down can continue from where it stopped, and the reporting database can be rebuilt by replaying them.
Every choice had a cost that I had to write down:
| Choice | Why I picked it | What it costs |
|---|---|---|
| Kafka | High throughput, keeps events, lets services read them separately | More to run, and the same event can arrive twice |
| Redis | Fast lookup of recent customer features | A feature can be missing or old |
| PostgreSQL | A reliable record that analysts can query | It is a second copy of the data next to the events |
| WebSockets | Alerts reach the analyst without refreshing | An analyst who disconnects misses alerts |
I left out a service mesh. The API Gateway, Kubernetes, and OpenTelemetry already cover routing, scaling, and monitoring for a system of this size.
What happens when something fails?
This was the part I understood least when I started, and the part I learned the most from.
A consumer tells Kafka that an event is done only after it has finished processing it. If the service crashes just before that, another instance reads the same event again. So the same transaction can be processed twice. I first thought Kafka’s exactly-once feature would fix this, but that only covers Kafka-to-Kafka steps. The database sits outside Kafka. The fix is to use the transaction ID or event ID as a unique key, so writing the same result a second time changes nothing.
The other protections in the design:
- An event that keeps failing is retried, then moved to a dead-letter topic so it does not block the rest.
- Kafka topics are replicated across brokers, and PostgreSQL has backups and point-in-time recovery.
- Redis is treated as a cache. If a feature is missing, the service uses a fallback value and records that it did.
- Alerts are stored, so an analyst whose WebSocket dropped can load the ones they missed over REST.
Every request and event carries a transaction ID and a trace ID. With OpenTelemetry that makes it possible to follow one transaction through the API, Kafka, Redis, the model, and the alert.
Targets I proposed
The brief only gives the 1,000 transactions per second. The rest are starting targets I suggested in the document.
| Proposed target | What it measures |
|---|---|
| 1,000 transactions / second | Peak load without the Kafka backlog growing |
| 99% within 200 ms | Time to acknowledge a valid transaction |
| 99% within 2 seconds | Time to a fraud result under normal load |
| 99% within 1 second | Time for a high-risk result to reach a connected analyst |
| 99.9% per month | Availability of the transaction API |
If I built this, the first thing I would do is load test it and watch consumer lag and model time, not only the API response time. A fast API tells you nothing if the fraud results are falling behind.
Read my architecture document