Spot the flaws: a payments API
A design review exercise. Eight flaws are planted below. Read the whole document, note your concerns, then reveal the answers at the end.
Design: Payments API v2
Section titled “Design: Payments API v2”Author: Payments platform team · Status: Ready for architecture review · Reviewers: you
1. Context
Section titled “1. Context”We process card payments and bank transfers for about 1,200 merchant customers. Today’s API is a monolith endpoint that calls the card processor and the bank synchronously and returns the result. Merchants report duplicate charges a few times a month, almost always after a timeout. Peak load is 300 payment requests a second on the last business day of the month.
2. Goals
Section titled “2. Goals”- No duplicate charges, including when merchants or networks retry.
- 99.99% availability for accepting payments.
- Merchants learn the final outcome of every payment.
- Payments complete within 3 seconds for 99% of card payments.
Non-goals: changing the card processor; supporting new payment methods.
3. Architecture
Section titled “3. Architecture”4. Key decisions
Section titled “4. Key decisions”4.1 Idempotency keys. Every POST /payments requires an Idempotency-Key header. Our merchant SDKs generate a new UUID for each HTTP request automatically, so merchants don’t have to think about it. The payment service stores the key with the response and returns the stored response if it sees the same key again.
4.2 Key retention. Idempotency keys are kept for 1 hour, which comfortably covers retries. Our mobile SDK queues payments made while the device is offline and submits them when connectivity returns, for up to 24 hours.
4.3 Accept, then process. POST /payments validates the request, stores the payment as pending and returns 202 Accepted with a payment ID. Processing continues asynchronously; merchants receive a webhook with the final outcome, and can poll GET /payments/{id}.
4.4 Retries. To ride out transient failures, the gateway retries failed calls to the payment service up to 3 times, the payment service retries failed calls to the adapters up to 3 times, and the adapters retry the provider up to 3 times. All retries use exponential backoff with jitter.
4.5 Timeouts. The gateway times out after 10 seconds. The bank adapter allows the bank up to 30 seconds, because transfers are occasionally slow.
4.6 Events. After the payment is committed to the database, the payment service publishes PaymentSucceeded or PaymentFailed to the event bus. The ledger and the webhook service consume these events. Consumers deduplicate on payment ID.
4.7 Ledger. The ledger is append-only. Corrections are new entries that reference the original; nothing is updated in place.
5. Reliability
Section titled “5. Reliability”5.1 Availability. The payment service runs 3 replicas behind a load balancer, which takes us from 99.9% for one instance to well beyond 99.99%. The replicas, the database primary and its synchronous standby all run in one availability zone, to keep the 3-second latency target.
5.2 Objective and alerting. Our SLO is 99.99% of POST /payments requests returning a 2xx status, measured at the gateway. We alert when the error rate exceeds 0.1% over 5 minutes.
5.3 Rate limits. Each merchant has a rate limit at the gateway, so one merchant’s burst can’t starve the others.
6. Operations
Section titled “6. Operations”6.1 Reconciliation. Every night, a job compares the number of payments and the total amount in our database with the card processor’s and the bank’s settlement files. A mismatch opens a ticket for the finance team.
6.2 Secrets. Provider credentials are in the secrets manager and rotated automatically every 30 days.
7. Rollout
Section titled “7. Rollout”New merchants go to v2 first; existing merchants move in batches of 50, with v1 kept running until the last batch has been on v2 for 30 days.
Reveal the flaws
Section titled “Reveal the flaws”Try to find all eight before opening these.
Flaw 1, in section 4.1
The SDK generates a new idempotency key for every HTTP request. A retry is a new HTTP request, so it gets a new key, and the server can’t recognize it. This is exactly the duplicate-charge problem the design sets out to fix.
Ask for: a key generated once per payment intent, stored by the client before the first attempt, and reused for every retry of that payment. A deliberate second payment gets a new key.
Learn it: The reservation that succeeded twice · Check C-01
Flaw 2, in section 4.2
Keys expire after 1 hour, but the mobile SDK can retry for up to 24 hours. An offline payment that was actually processed, followed by a retry after the key expired, is charged twice. The retention has to cover the longest time any client can retry, not a typical retry.
Ask for: retention at least as long as the longest client retry window, plus margin, or an SDK that stops retrying before keys expire. Also ask what the server does when a key is reused with a different request body.
Learn it: The idempotency key that hid a changed order · The cancellation that arrived before the booking, whose exercise covers retention
Flaw 3, in section 4.4
Three layers each retry three times: up to 4 × 4 × 4 = 64 provider calls for one payment. Backoff and jitter spread the retries out, but they don’t reduce the multiplication. When the card processor slows down at month-end peak, retries multiply load on it at the worst moment.
Ask for: retries at one layer only, the one closest to the failure that can retry safely, with a retry budget; other layers fail fast.
Learn it: Rule that breaks: “Retries make systems more reliable”, with a simulator · Incidents by mechanism: retries amplify overload
Flaw 4, in section 4.5
The caller gives up after 10 seconds, while the bank call can run for 30. Combined with section 4.4, the gateway times out and retries while the original bank transfer is still in progress, and may still succeed. The outcome is unknown, and nothing in the design resolves unknown outcomes.
Ask for: timeouts that shrink down the call chain, so each caller waits longer than everything it calls; an explicit unknown state; and a status lookup at the bank, using the payment’s own reference, before anything is retried.
Learn it: The outbox that shipped two packages, on side effects with unknown outcomes
Flaw 5, in section 4.6
The event is published after the commit, as a separate step. If the service crashes between committing and publishing, the payment exists but the ledger and the merchant never hear about it: a lost event. This is the dual-write problem, and the design has no recovery for it.
Ask for: a transactional outbox, so the payment and its event are committed together, with consumers that tolerate duplicates. The design already deduplicates on payment ID, which is the right key.
Learn it: The outbox that shipped two packages · Example Lending, ADR-008
Flaw 6, in section 5.1
Three replicas in one zone can’t exceed the zone’s availability. The 99.99% claim assumes replicas fail independently, but a zone outage takes out all three replicas, the primary and the standby together. The database, not the replicas, also decides availability.
Ask for: replicas and a standby across zones, with the latency cost measured rather than assumed; or an availability target the single-zone design can honestly meet.
Learn it: Rule that breaks: “More replicas mean higher availability”, with a simulator for zones
Flaw 7, in section 5.2
The SLO measures POST /payments returning 2xx, but section 4.3 returns 202 before processing. If processing stops entirely, every request still returns 202 and the SLO stays green. The goal in section 2, merchants learn the final outcome, isn’t measured at all.
Ask for: an objective on the journey: payments reaching a final state within a deadline, with an alert on the age of the oldest pending payment.
Learn it: The healthy dashboard for a broken customer journey · Drill: the orders that never arrived
Flaw 8, in section 6.1
Reconciliation compares counts and totals. A payment recorded against the wrong merchant, or a duplicate offset by a missing payment, leaves both totals unchanged. Finance never gets a ticket.
Ask for: reconciliation by key, matching each payment by its provider reference, and reporting every missing, extra and different payment.
Learn it: The migration that passed reconciliation, with a lab · Check C-15
Looks wrong, but is fine
- Section 4.3, returning 202 and finishing asynchronously. Reasonable for a payment that may take seconds; the problem is only how it’s measured (flaw 7).
- Section 4.7, the append-only ledger. Exactly right for money: corrections are visible, history can’t be rewritten.
- Section 5.3, per-merchant rate limits. Sound, and the right place for them is the gateway.
- Section 7, migrating in batches with a 30-day overlap. Cautious and correct for an API merchants integrate with.
- Section 4.4, backoff with jitter. Good practice; it just doesn’t fix multiplication across layers (flaw 3).
Score yourself
Section titled “Score yourself”| Found | What it suggests |
|---|---|
| 7 or 8 | You review like someone who has been paged for these. |
| 4 to 6 | Solid. The ones you missed are worth reading in full. |
| 3 or fewer | Normal for a first attempt. Read the linked stories, then try the eligibility integration. |
Count how many sound decisions you flagged as well. In a real review, each false alarm costs the credibility of your real findings.
Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.