Volume 14 · Chapter 2 — Enterprise Software Architecture Case Study Encyclopedia
Payment Processing Systems
2.1 Introduction
A payment processing system is a focused, extremely demanding sub-domain of banking: it must handle money movement securely, reliably, and idempotently, at scale, with fraud prevention and complete auditability. Its defining challenge is correctness under failure - a payment must never be lost or duplicated even when networks drop, services crash, or requests are retried. This case study works through the lifecycle and the reliability patterns that guarantee correctness.
2.2 The Payment Lifecycle
A payment flows through distinct stages - initiation, validation, authorization, processing (capture), settlement, and notification - each of which can fail and must be recoverable. Because these steps span services and external networks, the flow is orchestrated with idempotency and compensation rather than a single transaction.
sequenceDiagram autonumber participant C as Client participant GW as Payment Gateway participant Val as Validation participant Auth as Authorization participant Ledger as Ledger participant Bus as Event Broker C->>GW: submit payment (idempotency key) GW->>Val: validate (amount, account, limits) Val-->>GW: ok GW->>Auth: authorize (funds / card network) Auth-->>GW: approved GW->>Ledger: record debit (idempotent, outbox) Ledger-->>GW: committed GW-)Bus: PaymentProcessed Bus-)C: notification (async)
2.3 Idempotency - the Non-Negotiable
The single most important payment pattern is idempotency: every payment carries a unique key, and the system records processed keys so a retry or duplicate submission (from a timeout, a double-click, or at-least-once message delivery) produces exactly one payment. Without this, retries - which are inevitable in distributed systems - cause double charges.
public async Task<PaymentResult> Process(PaymentRequest req, CancellationToken ct)
{
// Idempotency: a repeated key returns the original result, never a second charge.
var existing = await _payments.FindByKeyAsync(req.IdempotencyKey, ct);
if (existing is not null) return existing.Result;
var payment = Payment.Create(req); // validate + authorize inside
await _payments.SaveAsync(payment, ct); // state + outbox event, one transaction
return payment.Result;
}
2.4 Reliability, Fraud, and Settlement
Reliability comes from idempotency, retries with backoff, dead-letter queues, transaction tracking, and audit logging. Fraud prevention runs in real time on the transaction stream - rule engines plus ML risk scoring - without blocking the payment path. Settlement (the actual movement of funds between institutions) is often batched and reconciled, using sagas and compensation because it spans external systems that cannot share a transaction.
How do you design a payment system that never loses or duplicates a payment, even under failure?
Correctness under failure is the whole game, and the cornerstone is idempotency combined with reliable, atomic state-and-event recording. Every payment request carries a unique idempotency key (client- generated), and the system records processed keys, so if the same request arrives again - because of a client timeout and retry, a double-click, or at-least-once message redelivery, all of which are inevitable in distributed systems - it returns the original result rather than creating a second payment. This is what prevents duplicates. To prevent lost payments, I make the state change and its outgoing event atomic using the transactional outbox: the debit and the “payment processed” event commit in the same database transaction, so I can never end up with money moved but no event, or an event with no money moved. Downstream steps and services react to that event and are themselves idempotent, so redelivery is safe. The payment lifecycle - validation, authorization, capture, settlement - is orchestrated as a saga with compensating actions, because it spans services and external networks that cannot share an ACID transaction, so if a later step fails, earlier steps are semantically reversed (a refund/void), and the whole flow is tracked with a clear state machine and full audit logging so every payment’s status is known and recoverable. I add retries with backoff for transient failures and dead-letter queues for messages that can’t be processed, so nothing is silently dropped and poison messages are isolated for investigation. I also reconcile against systems of record (and external networks) to catch any discrepancy. The design principle is to assume failure and retries as normal and engineer so that no sequence of failures and retries can lose or duplicate money: idempotency for exactly-once effect, outbox for atomic state-plus-event, sagas with compensation for cross-service flows, and reconciliation and audit for provable correctness. That combination is what makes a payment system trustworthy.
How do you integrate real-time fraud detection without slowing down payments?
I decouple fraud analysis from the payment’s critical path wherever possible and keep only the minimal, fast checks inline, so fraud prevention protects the business without adding latency that hurts the customer experience or throughput. The design: as payments are processed, the transaction events are streamed (via the event broker / a streaming platform like Event Hubs) to a fraud service that scores them in near real time using a combination of a fast rule engine (known-bad patterns, velocity checks, limits) and ML risk models. For the vast majority of transactions this scoring happens asynchronously alongside the payment, flagging or holding suspicious transactions for review or triggering step-up actions, rather than blocking every payment on a slow model call. Where the business genuinely requires a synchronous decision before authorizing - high-risk transaction types - I keep that inline check tightly bounded with a strict timeout and a sensible default (fail-open or fail-closed per risk appetite) and a circuit breaker, so a slow or failing fraud service degrades gracefully rather than taking payments down. I precompute and cache the features and signals the models need (customer history, velocity aggregates) so scoring is fast, and I use the streaming platform’s ability to feed many consumers so fraud, ledger, and notifications all react independently to the same events. Suspicious transactions can be held, reversed via the payment saga’s compensation, or escalated to human review, and everything is logged for audit and model improvement. The architectural principle is that fraud detection is primarily an event-driven, asynchronous concern running on the transaction stream in parallel with the payment, keeping the payment path fast, with only carefully-bounded synchronous checks where the risk truly demands a pre-authorization decision - so you get real-time fraud protection and low-latency payments, rather than trading one for the other.
2.5 Case Study Outcome
The payment platform guarantees exactly-once money movement through idempotency keys and the transactional outbox, orchestrates the multi-step lifecycle as a saga with compensation, and tracks every payment through an auditable state machine. Fraud detection runs asynchronously on the transaction event stream with tightly-bounded synchronous checks only where risk demands, keeping the payment path fast. Retries, dead-letter queues, and reconciliation ensure nothing is lost, and settlement across external networks is handled with sagas - a system that is correct under failure by design.
Chapter Revision Notes
- Payment correctness under failure is the defining requirement.
- Idempotency keys guarantee exactly-once effect despite inevitable retries.
- The transactional outbox makes state change and event atomic (no lost payments).
- Orchestrate the lifecycle as a saga with compensation; track state and audit everything.
- Run fraud detection asynchronously on the event stream; bound any synchronous checks.