Skip to the page

Testing a feature and estimating work · mid

Testing money, idempotency and rate limits

Features with money or abuse risk need tests for amounts, retries and races. Concurrency bugs need parallel tests against a real database, run many times.

The notes

What you must know

  • Refund cases: full, partial, several partials up to the paid amount and never above it, unpaid or failed orders rejected, amounts in paise, discounts spread across items, different timelines for UPI, cards and wallets, roles and limits, an audit record each.
  • Race test: two refunds at once on one order must never exceed the amount paid. Use a row lock or a conditional update plus a constraint. Run the test many times.
  • Gateway behaviour: stub timeouts, 5xx, slow answers, pending-then-processed and failed. A timeout makes the refund pending, confirmed by a status check or webhook; a retry reuses the same idempotency key. Duplicate and out-of-order webhooks change nothing twice.
  • Idempotent create-order cases: same key and body returns the same response; same key with a different body gives 409 or 422; a new key creates a new order; keys are scoped per customer; expiry works as documented.
  • Only a unique constraint or a lock on the key is safe. "Check, then insert" fails when many identical requests arrive at once. Test crash safety by killing the process between steps.
  • Rate limiter cases: the 5th request allowed and the 6th blocked, reset after the window, the edge of a fixed window (it can let 10 through around the boundary, unlike a sliding window), separate counts per number and per IP, several instances sharing one counter.
  • The count must be atomic (INCR with EXPIRE, or a Lua script). Use an injectable fake clock. Decide what happens when Redis is down: for OTP fail closed; for read APIs failing open may be fine.
  • Rate limit details: 429 with Retry-After, the same message for unknown numbers, the client IP taken from a trusted proxy, E.164 normalisation, and generous per-IP limits because of carrier NAT.

The red pen

Common traps: mistake → fix

  • Mistake: Testing one request at a time proves idempotency

    Fix: race many identical requests at once.

  • Mistake: A unique constraint is not needed if the code checks first

    Fix: the check and insert are not atomic.

  • Mistake: A fixed window limiter allows exactly N per window

    Fix: it can allow almost 2N around a boundary.

Easy to hard

Questions interviewers ask, easy to hard

  1. How would you test a refund API?
  2. How do you test that create-order is idempotent?
  3. How do you test a rate limiter on an OTP endpoint?
  4. How do you prove two parallel refunds cannot exceed the paid amount?

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