Payment systems fail in ways most backend systems don't. A retried request can charge a customer twice. A webhook can arrive after you've given up on a payment. A queue can hand the same message to two workers. A database commit can succeed while the message that announces it is lost. On any system with real traffic, all of this happens every week.
This handbook is about building payment infrastructure that stays financially correct anyway. It starts from what a payment is and works up to a complete, event-driven architecture, covering ledgers, idempotency, locking, queues, outboxes, retries, observability and reconciliation along the way. The chapters have diagrams you can watch draw themselves, and code in Go, Python and PostgreSQL.
Three principles
- Never move money twice. Any operation that moves or records money has to be safe to repeat, because in a distributed system everything gets repeated sooner or later.
- Never lose track of money. You should be able to explain every balance from an immutable record of what happened.
- Never blindly trust any component. That includes the provider, the network, the queue and your own database. Any of them can be late, wrong or duplicated, so we check what they tell us, put constraints on what they can write, and reconcile afterwards.
Who it is for
- Graduates and engineers new to fintech who want to understand what happens behind the “Pay” button.
- Backend engineers building wallets, payment gateway integrations, banking integrations or financial platforms.
- Senior engineers designing event-driven systems who want a reference for idempotency, outboxes, retries and recovery.
- Engineering leads weighing trade-offs, who want to know what can go wrong before it does.
The major chapters follow the same path. Each describes the problem in plain words and why it happens, shows what the obvious implementation gets wrong, and gives a better mental model, with a diagram, working code, the trade-offs and the common mistakes.
Contents
All eight parts are out. Part 1 has been proofread; the parts marked “In review” are still being checked, so if you spot a mistake, please tell me.
- Why processing payments is harder than it looks
- Designing a payment lifecycle
- Representing money and financial values
- Building an immutable financial ledger
- Transaction consistency and financial invariants
- Idempotency: the most important property of payment processing
- Database locks, compare-and-swap and concurrency control
- Why Redis is not always the answer for distributed locking
- Building an event-driven payment engine
- Understanding SQS visibility timeout and worker concurrency
- Designing reliable transactional outbox and inbox patterns
- Designing resumable payment workflows
- Database connections, connection pooling and short transactions
- Database query optimisation and scaling
- Retry strategies, exponential backoff and recovery storms
- Failure handling and compensation
- Structured logging and distributed tracing
- Metrics, monitoring and alerting
- Profiling and performance engineering
- Reconciliation: the final safety net
- End-to-end payment architecture
About the author
Frequently asked questions
What is payment system design?
It is the work of designing software that moves money and records it correctly: how payments are modelled, how they are stored, how the system talks to payment providers and banks, and how it behaves when any of those parts fail or answer twice. The goal is to never move money twice and never lose track of it, which is harder to meet than it sounds.
What is idempotency in payment processing?
An operation is idempotent if doing it twice has the same effect as doing it once. In payments it means a retried request, or a webhook delivered twice, cannot charge or credit anyone a second time. The usual way to build it is a unique key per operation plus a database constraint that rejects the duplicate.
Does a timeout mean a payment failed?
No. A timeout only means you stopped waiting. The provider may never have received the request, may have declined it, or may have charged the customer and lost the response. Treat it as an unknown state and find out the real result by checking the payment status, waiting for the webhook, or reconciling.
Why shouldn't money be stored as floating-point numbers?
Binary floating-point numbers cannot represent most decimal fractions exactly, so a sum like 0.1 + 0.2 comes out slightly wrong, and across many transactions the errors add up. Store money as integers in minor units (such as cents) or as fixed-precision decimals, and round only at defined points with a written rule.
Does an SQS visibility timeout guarantee a message is processed only once?
No. The visibility timeout hides a message from other consumers for a while after one consumer receives it, but if processing takes longer than the timeout, or the message is not deleted, it can be received again. Standard SQS queues deliver at least once, so correctness has to come from idempotent processing and database constraints.
Is a Redis lock enough to stop double payments?
Not on its own. A Redis lock is a lease that can expire while the holder is still working, for example during a long pause or a network problem, and then a second worker can take it. Locks are fine for coordination, but the database should enforce the rule that money moves only once, with unique constraints and conditional updates.
Do I still need this if I use Stripe?
The provider moves the money and keeps card data away from you, and you should let it. But your own records are still yours to keep right: the order or wallet that gets updated when a webhook arrives, what happens when that webhook arrives twice or after a timeout, and whether your numbers match the provider's at the end of the day. This handbook covers that side.
Who is this handbook for?
Backend engineers new to fintech, graduates who want to understand how real payment systems work, engineers building wallets, payment gateways or banking integrations, and senior engineers and leads who want a reference for the trade-offs. It begins with first principles and finishes with a complete architecture.
If this was useful
If you know someone building or fixing a payment system, send it their way, or share it with your network. The handbook is free, and people passing it on is how it reaches the engineers it was written for.
