Back to projects

Software architecture / Udacity project

Following a transaction
through a fraud system.

My project for the Udacity Software Architect Nanodegree. A bank checks for fraud in overnight batches and wants to do it in real time. I had to design the system that replaces it.

Fraud detection system architecture: API, transactions, Kafka, fraud analysis with Redis, alerts, reporting, PostgreSQL and S3

Click the diagram to inspect it at full size. Based on my submitted architecture document. Arrows describe the proposed event and request flow.

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.

  1. Accept. The API Gateway checks who is calling. The Transaction Service validates the transaction, saves it in PostgreSQL, publishes a TransactionReceived event to Kafka, and returns a transaction ID.
  2. Analyse. The Fraud Detection Service reads the event, looks up recent customer features in Redis, runs the model, and publishes FraudAnalysisCompleted with the risk score and the model version.
  3. 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:

Technology choices, why I picked each one, and what it costs
ChoiceWhy I picked itWhat it costs
KafkaHigh throughput, keeps events, lets services read them separatelyMore to run, and the same event can arrive twice
RedisFast lookup of recent customer featuresA feature can be missing or old
PostgreSQLA reliable record that analysts can queryIt is a second copy of the data next to the events
WebSocketsAlerts reach the analyst without refreshingAn 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 service targets from the design, not measured results

Proposed targetWhat it measures
1,000 transactions / secondPeak load without the Kafka backlog growing
99% within 200 msTime to acknowledge a valid transaction
99% within 2 secondsTime to a fraud result under normal load
99% within 1 secondTime for a high-risk result to reach a connected analyst
99.9% per monthAvailability 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