What Generalizes

You will probably never build a payments API. You will very likely build something that moves an inventory reservation, sends an email that can't be unsent, provisions a resource that costs money the moment it exists, or calls a partner API that has its own side effects. The eight principles below are what's left of this guide once you subtract "Stripe" from it.

1. Make retries safe, and say so in the contract

Idempotency isn't an implementation detail you bolt on when someone files a bug about a duplicate — it's a first-class, documented part of the API surface, with an ergonomic client-facing mechanism (see Idempotency: the contract, Idempotency: the machinery). The design choice worth stealing: Stripe makes idempotency the client's responsibility, not the server's guess. The client picks the key, because only the client knows what "the same request" means for its own retry logic — a server-generated key would have to guess at the caller's intent and would guess wrong.

Concretely: an internal "provision a VM" API that doesn't take a client-supplied idempotency key forces every caller to build their own deduplication layer on top of it — and every caller will build a slightly different, slightly buggy one. Put the mechanism in the API once, and document the field, the header name, and the retention window (Stripe's is a floor of at least 24 hours) as part of the contract, not as a support-forum tip.

2. Scope everything per tenant

Idempotency keys, rate limits, quotas — every one of them keyed to the account, never global (see Idempotency: the contract, Rate limiters and load shedding). A global namespace for any of these lets one tenant's bug or abuse degrade every other tenant sharing it. This is the same design decision made twice, at two different layers of the same system, and that repetition is the signal it's a real principle and not a coincidence.

Concretely: a job queue that rate-limits by worker process instead of by originating customer will happily let one noisy customer's jobs consume every worker, starving everyone else — the fix isn't a bigger queue, it's a per-tenant limit inside it.

3. Return a durable object, not just a result

A PaymentIntent ID (see PaymentIntent state machine) converts "did my request work?" from a guess you make based on whether a response arrived into a GET you can issue at any time, from any process, with no memory of having made the original call. Any API that does something consequential should hand back a stable identifier for that consequence, not just a 200 and a payload that vanishes with the response.

Concretely: an email-sending API that returns only {"sent": true} gives a caller nothing to check after a timeout. One that returns {"id": "msg_abc123"} lets the caller ask "did msg_abc123 go out?" forever afterward — turning an unanswerable question into an ordinary lookup.

4. Model the lifecycle explicitly as a state machine

A boolean — succeeded: true/false — cannot express "in progress, might still succeed, safe to retry, do not treat as final yet" (see PaymentIntent state machine). An asynchronous, resumable, externally-driven process needs named states and explicit legal transitions between them, because every state that isn't named becomes a special case some caller eventually mishandles.

Concretely: an inventory reservation system with only reserved: true/false can't distinguish "reservation is being retried after a transient failure, still safe to wait" from "reservation permanently failed, release the stock" — a caller reading the boolean has to guess, and guesses in inventory systems eventually oversell.

5. Push the irreversible step as late as possible

Find the one step in your process that can't be undone with a compensating action (see Sagas and compensation) — the pivot — and structure everything else around minimizing what happens after it. In payments that's capture, or clearing, depending on how you draw the line; in your system it's whatever step has no clean "undo." Everything before the pivot is cheap to abandon. Everything after it needs a saga, not a rollback.

Concretely: in a travel-booking flow, holding a seat is reversible right up until the airline's system issues a ticket number — everything before that (search, price, hold) can be abandoned for free, and everything after it needs an explicit cancellation flow, not a delete.

6. Deliver events at-least-once, and make the consumer idempotent

At-least-once with idempotent handling (see Webhooks) is the only delivery guarantee that doesn't couple your uptime to your consumer's. Exactly-once delivery either doesn't exist in a distributed system or is exactly-once-effective-delivery wearing a disguise — it's at-least-once delivery plus deduplication, moved from being your consumer's explicit job to being an invisible implementation detail you can't audit or reason about when it breaks.

Concretely: a message bus that promises exactly-once and silently drops a message during a rebalance is worse than one that promises at-least-once and hands you a duplicate — the duplicate is annoying but visible and handleable; the silent drop is invisible until someone notices a missing record weeks later.

7. Degrade in layers

No single mechanism protects a system from every kind of overload (see Rate limiters and load shedding). A rate limiter, a concurrency limiter, and a load shedder measure different things and fail differently when missing. Build the layer that matches the failure you're actually worried about, and don't assume the one you built covers the ones you didn't.

Concretely: a per-second rate limit on your internal search API won't stop a single slow, expensive query from pinning every worker for thirty seconds — that failure needs a concurrency cap or a query timeout, a genuinely different mechanism measuring a genuinely different thing.

8. Keep an append-only record of every irreversible thing you did

Once money, inventory, or any other scarce, real-world thing moves, the record of that movement should never be edited — only appended to (see Money invariants). The audit trail, the ability to answer "why is this number what it is," and the ability to detect a duplicate after the fact all fall out of that one discipline for free. A system that edits its own history in place can't tell you what it used to believe, which is exactly the information you need most when something has gone wrong.

Concretely: an access-control system that updates a permissions row in place instead of appending a grant/revoke event log can't answer "who had access to this file on the day of the incident" — which is precisely the question a security review will ask, and precisely the one an append-only log answers by construction.

A payment is just the clearest example of a rule that applies everywhere: the guarantee a distributed system can actually make is never "this happened exactly once" — it's "this is safe to say again," and every mechanism in this guide is a different way of buying that second sentence.

The Invariants

  • Guarantees: These eight principles are the minimum set of decisions that keep an API honest about an irreversible action it initiates across a trust boundary it doesn't control — they apply whether the irreversible thing is a charge, an email, a provisioned resource, or a shipped package.
  • Does not guarantee: That naming these principles builds the system. Each one still needs the specific mechanism this guide spent ten sections on — a real state machine, a real ledger schema, a real rate limiter — to become anything other than good intentions.