Skip to the page

Common backend failure patterns · mid

Duplicates: charged twice, jobs that run twice

A timeout means unknown, not failed. Retries, many replicas and lost locks create duplicates. Make the operation itself idempotent with a stored key and a state machine, not only the schedule.

The notes

What you must know

  • A timeout means the payment may have gone through. Marking it failed and inviting a retry causes a double charge. Show "pending" and confirm with a status check or webhook.
  • A call with side effects is safe to retry only if it is idempotent (Amazon Builders Library).
  • One idempotency key per payment attempt, saved before the call and reused on every retry. Stripe, for example, replays the saved response for a repeated key and rejects a key reused with different parameters.
  • A state machine (created, pending, captured, failed) where a timeout means pending, plus a status check before any retry. Webhooks update state and are themselves deduplicated.
  • Shared HTTP clients that retry POST once on any timeout are a hidden cause. Turn automatic retries off for payment POSTs.
  • In-process schedulers (@Scheduled, node-cron and similar) fire in every replica. Going from 1 to 2 replicas for a sale doubles every run.
  • A lock with a TTL is a hint, not a guarantee: a run longer than the TTL lets the next one start while the first still works.
  • Fix the work itself: claim rows atomically (UPDATE ... SET status = processing WHERE status = approved ... RETURNING, or SELECT ... FOR UPDATE SKIP LOCKED), send a fixed payout reference so the bank rejects repeats, use a unique constraint, and run schedules in one place (a Kubernetes CronJob with concurrencyPolicy: Forbid).

The red pen

Common traps: mistake → fix

  • Mistake: A timeout is a failure

    Fix: it is unknown; the other side may have succeeded.

  • Mistake: A distributed lock makes a job run once

    Fix: locks expire and processes pause; make the operation idempotent.

  • Mistake: Retry with a new idempotency key each time is fine

    Fix: the gateway sees different payments; reuse one key.

Easy to hard

Questions interviewers ask, easy to hard

  1. Customers were charged twice after gateway timeouts. How do you find the cause and fix it?
  2. What is an idempotency key and where is it stored?
  3. A cron job ran twice and paid sellers twice. What do you do first?
  4. Why is a Redis lock with a 10-minute TTL not enough for a job that can run 26 minutes?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview