Resilient Application Design Patterns


Resilient Application Design Patterns

Anna
Welcome to the episode. Today we're exploring five design patterns for building resilient applications: timeouts, graceful degradation, retries, exponential backoff, and circuit breakers. We'll also cover a practice called chaos engineering, which helps us test whether those patterns really work.

Michael
And the central idea comes from Werner Vogels: failures are given, and everything will eventually fail over time. In a modern distributed cloud application, your system depends on hardware, networks, services, and different technology stacks. Any one of those components can fail, so resilience means designing the application to recover and continue serving users.

Anna
The example application is deliberately simple: a React front end calls a .NET microservice. That microservice acts as a proxy through a joke client, which calls a third-party joke API. The joke API returns JSON containing a setup, a punchline, and a source, and the .NET service serializes that data back to React for display.

Michael
So the request path is React, then the .NET controller, then the joke client, then the third-party API, and the response travels back in the opposite direction. The React component's important function is called populate joke data. It calls the API endpoint, receives JSON, and renders the result in a table or on the page.

Anna
That architecture works perfectly when the third-party service responds. But imagine that service becomes slow or starts failing. A request can sit in the .NET service waiting for a response, while the browser waits too. More clients create more waiting requests, increasing the load until the microservice itself may be overwhelmed.

Michael
That is the failure chain we want to interrupt. The first pattern is a timeout. A timeout places a maximum limit on how long the application waits for a response from a dependency.

Anna
In .NET, one straightforward approach is to set the HttpClient timeout property. In the example, it was set to five seconds. If the joke API does not respond within those five seconds, the request stops waiting and a timeout exception is produced.

Michael
That is already better than waiting forever, because the microservice can release the request instead of allowing stalled work to accumulate indefinitely. But a basic timeout only tells us that the request failed; by itself, it does not decide what the user should see or whether another attempt should be made.

Anna
For more control, the example uses Polly, a .NET resilience library. Other programming languages have comparable resilience libraries. With Polly, a timeout policy can wrap the asynchronous request, and a cancellation token can be passed into the operation so the individual asynchronous task can be controlled more precisely.

Michael
So there are two timeout approaches here: configure the timeout directly on HttpClient, or use a policy library such as Polly. The library approach becomes especially useful because multiple policies can be combined and applied in a deliberate order.

Anna
That leads to the second pattern: graceful degradation. Instead of exposing an unhandled timeout or other exception to the user, the application supplies a reduced but usable response.

Michael
In the demo, Polly's fallback policy handles a generic exception and returns a list containing one locally defined joke. Its source is set to 'fallback,' so the UI can make it clear that the response came from the fallback rather than the third-party API.

Anna
The fallback joke is the band joke: 'I'm in a band called 1023 megabytes. We haven't had a gig yet.' When the third-party API takes longer than the timeout, the user receives that fallback joke instead of being left with an unhandled failure.

Michael
And graceful degradation is not the same as pretending the dependency worked. The response is intentionally a backup response, and the source field helps identify that state. If the API works on a later request, the application can return a normal third-party joke again.

Anna
The third pattern is retries. A retry policy gives a failed request another chance, because a temporary problem may disappear quickly.

Michael
The example defines a Polly retry policy that handles an exception and retries asynchronously. The retry count is set to five, and the retry policy is added to the policy wrapper along with the timeout and fallback policies.

Anna
The combined behavior is: try the original request, enforce the timeout, retry when the attempt fails, and if those attempts still do not succeed, use the fallback. In the demonstration, each attempt had a timeout, so several failed attempts could make the user wait much longer than a single timeout.

Michael
That brings up an important warning. A basic retry policy can amplify an outage. If many clients all retry a struggling third-party API several times, the extra requests increase its load and can resemble a distributed denial-of-service attack. So retries are useful, but retrying immediately and uniformly is not a good final design.

Anna
The fourth pattern is exponential backoff, which improves on simple retries by increasing the delay between attempts. Instead of retrying after the same interval every time, the waiting period grows exponentially.

Michael
The example sequence is two seconds, then four seconds, then eight seconds, and then sixteen seconds. This spreads requests out over time, giving the third-party service a chance to recover and reducing the pressure created by immediate retries.

Anna
In Polly, the retry policy is changed to a wait-and-retry policy. The delay is calculated using a power-of-two expression based on the retry attempt, producing that increasing sequence. The policy can still fall back if the retries do not produce a successful response.

Michael
But even exponential backoff has a subtle problem: retries can cluster. If many clients start at roughly the same time and all calculate exactly the same two-, four-, and eight-second delays, they may send their retries together. That clustering partly defeats the purpose of spreading the load.

Anna
The solution shown is jitter: add a random variation to each delay. The code adds a random number of milliseconds between zero and one thousand, creating roughly a plus-or-minus-one-second variation in the retry timing.

Michael
That randomness makes synchronized clients less likely to retry simultaneously. So the retry progression becomes exponential backoff with jitter: increasing delays, plus a random adjustment that distributes requests more naturally.

Anna
The fifth pattern is the circuit breaker. Retries are still wasteful when the dependency is clearly broken. If every attempt continues to fail, the application should stop sending requests temporarily rather than repeatedly hitting the same unhealthy service.

Michael
A circuit breaker watches for failures and, after a configured threshold, opens the circuit. In the example, five exceptions within a minute cause the circuit to break for one minute. During that minute, requests do not go to the third-party joke API at all.

Anna
Instead, the circuit-breaker fallback returns a dummy joke immediately. This fallback specifically handles a broken-circuit exception, and its source identifies that the circuit breaker produced the response.

Michael
After the one-minute break period, the circuit becomes available again and the application can try the endpoint. The key benefit is that a known failure does not keep consuming resources or adding load to the already unhealthy third-party API.

Anna
There is also an implementation detail behind that behavior: a circuit breaker needs state. The example moves the policy wrapper into a class field and constructs the policies with the joke client, rather than recreating them for every request. That allows the breaker to remember its failure state across calls.

Michael
So the patterns build on one another. Timeouts stop requests from waiting indefinitely. Retries give temporary failures another chance. Exponential backoff and jitter make those retries less aggressive and less synchronized. Fallbacks provide a usable response, while the circuit breaker stops sending traffic when continued attempts are clearly unproductive.

Anna
The final topic is not another pattern but a practice: chaos engineering. During the demonstration, random failures were injected into the third-party joke API so the resilience policies could be observed under stress.

Michael
Chaos engineering is more than simply breaking a service. It also means observing how the system behaves under that stress and using what you learn to improve its resilience and performance. The goal is to uncover hidden issues and expose blind spots—the things you did not know your system was vulnerable to.

Anna
Traditionally, teams might need to stitch together scripts and tools, install agents, or work with complex libraries to create those failures. It is also difficult to reproduce realistic events in a safe environment, because injecting chaos directly into production can be risky.

Michael
The talk demonstrates AWS Fault Injection Simulator as a way to run controlled experiments. Suppose the joke API is distributed across several EC2 instances. An experiment can use a stop-instances action, target instances tagged as API servers, and stop them for a defined period—in this case, one minute.

Anna
The targeting matters because the experiment is directed at specific resources rather than indiscriminately affecting everything. The simulator shows warnings because this is a real operation: it actually stops the selected instances. Once the experiment runs, the team can test how the application behaves while those servers are unavailable.

Michael
The simulator can support other actions as well, including actions involving containers. It can also be configured with specific time spans, making it possible to test a service during a chosen period. The important practice is to run a controlled failure, observe the system, and use the results to strengthen it.

Anna
Let's recap the full resilience toolkit. Use timeouts to bound waiting, graceful degradation to return a backup response, retries for potentially temporary failures, exponential backoff to space out retries, jitter to reduce retry clustering, and circuit breakers to stop calling a dependency that is repeatedly failing.

Michael
Then use chaos engineering to verify those protections in realistic failure scenarios. The broader lesson is not that failures can be eliminated. It is that distributed applications should be designed with failure in mind, so when a component eventually goes down, the application can recover and continue serving its users.

Ask anything
We've read this whole study set, so questions about it get the best answers.