The design brief#
“Design the core of a bank’s instant payments service. Customers send money from mobile and web; payments must settle in under 10 seconds, end to end, 24×7. Each payment must be fraud-checked, posted to the ledger exactly once, notified to both parties and reconciled with the scheme daily.”
1. Requirements#
| Type | Requirement |
|---|---|
| Functional | Initiate, validate, fraud-check, reserve funds, submit to the scheme (for example SEPA Instant, UPI or FedNow), confirm, post to the ledger, notify, reconcile |
| Throughput | 3,000 payments/s average, 15,000/s peak (salary days, festivals) |
| Latency | Under 10 s end to end (scheme rule); the internal path, excluding the scheme, at p99 under 1.5 s |
| Correctness | No double debit, no lost payment; every state change auditable |
| Availability | 99.99%; survive the loss of an AZ with no data loss, and a region with RPO ≈ 0 for the ledger |
| Compliance | PSD2/PSD3 SCA, PCI DSS (for card-funded flows), DORA, RBI/NPCI rules (for UPI), GDPR/DPDP |
2. The key decision: what is synchronous?#
The customer is waiting, so the critical path is synchronous and as short as possible. Everything else is asynchronous through events.
flowchart LR
subgraph SYNC["Synchronous: customer waits"]
A["API gateway<br/>SCA, idempotency key"] --> P["Payment service<br/>validate, state machine"]
P --> F["Fraud score lookup<br/>under 50 ms"]
P --> L["Ledger: reserve funds"]
P --> S["Scheme gateway<br/>submit + confirm"]
end
subgraph ASYNC["Asynchronous: events on Kafka"]
E["payment events"] --> N["Notifications"]
E --> R["Reconciliation"]
E --> AN["Analytics / lakehouse"]
E --> FS["Fraud features update"]
end
P -->|"outbox"| E
3. Topics and keys#
| Topic | Key | Retention | Notes |
|---|---|---|---|
payments.payment.lifecycle.v1 |
paymentId |
30 days + tiered | All state changes for one payment, in order |
payments.account.activity.v1 |
accountId |
30 days | For per-account ordering (balance views, limits) |
fraud.features.v1 |
cardOrAccountToken |
compacted | Latest features per entity |
notifications.requests.v1 |
customerId |
7 days | Consumed by a share group (queue semantics) |
*.dlt |
same | 30 days | Dead letters, alerted |
- RF=3,
min.insync.replicas=2, rack-aware across 3 AZs. Size partitions for peak: 15,000/s at roughly 1–2 MB/s each needs well over 48 partitions for consumer parallelism, so plan 96–192 with growth. - Use Avro with
FULL_TRANSITIVEcompatibility, and no PII beyond tokens in events.
4. Correctness: never double-debit#
- Idempotency key from the client (
Idempotency-Keyheader). The payment service stores the key with the request hash and the final response. A retry returns the same result. - Payment state machine persisted in the payment DB:
RECEIVED → VALIDATED → FUNDS_RESERVED → SUBMITTED → SETTLED | REJECTED | TIMED_OUT. Each transition is a local ACID transaction. - Transactional outbox: every state change writes an outbox row in the same transaction. Debezium publishes it to Kafka. See outbox and CDC.
- Ledger is double-entry and append-only. Postings are idempotent on
(paymentId, leg), so a replay cannot post twice. - Scheme timeouts: when the scheme does not answer in time, the payment moves to
TIMED_OUT. An investigation and status query flow resolves it. Never blindly retry a submission without the scheme’s own idempotency reference.
5. Fraud in the stream#
- A Flink job maintains per-card and per-account features (velocity, amount z-score, new payee, device change, geo) in event-time windows. It writes them to a low-latency store.
- The payment service calls the fraud decision service synchronously. That service reads the precomputed features and scores them with a model under a 50 ms budget, returning approve, step-up (extra SCA) or block.
- The decision and its explanation are published as events for case management and model feedback.
6. Notifications, reconciliation, analytics#
- Notifications consume lifecycle events through a share group, which scales beyond the partition count. They are idempotent on
(paymentId, status, channel). - Reconciliation compares scheme settlement files or messages with ledger postings in a daily batch, plus a streaming intra-day check. Breaks open cases.
- Analytics: a Kafka Connect Iceberg sink loads events into the lakehouse bronze layer. See data platforms.
7. Multi-region and DR#
| Component | Approach |
|---|---|
| Ledger DB | Synchronous replication within the region (3 AZs). Cross-region uses a distributed SQL database (for example CockroachDB, YugabyteDB or Spanner) or async replication with an RPO-tested failover. |
| Kafka | One cluster per region. Cluster Linking or MirrorMaker 2 for DR; producers fail over with the region. |
| Payment service | Active-active per region, with home-region pinning per account, so one account’s payments are processed in one region at a time. |
| Failover | Tested quarterly (DORA requires testing). Runbooks automated. |
8. Security and compliance#
- mTLS between services; OAuth 2.0 / FAPI 2.0 profiles for external APIs; SCA at initiation.
- Tokenised account and card data. Field-level encryption for sensitive fields in events. Kafka ACLs per service principal.
- Immutable audit trail: the lifecycle topic plus ledger plus logs, sent to a SIEM. DORA major-incident classification and reporting runbooks. See FinTech regulations.
9. Trade-offs#
| Decision | Benefit | Cost |
|---|---|---|
| Synchronous critical path, asynchronous rest | Low latency and decoupled side effects | Two consistency models to explain |
| Outbox + CDC | Atomic state and event | Debezium to operate |
| Precomputed fraud features | Fast decisions | Feature freshness is eventual (seconds) |
| Home-region pinning | No cross-region conflicts | Failover must move account ownership |
Common mistakes#
Quick check#
Why is the critical path synchronous? The customer and the scheme need a definitive answer within seconds. Notifications, analytics and reconciliation can be eventual.
How do you prevent a double debit when the client retries? An idempotency key stored with the result, a persisted state machine, and idempotent ledger postings keyed by payment and leg.
Why use a share group for notifications? Queue semantics with per-record acknowledgement and parallelism beyond the partition count. Per-key ordering is not needed there.
What happens on a scheme timeout? The payment goes to a timed-out state, and a status-inquiry or investigation flow resolves it using the scheme’s reference. It is never blindly resubmitted.
Summary#
- Short synchronous core; everything else is events through the outbox.
- Correctness comes from idempotency keys, a persisted state machine and an idempotent double-entry ledger.
- Fraud features are computed in the stream and read synchronously within the latency budget.
- Multi-region with per-account home regions and tested failover.
References#
- European Payments Council, SEPA Instant Credit Transfer rulebook.
- NPCI, Unified Payments Interface.
- Stripe, Designing robust and predictable APIs with idempotency.