Blog

How to Break Your Own API on Purpose

September 6, 2026Xerat024 min read
Share
How to Break Your Own API on Purpose

There's a specific kind of bug report that's more frustrating than a clean crash: "sometimes it works, sometimes it doesn't." No stack trace, no consistent repro steps, just an intermittent failure that shows up in production logs a few times a week and refuses to happen on demand when you're actually looking at it.

A large fraction of these turn out to be timing problems: a request that occasionally takes longer than an assumed timeout, a retry that fires before the thing it's waiting on has actually settled, a race between two calls that usually resolves in the lucky order and occasionally doesn't. These are hard to debug by reading code, because the code is often "correct" in the sense that it does exactly what it says — it's just that what it says turns out to be wrong under a timing condition nobody thought to write down, let alone test.

Why you can't just "try it again until it fails"

Waiting for a flaky condition to occur naturally has an obvious problem: it's flaky. If a race condition fails 1 time in 50, you can spend an afternoon manually retrying and either get unlucky and never see it, or get "lucky," see it once, and still have no reliable way to confirm your fix actually addressed the cause rather than just moved the odds slightly.

What you actually want is the ability to force the specific timing condition on demand — make the slow response slow every time you ask it to be, make the failure happen at a rate high enough to observe repeatedly in a short test session, and confirm your fix survives that forced condition consistently, not "seemed better over a few tries."

The two knobs that matter: latency and failure rate

Most flaky-API bugs come down to a combination of two things that a normal dependency does unpredictably and that you can instead set deliberately when testing against a mock of it:

Added latency. A dependency that's usually 100ms but occasionally 4 seconds will expose every place your code assumed "fast" — a timeout set too aggressively, a UI with no loading state because the response "always" comes back before anyone would notice, a batch of requests fired in parallel that all queue up behind each other under load. Setting a fixed delay on a mocked endpoint — even a large, obviously-unrealistic one like 5 seconds — turns "does my loading state actually work" from a question you hope the answer to into one you can just look at.

Failure rate. A dependency that fails outright some percentage of the time is what actually exercises retry and backoff logic. This is the part that's easy to get wrong in a way that's invisible until it matters: retry logic that's never seen a real failure might retry immediately in a tight loop (making an already-struggling dependency worse), might not distinguish a retryable 503 from a non-retryable 400 (retrying something that will never succeed), or might simply not be wired up to the actual failure path at all, because nobody ever exercised it.

The trap here is testing only the extremes. A route configured to fail 100% of the time tells you whether your error UI exists at all, which is useful but limited — it doesn't tell you whether a transient failure is actually handled, because a 100%-failure mock never gives your retry logic a chance to eventually succeed. A more honest test sets a partial failure rate — 10%, 20% — and confirms that a request which fails once and retries actually goes on to succeed, the way a real transient blip would resolve.

Making this repeatable instead of anecdotal

The useful version of this isn't a one-off manual test where you hardcode a setTimeout in a debug branch of your code and then remember to remove it. It's a mock of the dependency that you can configure once — this endpoint responds in 3 seconds, this one fails 15% of the time with a 503 — and then run your actual, unmodified client code against, so the same test is repeatable by anyone on the team and doesn't require temporarily sabotaging production code to run it.

That's what delay and chaos injection are for on a MockBase route: a configurable, fixed delay for the latency case, and a configurable per-request failure rate (with the status code of your choosing) for the flakiness case — both usable independently or together, against real client code making real requests, so what you're testing is your actual retry and timeout logic, not a mental model of how you assume it behaves.

The bugs that only show up "sometimes, randomly, in production" are usually bugs that were always there — they just needed a specific, uncommon timing condition to surface. Making that condition common, on purpose, in a test environment, is a lot cheaper than waiting for production to hand it to you again.

X

Xerat02

Building MockBase.