Sub topic 2
What graceful degradation means
This chapter assumes you have built a service that calls other services or databases over a network, and that you know how exceptions and HTTP responses work. It defines graceful degradation and separates it from two other ways a service can react to a failure. Later chapters cover the techniques in detail.
Three reactions to a failing dependency
Suppose a product page calls a recommendations service, and that service stops responding. The product page can react in three ways.
- Fail completely. The page waits on the broken call, requests pile up, and the whole site becomes slow or unavailable. One optional feature takes down everything.
- Fail fast. The page notices the problem quickly and returns an error immediately. Resources are freed, but the user still gets no page.
- Degrade gracefully. The page notices the problem quickly and returns the product details without recommendations, or with older recommendations. The user can still do what they came to do.
| Reaction | What the user sees | What happens to the service |
|---|---|---|
| Fail completely | Timeouts or a blank page | Threads and connections are held, and the failure spreads |
| Fail fast | A clear error | Resources are released, but the feature is lost |
| Degrade gracefully | A working page with reduced functionality | Resources are released, and the core function survives |
Failing fast is a building block of graceful degradation. It is not the same thing. Degradation adds a decision about what to return instead of an error.
The definition
A service degrades gracefully when a failure in one part reduces the quality or scope of the response, but the service still answers its most important requests correctly. The reduction is deliberate, bounded, and visible. It is not an accident that happens to look like success.
Three properties matter:
- Deliberate. You chose the degraded behavior in advance. You did not discover it in an incident.
- Bounded. The failure stays within a limited set of features. A broken recommendation call must not exhaust the connection pool that checkout uses.
- Honest. The response never pretends to be complete when it is not. Callers and operators can tell that something was reduced.
A first example
The function below fetches recommendations. If the call fails, it returns the last cached result. If there is no cached result, it returns an empty list. In both cases it marks the response as degraded.
import time
_cache = {}
def get_recommendations(user_id, fetch, timeout=0.5):
"""Return recommendations, falling back to cached or default data."""
try:
items = fetch(user_id, timeout=timeout)
_cache[user_id] = (items, time.monotonic())
return {"items": items, "degraded": False}
except (TimeoutError, ConnectionError):
if user_id in _cache:
items, _ = _cache[user_id]
return {"items": items, "degraded": True, "source": "cache"}
return {"items": [], "degraded": True, "source": "default"}
def healthy_fetch(user_id, timeout):
return ["laptop stand", "usb hub"]
def broken_fetch(user_id, timeout):
raise TimeoutError("recommendations service did not answer")
print(get_recommendations(1, healthy_fetch))
print(get_recommendations(1, broken_fetch))
print(get_recommendations(2, broken_fetch))Running it prints three results:
{'items': ['laptop stand', 'usb hub'], 'degraded': False}
{'items': ['laptop stand', 'usb hub'], 'degraded': True, 'source': 'cache'}
{'items': [], 'degraded': True, 'source': 'default'}The first call succeeds and fills the cache. The second fails for the same user, so the function serves the cached items. The third fails for a user with no cache entry, so it returns the default. The caller always gets a dictionary and never an exception. The degraded flag and source field keep the response honest.
What degradation is not
Several common mistakes look like degradation but are not.
- Swallowing errors. Catching every exception and returning an empty result with no signal hides the problem. Operators see healthy dashboards while users get wrong answers.
- Silently wrong data. Serving a stale price on a checkout page is not a harmless reduction. For some data, an error is the correct behavior.
- Unbounded fallbacks. A fallback that calls another slow system moves the failure instead of containing it.
- Untested paths. A fallback that has never run in production conditions often fails the first time it is needed.
Deciding how much to degrade
Not every feature should degrade the same way. Showing an empty recommendations panel is acceptable. Charging a customer without confirming payment is not. The next chapter covers how to classify dependencies as critical or optional. For now, remember that the degraded behavior is a product decision as well as an engineering one. Engineers can build the mechanism, but someone must decide what "reduced but acceptable" means for each feature.
Exercise
A checkout service calls a loyalty-points service to show "You will earn 120 points". The loyalty service times out. One engineer proposes returning an HTTP 500 for the whole checkout. Another proposes omitting the points line and completing the order, with a metric that counts the omissions. Which reaction is graceful degradation, and what makes it graceful?
Show answer
The second proposal is graceful degradation. The core function (completing the order) keeps working, only the optional points message is dropped, the reduction was chosen in advance, and the metric makes it visible to operators. The first proposal fails fast, which frees resources but turns an optional feature failure into a lost sale.