Back to insights
Engineering
5 min read

Designing for Failure: Building Systems That Bend Instead of Break

Every system you build will fail. Networks drop packets, third-party APIs slow to a crawl, databases hit connection limits, and deployments occasionally go sideways. The difference between a fragile product and a dependable one is not whether failures happen. It is what the system does when they do.

This post walks through the practical habits that make services degrade gracefully instead of collapsing, with patterns you can apply this week.

Start by assuming failure

Most outages are not caused by one catastrophic event. They are caused by a small failure that was allowed to spread. A slow payment provider ties up your request threads, the queue backs up, the database fills with waiting connections, and suddenly an unrelated page is down.

The mindset shift is simple: for every dependency, ask three questions.

  • What happens if it is slow?

  • What happens if it returns an error?

  • What happens if it is completely unreachable?

If you cannot answer all three in one sentence, you have found a risk worth addressing.

Timeouts come first

The most underrated reliability tool is the humble timeout. Without one, a call to a misbehaving service can wait indefinitely, and every waiting request consumes memory, connections, and attention.

Set explicit timeouts on every network call, and make them shorter than you think. A useful rule is to base them on how long a user would reasonably wait, then subtract the time your own service needs to respond.

TypeScript
async function fetchWithTimeout(url: string, ms = 2000) {
  const controller = new AbortController();
  const timer = setTimeout(() => controller.abort(), ms);
  try {
    return await fetch(url, { signal: controller.signal });
  } finally {
    clearTimeout(timer);
  }
}

A fast failure is almost always better than a slow one, because a fast failure lets you do something useful next.

Retry with care

Retries are powerful and dangerous. Used well, they smooth over brief network blips. Used badly, they multiply load on a service that is already struggling and turn a small incident into a large one.

Three rules keep retries safe:

  • Retry only what is safe to repeat. Reads are usually fine. Writes need idempotency, which we cover below.

  • Use exponential backoff. Wait longer between each attempt so the failing service gets room to recover.

  • Add jitter. Randomizing the delay prevents thousands of clients from retrying in lockstep.

TypeScript
const delay = Math.min(30_000, 200 * 2 ** attempt);
const jittered = Math.random() * delay;

Also cap the total number of attempts. Three tries is usually plenty.

Circuit breakers stop the bleeding

If a dependency is clearly down, continuing to call it wastes resources and slows everything else. A circuit breaker watches for repeated failures and, after a threshold, stops sending requests for a cooling-off period.

It works in three states. Closed means traffic flows normally. Open means calls fail immediately without touching the dependency. Half-open means a small number of test requests check whether it has recovered.

The real value is not the library you choose. It is that your system makes a deliberate decision to stop hammering something that cannot help, and protects the rest of the product in the process.

Degrade on purpose

Graceful degradation means deciding in advance what a reduced experience looks like. Not every feature is equally important, so rank them.

Consider an online store. Checkout is critical. Product recommendations are nice to have. If the recommendation service fails, the right response is to hide that section or show popular items from a cache, not to break the whole page.

Some practical fallbacks:

  • Serve slightly stale cached data instead of an error.

  • Hide non-essential widgets rather than showing broken ones.

  • Queue writes for later processing when a downstream service is unavailable.

  • Show a clear, honest message when something genuinely cannot be done.

That last point matters. Users forgive a feature being temporarily unavailable far more readily than they forgive silent failure or a spinner that never ends.

Make operations idempotent

When retries and failures mix, you risk performing an action twice: charging a card two times, sending a duplicate email, creating two orders. Idempotency solves this by making repeated requests produce the same result as a single one.

The common approach is an idempotency key. The client generates a unique key per logical action and sends it with the request. The server stores the result against that key, and if the same key arrives again, it returns the stored result instead of repeating the work.

Once your writes are idempotent, retries stop being scary, and a whole category of subtle bugs disappears.

Isolate failures with bulkheads

Ships use watertight compartments so a leak in one section does not sink the vessel. Software can do the same. Give critical workloads their own connection pools, queues, or worker processes, so a flood of low-priority traffic cannot starve the important paths.

Even a simple version helps: separate the pool for background jobs from the pool for user-facing requests. When the batch job misbehaves, customers do not notice.

You cannot fix what you cannot see

Resilience without observability is guesswork. At minimum, track:

  • Latency percentiles, not just averages. The p95 and p99 reveal the pain that averages hide.

  • Error rates broken down by dependency.

  • Saturation, such as queue depth, pool usage, and memory.

Alert on symptoms users feel, like elevated errors or slow responses, rather than on every internal metric. A page that wakes someone at night should always mean something a customer would care about.

Practice failing

Teams that handle incidents well have usually rehearsed them. Run a game day: pick a dependency, disable it in a staging environment, and watch what happens. You will almost always discover a timeout that was never set, a fallback that does not work, or a dashboard that does not show what you need.

Start small and keep it low-risk. The goal is learning, not drama. Each exercise turns an unknown weakness into a known one you can fix calmly rather than at 3 a.m.

A short checklist

Before you ship your next service, confirm that:

  1. Every outbound call has a timeout.

  2. Retries use backoff, jitter, and a cap.

  3. Writes that may be retried are idempotent.

  4. There is a defined fallback for each non-critical dependency.

  5. You can see latency, errors, and saturation per dependency.

Final thoughts

Reliability is not a feature you add at the end. It is a set of small, deliberate decisions made throughout the design. You will never eliminate failure, but you can decide what it costs. Systems that bend instead of break earn something valuable: trust. And trust, once earned, is far cheaper to keep than to rebuild.

Written by

Software Engineer