Skip to main content
Saved

Resilience by Design

foundation
13 minute read
written by
den odell
added

Systems keep working when things fail if you design for failure and recovery from the start, instead of adding error handling at the end.

server errorYour tests should cover what happens when the API returns a 500.

TL;DR

Networks drop and APIs time out, so with resilience by design, you plan for failure and recovery from the start. You can’t prevent every failure, but you can keep your app working when one happens.

  • You plan how each feature handles failure before you build it, instead of adding error handling at the end
  • A failure in one part of your app stays in that part, so it doesn’t crash everything else
  • Your error messages tell users what happened and what they can do about it
  • Anything a user has typed or uploaded is saved, so they don’t lose it when something fails

Hope is not a strategy.

Common saying

Accepting Reality

On the web, things break all the time. Networks drop, APIs time out, users click buttons twice, data arrives malformed, servers return errors, and JavaScript fails to load. All of these happen to real users every day, so you can’t leave them as edge cases to get to eventually.

But a lot of applications are designed for the happy path. Teams build features as if everything will work, then add error handling as an afterthought, and the software can break in unpredictable ways once real users and real networks are involved.

With resilience by design, we start by assuming things will fail, and we design how the app recovers.

So when we plan a feature, we ask “what happens when this fails?” as well as “what should happen when this works?” Asking that early changes how we structure the whole system.

The web is distributed and asynchronous, and it runs on devices and networks we don’t control. If we design for that, we end up with applications that keep working, and that users can trust, even when some of the pieces fail.

Defining Resilience

A resilient app can handle a failure without falling over, and recover from it when that’s possible. You can’t prevent every failure, but you can control what happens when one occurs.

Resilience comes in degrees. At one end is fault tolerance, where the app deals with a failure quietly and users might not notice anything went wrong. At the other is failure transparency, where users can see what failed and what they can do about it.

The Resilience Spectrum

Invisible Handling

Fault Tolerance: The app keeps working when parts of it fail

  • Data table renders and skips the malformed cell
  • Dashboard shows the widgets that loaded and hides the ones that failed
  • Users might not notice anything went wrong

Graceful Degradation: Users still get some of the functionality

  • Image gallery shows thumbnails when the high-resolution images fail to load
  • Search shows cached results when live search is unavailable
  • Rich editor falls back to a plain textarea

Visible Recovery

Error Recovery: The app gets back to a working state after a failure

  • Form retries the submission after a network timeout
  • Video player rebuffers after the connection drops
  • Sync process resumes when the device comes back online

Failure Transparency: Users can see and understand what failed

  • Clear error messages explaining what went wrong
  • Guidance on what users can do
  • Honest acknowledgment of the problem, with a way forward

Each level suits a different kind of failure, and most of the work is in choosing the right level for each failure your app might run into. Some failures should be invisible to users, while others need the user to notice them or do something.

At all four levels, you plan for failure from the start, as a normal part of how the app works.

Happy-Path Design Is Brittle

A lot of code is written as if everything will succeed. Developers write the flow for when data loads and users behave predictably, and if there’s any error handling, it’s added afterward, usually as a catch block that logs something and maybe shows a generic error message.

Code written this way is brittle, and a single failure can take down everything around it. An API timeout can crash the entire page instead of just the section that needed that data, and a malformed response might cause an exception that breaks unrelated components.

One problem can lead to another, too. If a component doesn’t validate its data, it can pass corrupt data on to other components, and without error boundaries, exceptions can bubble up and crash the whole application. A loading state that never times out can leave users unsure whether to wait or refresh, and if your app doesn’t validate its state, it can end up with combinations of values that should never be possible.

With happy-path design, you’d build a typical e-commerce checkout as one flow: the user enters a shipping address, selects a payment method, reviews the order, and confirms the purchase. But what happens when the address validation API is slow? When the payment processor returns an unexpected error code? When the user’s session expires mid-checkout? When the network drops after the user clicks “confirm” but before the confirmation arrives? You need to design for each of these, both what the user sees and how the code handles it.

If things break unexpectedly, users can feel anxious and confused. Did I do something wrong? Is my data lost? A happy-path app has nothing to tell them, because the team never planned for that case.

Defensive Architecture

With defensive architecture, you assume that anything can fail.

Defensive Principles

You check data, set timeouts, and plan fallbacks wherever something can go wrong.

Every input is suspect. Data from users can be malicious, and data from APIs or your own storage can be malformed or out of date. Your API “should” return a certain format, but that doesn’t mean it always will, so you validate data at the boundaries where it comes into your app.

Every boundary is a failure point. Component boundaries, API boundaries, storage boundaries and module boundaries are all places where one side sends something the other side didn’t expect, or a call never gets through. At each boundary, your code should check the data it receives.

Every operation needs a timeout. Network requests can hang forever, and a promise might never resolve. Users would usually rather see an error than wait without knowing what’s happening, so if something takes too long, your code should treat it as a failure once the timeout passes and tell the user what happened.

Every dependency needs a fallback. The CDN can go down, or a third-party script can fail to load. Your critical features shouldn’t depend on things you don’t control unless there’s a fallback, such as cached data or a simpler version of the feature, so users still get something useful when the dependency fails.

In practice, you design components with explicit failure states, and you build timeout and error handling into your data fetching from the start. You design your state management to handle partial and invalid data, and you plan each feature with its failure cases in mind.

Containing Failure

In a resilient system, failures are also contained, so a problem in one part doesn’t break unrelated parts.

You contain failure mostly with error boundaries, which wrap sections of the application and catch errors thrown while the components inside them render. If one of those components throws, the boundary displays fallback UI instead of the whole tree crashing. A broken widget shouldn’t take down the dashboard, and a failing comment section shouldn’t stop someone reading the article.

Placing error boundaries is part of designing your architecture. With too few, a failure can spread too far, and with too many, the app can end up full of small fallbacks that don’t fit together.

You’ll get the most out of error boundaries when you put them around sections that can fail independently. A settings page might have boundaries around each section, so if notification preferences fail to load, account settings still work. A social feed might have boundaries around each post, so if one post’s media fails, other posts still display. The boundaries should follow the sections that your users already think of as separate.

You can also contain failure by isolating state. If a lot of components share the same state, bad data in it can spread to all of them, but if each piece of state is kept close to the component that uses it, a problem in it only affects that component.

Circuit breakers stop repeated failures from spreading. If an API call fails three times, your app might stop trying for a while, and if a component keeps crashing, the app might stop rendering it. Unlike a simple retry, a circuit breaker backs off when a failure keeps happening, instead of sending more requests to a server that’s already failing. That gives the server time to recover, and the user can keep using other parts of the application in the meantime.

You’ll usually need all three, because each one handles a different kind of failure. Using several layers like this is called defense in depth.

The User Experience of Failure

Resilience is about user experience as well as code, because users only see what the app shows them when something fails.

Help Users Through Failures

Understanding & Agency

Understanding: What happened?

  • Not “Something went wrong”, which is too vague
  • Not “Error 500: Internal Server Error”, which is too technical
  • But “We couldn’t save your changes because the network connection was lost”

Agency: What can they do about it?

  • Next steps they can actually take
  • A way to retry, when retrying makes sense
  • A way to cancel or come back later

Preservation & Path Forward

Preservation: Is their work safe?

  • “Your work is saved locally and will sync when you’re back online”
  • User input that’s never lost without a warning
  • Drafts that auto-save and form data that’s kept

Path Forward: How do they continue?

  • Status updates while the problem is being fixed
  • Estimated resolution times when known
  • Alternative actions if available

Generic error messages give users none of these. “Something went wrong” doesn’t say what happened or what to do next, and it can leave users anxious. Technical error messages can be even worse, because when non-technical users see a stack trace or an error code, it looks like even the developers don’t know what happened.

A good error message says exactly what happened and what the user can do about it, such as “We couldn’t save your changes because the network connection was lost. Your work is saved locally and will sync when you’re back online.” With that message, users know their work is safe, so they can stop worrying and either fix their connection or keep working.

A temporary failure that clears up without the user doing anything might not need a message at all, just a small indicator that syncing is paused. If the user caused the error, they need clear guidance on what to fix, and if the system failed for reasons outside their control, they need you to acknowledge it and, if you have one, give an estimate of when it’ll be fixed.

Loading states are part of this too, because as far as the user is concerned, a spinner that never stops is a failure. Users need to know whether to wait or give up. A progress indicator tells them how much longer it might take, and a cancel option lets them stop. “Still loading…” after thirty seconds is better than a spinner with no explanation, and “This is taking longer than usual. You can wait or try again” lets the user choose.

If your app loses something a user typed, you can lose their trust very quickly, so keeping their work safe comes first.

Form data in your app should survive page refreshes, and drafts should auto-save. Anything a user has typed or uploaded needs protecting. Local storage and IndexedDB both work for this, and you should save to them as the user works, in case the submission fails.

Building Resilience In

Resilience is hard to add at the end, so it needs to be part of your architecture from the beginning.

The data fetching for a resilient system includes error states, loading states, timeout handling, and retry logic from day one, instead of fetching data first and figuring out errors later. More and more libraries that manage server state build this in, and give you loading and error states alongside the data itself.

You also design your APIs and data structures to allow for uncertainty, so a user profile becomes { data, isLoading, error, lastFetched } instead of { name, email, avatar }. The component that renders the profile receives all of this and can decide what to display in each state.

Your component design includes failure props and fallback rendering too. A UserProfile component takes user data, and it also handles the case when that data is unavailable. Because failure handling is part of the component’s API, developers using the component don’t have to remember to wrap it in error handling.

Parent components that render children need a plan for when a child fails, too. Wrapper components can provide default error boundaries, and layout components can handle the empty space when content components fail.

Your state management has to allow for invalid and partial states as well. Not every combination of state values is valid, so you either make invalid states impossible to represent, or you handle them explicitly. State machines and explicit state modeling help here, because they make the possible states visible and control how the app moves from one state to another.

Your testing should cover failure paths as thoroughly as success paths, with tests for what happens when the API returns 500, when data is malformed, when an operation times out, and when the user is offline. If your failure handling isn’t tested, it might not work when you need it.

The Mindset Shift

To build resilient systems, we have to change how we think about development.

Two Approaches to Building Software

The Optimistic Mindset Asks: "What should happen when this works?" The Resilient Mindset Asks: "What happens when this fails?"
  • Designs the happy path first
  • Grudgingly handles errors later
  • Produces code that works in demos
  • Breaks unpredictably in production
  • Leaves users confused when things fail
  • Designs for failure first
  • Builds the success path on top of that
  • Produces code that copes with real networks and users
  • Degrades gracefully when things fail
  • Keeps users' trust when things go wrong

You don’t have to over-engineer it, though. Not every failure needs handling, and you still have to ship, so you work out which failures are likely and which would do the most damage, and you put the effort there.

Your users will run into network interruptions, slow responses, invalid data, and their own mistakes all the time, so those need thorough handling. Rare but severe failures might only need a simple fallback, such as a friendly error page instead of a blank screen when everything breaks. And if an edge case only happens when a lot of unlikely conditions line up, it can be reasonable to let it fail.

You make these decisions deliberately, so you know which failures you’re handling and which you’re accepting. You should design the user experience of failure as carefully as the user experience of success.

Progressive enhancement and resilience by design both start from the idea that you can’t count on ideal conditions, so you design for the worst case first. Progressive enhancement deals with browsers that don’t support a feature and scripts that don’t load, and resilience by design deals with failures while the app is running. If you build with both, your app can keep working on real devices and real networks.

Summary

Resilience by design changes how we build for the web:

  1. Failure is normal: Networks drop and APIs time out, so you design for failure from the start
  2. Failures stay contained: Error boundaries and state isolation stop one failure from bringing down everything else
  3. You design what users see when things fail: They need to understand what happened, know what they can do, keep their work, and have a way to continue
  4. Resilience is built in from the start: You build components and state to allow for failure
  5. Failure paths get tested: You test error handling as thoroughly as success paths
  6. Trade-offs are deliberate: Not every edge case needs handling, but you know which ones you’re leaving unhandled

Failures will happen on the web whatever you do, but if you’ve designed for them, your users still have an app that keeps working and doesn’t lose their data when one happens.


Related Content:

External Resources:

Next Foundation · 10 min read

The Progressive Enhancement Mindset

With progressive enhancement, your web app keeps working for everyone, even when some of its code fails to load.

Read the next Foundation →

Keep Exploring

A monthly email
from Den Odell.

What I'm thinking about frontend patterns, plus news when the site changes.

No spam. Unsubscribe anytime.