Sub topic 10
Making Degraded Behavior Visible to Operators and Users
This chapter assumes you have read the earlier chapters on fallbacks and circuit breakers. You should be comfortable with Python exceptions, HTTP responses, and the idea of a metric. A fallback that nobody can see is a hidden failure. The service keeps answering, so dashboards stay green, while users get stale or reduced results for hours. This chapter shows how to expose degraded state so people can notice it and act on it.
Why silent fallbacks are dangerous
A fallback replaces a failure with a plausible answer. That is its purpose, and it is also the risk. If a cached recommendation list is served for a week because the recommendation service is down, nothing crashes and no alert fires. Revenue may drop with no visible cause.
Three audiences need to know about degradation:
- Operators need to know that it is happening, how much of the traffic it affects, and which dependency caused it.
- Calling services need a machine-readable signal so they can adapt, for example by skipping their own optional work.
- End users need an honest, short message when the reduced result changes what they can do.
Mark every response
The first code example in this tutorial returned {"items": ..., "degraded": True, "source": "cache"}. That flag is the foundation. Keep the same shape across the whole service: a boolean, a source, and ideally the name of the failed dependency. Do not leave it to each handler to invent its own convention.
For HTTP APIs, put the signal where clients can read it without parsing the body. A custom header works well, and so does a Warning-style note in the JSON body.
def build_response(result):
body = {
"items": result["items"],
"degraded": result["degraded"],
}
headers = {}
if result["degraded"]:
headers["X-Degraded"] = "recommendations"
headers["X-Degraded-Source"] = result.get("source", "unknown")
return 200, headers, body
status, headers, body = build_response(
{"items": [], "degraded": True, "source": "default"}
)
print(status, headers, body)The status stays 200 because the request did succeed in a reduced form. Use 503 only when the endpoint cannot produce a useful answer at all.
Metrics: count the fallbacks
Operators need numbers. At minimum, track these for each dependency:
| Metric | Type | What it tells you |
|---|---|---|
| `fallback_total{dependency, kind}` | Counter | How often a fallback ran, and whether it was cache or default |
| `circuit_state{dependency}` | Gauge | Whether the breaker is closed, open, or half-open |
| `dependency_latency_seconds{dependency}` | Histogram | Whether slowness started before the failures |
| `cache_age_seconds{dependency}` | Gauge or histogram | How stale the cached answers being served are |
Alert on the fallback rate, not only on errors. A useful rule is "more than 5% of requests used a fallback for 5 minutes". Also alert on the breaker staying open for a long time, because a breaker that never closes means a dependency that never recovered.
from collections import Counter
fallback_total = Counter()
def record_fallback(dependency, kind):
fallback_total[(dependency, kind)] += 1
record_fallback("recommendations", "cache")
record_fallback("recommendations", "cache")
record_fallback("recommendations", "default")
print(dict(fallback_total))In production you would use a metrics library such as Prometheus or OpenTelemetry instead of a plain Counter. Keep label values to a small fixed set. Putting a user ID in a label creates millions of time series and can take down your metrics system.
Logs: record state changes, not every call
Logging every fallback during an outage floods storage and hides the useful lines. Log transitions instead: the breaker opened, the breaker closed, the cache age passed a limit. Include the dependency name, the reason, and the counts.
import logging
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
log = logging.getLogger("degradation")
def on_breaker_open(dependency, failures):
log.warning(
"breaker_open dependency=%s consecutive_failures=%d",
dependency,
failures,
)
on_breaker_open("recommendations", 3)Attach a request or trace ID to per-request logs so a support engineer can follow one affected request through the system.
Telling users
Not every degradation needs a message. If a missing "recommended for you" row is replaced by a generic popular list, a user will likely not care. Tell users when the reduced behavior could mislead them or change a decision:
- Prices, balances, or stock levels that may be out of date.
- A feature that is temporarily unavailable, such as order tracking.
- Actions that are accepted but delayed, such as an email queued for later.
Write the message in plain terms. Say what is limited, and what the user can do, if anything. "Order tracking is temporarily unavailable. Your order is not affected." is better than "Error 503: upstream failure".
How the signals fit together
Common mistakes
- Reporting the fallback as a success in metrics, so the success rate hides the degradation.
- Setting a flag in the body only on some code paths, so clients cannot rely on it.
- Serving cached data with no age limit and no signal about how old it is.
- Alerting on every single fallback, which trains the team to ignore the alert.
- Forgetting to signal recovery, so users keep seeing a warning after the dependency is healthy.
Exercise
A service returns cached prices when its pricing dependency times out. Name two signals you would add so operators notice the problem, and one signal for the user. Why is returning HTTP 200 without any flag a problem?
Show answer
Operator signals: a fallback_total{dependency="pricing", kind="cache"} counter with an alert on the fallback rate, and a cache_age_seconds metric or a breaker-open log line. User signal: a short notice such as "Prices may be a few minutes out of date." A bare 200 with no flag looks identical to a healthy response, so dashboards stay green, callers cannot adapt, and stale prices can go unnoticed for a long time.