What it is
A payment system takes money from a customer and credits it to a merchant. It talks to one or more payment service providers (PSPs: card processors, acquirers, wallets, bank transfer rails) that actually move money, and keeps its own ledger that records every movement. Around those sit checkout, refunds, payouts, notifications and reconciliation.
Why it exists
Money is unforgiving: a double charge or a lost payment is a support case, a refund, sometimes a regulatory problem. The PSP is a remote system you cannot roll back, reached over a network that times out, and it reports results asynchronously (webhooks, daily files). A payment system exists to turn this unreliable path into effects that happen exactly once and records that can be proven correct.
How it works
Requirements. Functional: pay, refund, query status, notify downstream services. Non-functional: no double charges, no lost payments, exact amounts (integers in minor units with an explicit currency), a full audit trail, and availability during peaks. Throughput is rarely the hard part.
Estimates (illustrative). 2 million payments a day is about 23 per second on average and a few hundred per second during a big sale. Each payment writes a handful of ledger rows. A relational database handles this comfortably; the design effort goes into correctness.
Idempotency keys. The client generates a key per payment attempt and sends it with every retry. The payment service inserts the key (unique constraint) with status pending before calling the PSP, so a concurrent or later retry finds it and either waits for or returns the stored result. The same key goes to the PSP: major PSP APIs accept idempotency keys and return the original result for a repeated key.
Timeouts are unknowns. If the PSP call times out, the charge may or may not exist. Retry with the same key, query the PSP for the payment status, or wait for the webhook. Never retry with a new key and never switch to another provider for an unknown outcome.
Double-entry ledger. Each business event is a transaction of entries across accounts whose amounts sum to zero (debit customer funds, credit merchant payable). Entries are immutable; refunds, chargebacks and fixes are new transactions. Balances are derived from entries (often cached in balance rows updated in the same transaction).
Outbox. The ledger transaction also inserts a PaymentSucceeded row into an outbox table. A relay publishes outbox rows to a broker and marks them sent; consumers deduplicate by payment id because delivery is at least once.
PSP routing and failover. A router can spread traffic over several PSPs (for cost, approval rates and redundancy) and health-check them. A request that failed definitely (connection refused, or an error the PSP documents as "not processed") may be retried on another PSP; a request with an unknown outcome may not.
Reconciliation. PSPs publish settlement reports listing what they captured and paid out. A daily job matches them against the ledger, payment by payment, and resolves differences: missing captures, amount mismatches, fees, chargebacks.
When to use it
Whenever your product moves money on behalf of customers or merchants: e-commerce checkouts, marketplaces, subscriptions, wallets, payouts. Even if a PSP's hosted checkout does most of the work, you still need idempotent calls, a ledger of what you believe happened and reconciliation.
When not to
- A single merchant with a hosted checkout page and no internal balances: the PSP's dashboard and webhooks may be enough; a full ledger is overhead.
- Do not build card processing itself unless you are a PSP: holding raw card data brings full PCI DSS scope. Tokenise through the PSP instead.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Authorise now, capture later | Can cancel without a refund; capture on shipment | Authorisations expire; two steps to track |
| Several PSPs | No single point of failure, cost and approval-rate routing | Integration and reconciliation per PSP, harder failover rules |
| Strongly consistent relational ledger | Simple invariants, transactions | Write throughput limits; partition by account at very large scale |
| Synchronous charge in checkout | Immediate answer to the shopper | Checkout latency and availability tied to the PSP |
| Aggressive client retries | Fewer visible failures | Load amplification during outages; must be idempotent |
Alternatives
- Asynchronous payments: accept the order, charge in the background and notify the customer; better availability, worse immediacy.
- Saga across order, payment and inventory with compensations (refund, release stock) instead of one synchronous flow.
- Payment orchestration platforms that provide routing, retries and reconciliation across PSPs as a service.
Failure modes
- Double charge from retries without an idempotency key, or from failing over to another PSP after a timeout.
- Lost payment: charged at the PSP, never recorded (crash after the call). The pending idempotency record plus status queries and reconciliation recover it.
- Lost event: ledger committed, event never published. The outbox prevents it.
- PSP outage or slowness: health checks catch dead providers, not slow ones; watch latency and error rate per PSP and drain slow ones.
- Retry storms during an outage: cap attempts, back off with jitter, open a circuit breaker.
- Floating-point money: rounding errors. Use integer minor units or decimal types.
In production
Monitor authorisation rate, error rate and latency per PSP and per payment method, the age of pending payments, outbox lag, and reconciliation mismatches. Keep webhooks idempotent too (PSPs retry them). Expire idempotency keys after a documented window (often 24 hours or more). Separate the ledger's accounts by currency. Keep card data out of your systems with PSP tokenisation to minimise PCI DSS scope. Run reconciliation daily and alert on unresolved differences.
Common misconceptions
- "Retries make payments reliable." Only idempotent retries do; otherwise they make duplicates.
- "A timeout means the payment failed." It means unknown.
- "Exactly-once delivery solves it." No network delivery is exactly once; exactly-once effects come from idempotency on the receiving side.
- "Failing over to another PSP is always safe." Only for definite failures.
- "The PSP's dashboard is our ledger." You need your own record to reconcile against theirs.
Interview angle
Start with requirements that emphasise correctness, then draw checkout → payment service → ledger and PSP. Interviewers probe: what happens if the PSP times out (unknown, idempotency key, status query), how you avoid double charges on client retries, how the ledger is structured (double-entry, append-only, minor units), how other services learn about payments (outbox), what happens when a PSP is down or slow, and how you know the books are right (reconciliation). Saying "a timeout is an unknown, not a failure" early sets the tone.
Model assumptions
The simulated steps are a simplified model, not a benchmark. Checkout starts 20 payments per second (Poisson) with a 1.5 s timeout and one retry after a random delay of up to 200 ms; every attempt that fails is retried, whether the failure was definite or not. The payment service writes to the ledger (about 8 ms) and then calls the PSP, an external server with about 300 ms per call (lognormal, cv 0.3); the slow PSP runs six times slower. A PSP that answers successfully is counted as one charge, so a payment whose original and retried attempts both reach the PSP is counted as charged twice. The PSP router health-checks every 500 ms and ejects a PSP after two failed checks; health checks do not detect slowness. Idempotency keys, ledger transactions, the outbox and reconciliation are scripted illustrations; the engine does not model idempotency, transactions or settlement files. Numbers show behaviour and orders of magnitude only.