Architecture

Graceful Degradation: Keep Your Service Useful When Parts of It Fail

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

Every real system depends on things that eventually fail: a payment gateway, a search engine, a recommendation service, an external API, or simply a database that is overloaded for a few minutes. The question is not whether a dependency will break, but what your users see when it does. A brittle system shows a blank error page because one widget could not load. A system designed for graceful degradation keeps the core working, switches off or simplifies the parts that depend on the broken component, and tells the user honestly what is limited. This article walks through how to plan for that and the concrete techniques that make it work.

Start by ranking your features

You cannot degrade gracefully without deciding in advance what matters most. Sit down with the team and sort every feature into tiers:

TierMeaningOnline store example
CriticalWithout it the service has no purposeBrowse products, add to cart, checkout
ImportantUsers notice quickly, but can live without it brieflySearch, order history, stock levels
Nice to haveCan disappear with little complaintRecommendations, reviews widget, live chat

The rule that follows is simple: a failure in a lower tier must never take down a higher one. A recommendation service timing out should hide the "You may also like" box, not break the product page.

Core techniques

Timeouts on every external call

The most common way a small failure becomes a big outage is waiting forever. If a dependency hangs and your code has no timeout, worker processes pile up waiting until none remain to serve anyone. Set explicit, short timeouts for HTTP clients, database queries and cache lookups. A recommendation call might get 300 milliseconds; a payment call might get several seconds because it is critical and slower by nature.

Fallbacks for each dependency

Decide what to show when each call fails:

  • Cached or default data. Show last known prices or a static list of popular items instead of personalised ones.
  • Hide the component. Render the page without the reviews section rather than with an error inside it.
  • Simplified behaviour. If full-text search is down, fall back to a basic database LIKE query on product names, accepting worse results.
  • Queue for later. If the email provider is down, store the message and send it when it recovers, while confirming the order immediately.

Serve stale content from Nginx

When Nginx sits in front of your app as a caching reverse proxy, it can serve an expired copy of a page while the backend is failing:

proxy_cache_path /var/cache/nginx keys_zone=app_cache:10m max_size=1g inactive=24h;

server {
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_cache app_cache;
        proxy_cache_valid 200 5m;
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        proxy_cache_background_update on;
        proxy_cache_lock on;
    }
}

With this, public pages that were cached at least once keep loading during a backend outage. Only use it for content that is safe to share between users; never cache pages containing personal data or session-specific output.

Read-only mode

If your primary database is struggling or under maintenance, a read-only mode lets people keep browsing and reading while writes are paused with a clear message. Many frameworks make this easy with a configuration flag checked in middleware. It is far better than a full maintenance page for content sites, catalogues and dashboards.

Feature flags as kill switches

Wrap expensive or fragile features in flags that can be turned off without a deploy. During an incident, switching off a heavy report or a costly search filter can free enough capacity to keep the critical path healthy.

Shed load before collapsing

Under extreme traffic, it is better to reject some requests quickly with 503 Service Unavailable and a Retry-After header than to accept everything and time out for everyone. Nginx limit_req and limit_conn are simple tools for this. Our article on admission control explains the idea in depth.

Communicate the degraded state

Silent degradation confuses users and support staff. Show a short, honest banner such as "Search is temporarily limited; browsing and checkout work normally". Log every fallback activation and expose it in your monitoring, so you know the system is running degraded rather than discovering it from complaints.

Test it before you need it

Graceful degradation that has never been tested usually does not work. On a staging server, try these drills:

  1. Block an external API with the firewall, for example sudo ufw deny out to 203.0.113.10, and confirm the page still renders.
  2. Stop the cache service with sudo systemctl stop redis-server and check the app falls back to the database instead of crashing.
  3. Add artificial latency with tc qdisc add dev eth0 root netem delay 2000ms (remove it with tc qdisc del dev eth0 root) and verify your timeouts fire. Run this only on a test machine you can reach through a console, since it slows your SSH session too.

A browser console, such as the one included with NileVPS servers, is useful here because you keep access even if a network experiment goes wrong. Take a snapshot before experimenting so you can roll back in minutes.

Graceful degradation works best alongside the circuit breaker pattern, which stops calling a failing dependency so fallbacks kick in immediately instead of after every timeout.

FAQ

What is the difference between graceful degradation and fault tolerance?

Fault tolerance aims to hide a failure completely, usually through redundancy, so users notice nothing. Graceful degradation accepts that some functionality is lost and focuses on keeping the most important parts working. Most systems use both: redundancy where it is affordable and degradation for everything else.

Is serving stale cache content risky?

It is safe for public content such as articles, product pages and listings, where a few minutes old is acceptable. It is risky for personal or transactional data like account pages, carts and balances. Exclude those paths from caching, or bypass the cache when a session cookie is present.

How short should my timeouts be?

Base them on real measurements. Look at the normal response time of each dependency, then set the timeout comfortably above its slow end but far below the point where your workers would pile up. Non-critical features deserve aggressive timeouts; critical ones get more patience plus a clear error path.

Related guides