Sub topic 17
Scaling and availability
This chapter assumes you know HTTP APIs, relational transactions and unique constraints, and how timeouts and retries work. It builds on idempotency, the outbox pattern, and PSP routing from earlier chapters.
What scaling means for payments
A payment system rarely has huge throughput compared with a social feed. The harder requirement is correctness under load and during failures. A slow response is acceptable. A double charge or a lost payment is not.
Plan capacity for peaks such as a flash sale. Measure payments per second at the peak, not the daily average.
Scale the stateless layers first
API servers hold no payment state, so you can add instances behind a load balancer. Keep them stateless by putting all state in the database and the outbox. Any instance can then handle any retry, because the idempotency key is checked in the database.
The database is the hard part. These options work in order of increasing complexity:
- Add read replicas for reporting and status reads. Never use a replica for the idempotency check, because replication lag can hide a row that was just written.
- Partition (shard) by
merchant_id. The unique constraint on(merchant_id, idempotency_key)then stays inside one shard, so uniqueness is still enforced atomically. - Move slow work, such as webhook delivery and reconciliation, off the request path through a queue.
Watch for hot merchants. A very large merchant can overload one shard. Mitigate this by giving large merchants dedicated shards.
Availability
Availability comes from removing single points of failure while keeping writes consistent.
- Run the database with a primary and a synchronous standby, so a failover does not lose acknowledged payments.
- Spread instances across availability zones.
- Use timeouts and circuit breakers on every PSP call. A circuit breaker stops sending traffic to a dependency that is failing, so threads are not tied up waiting.
- Route to a backup PSP when the primary is down. Reuse the same payment record and idempotency rules so the customer is not charged by both.
Common errors
- Checking idempotency on a read replica, which allows duplicates.
- Retrying without jitter, which turns a brief outage into a retry storm.
- Failing over to a standby that is behind, which loses recent payments.
- Sharding by
payment_id, which splits one merchant's keys across shards and breaks the uniqueness check. - Letting the system accept payments when the ledger is unavailable. It is safer to reject new requests than to create unrecorded money movement.
Exercise
Why is merchant_id a better shard key than a random payment_id for this design, and why must the idempotency check read from the primary?
Show answer
The uniqueness constraint on (merchantid, idempotencykey) can only be enforced atomically within one database. Sharding by merchantid keeps all of a merchant's keys on one shard, while a random paymentid would spread them across shards. The check must read from the primary because a replica may lag and miss a recently written key, which would let a duplicate charge through.