How-To Guide

How to Implement Graceful Degradation with Feature Flags

Keep a service usable under stress by disabling expensive features at runtime with flags instead of failing the whole request.

On this page

  1. Introduction
  2. What Graceful Degradation Means
  3. Feature Flags as Operability Controls
  4. Fallbacks and Timeouts
  5. Degrading Versus Failing
  6. Flag Lifecycle and Cleanup
  7. Testing Degraded Paths
  8. Conclusion

Introduction

When a dependency gets slow or load spikes, failing every request is rarely the best outcome. Many features are optional: recommendations, personalised ranking, a live activity feed, an expensive search refinement. Turning them off keeps the core request working, which is more valuable to users than a perfect page that never loads. Feature flags make that switch immediate and reversible: an operator flips a flag and the expensive path is skipped, without a deploy. This article covers how to design degradation behind flags, how to keep the flags from becoming permanent, and how to test the degraded path so it works when you need it.

What Graceful Degradation Means

Graceful degradation is the practice of giving up some functionality to preserve the rest. It assumes features have a priority order and that the lowest-priority ones can be dropped under stress. A request that returns the essential content without recommendations is degraded but useful; a request that returns an error because the recommendation service is slow is a failure, even though the recommendation was optional.

The alternative, failing the whole request on any dependency error, couples every feature’s availability to the least reliable dependency. Degradation breaks that coupling. It requires knowing which features are optional, which is a product decision, and making the optional path skippable at runtime, which is what a flag provides.

Feature Flags as Operability Controls

A feature flag is a runtime switch that changes behaviour without a deploy. Used for degradation, it names a feature and its off-state is a safe fallback: return a default, skip an enrichment, or serve a cached copy. The key property is that flipping it is fast and does not require building or deploying, because degradation happens during an incident when every minute of delay is felt by users.

Evaluate flags with a safe default when the flag service itself is unavailable: if the flag system is down, the fallback state should be the safe one, not the expensive one. This is easy to get wrong: a code path that throws when it cannot fetch the flag turns a flag-service blip into a full outage. Make the default a constant in code and treat flag-service failures as returning that default.

// Degradation flags default to the SAFE state if the flag service cannot be reached.
const DEFAULTS = {
  'recommendations.enabled': true,     // expensive; can be turned off
  'search.refinements.enabled': true,  // expensive; can be turned off
};

async function flag(name) {
  try {
    const value = await flagService.get(name);
    return value === undefined ? DEFAULTS[name] : value;
  } catch {
    return DEFAULTS[name];             // fail safe, do not throw
  }
}

async function homePage(userId) {
  const base = await loadBaseContent(userId);
  if (await flag('recommendations.enabled')) {
    try {
      base.recommendations = await loadRecommendations(userId);   // optional
    } catch (err) {
      base.recommendations = [];        // degrade, do not fail
    }
  }
  return base;
}

Fallbacks and Timeouts

Degradation and timeouts are complementary. A timeout bounds how long an optional call may take; if it exceeds the bound, the fallback is used and the request continues. This keeps a slow dependency from making the whole request slow even when the flag is on. Pair every optional dependency with a shorter timeout than the core path, so the optional work cannot extend the user’s wait.

Choose the fallback deliberately. An empty list is honest for recommendations; a cached copy is better for a content feed; a default value is fine for a non-critical label. Never return a fabricated value that the user might act on, such as a fake price or a fake availability, because degrading correctness is worse than degrading richness.

Degrading Versus Failing

Define which dependencies are critical and which are optional, and encode that in the code: critical dependencies fail the request, optional ones degrade it. Making this explicit prevents accidental coupling, where an optional feature’s failure takes down the page. Review the list whenever a new dependency is added, because a new feature often brings a new critical-looking dependency that is actually optional.

Consider a tri-state rather than a boolean: full, degraded, and minimal. Under light stress, drop only the most expensive feature; under heavy stress, drop more. A single flag per feature lets operators compose the level they need, and a flag that controls a group of features lets them switch levels in one step. Keep the levels few, so operators can remember what each one does under pressure.

Flag Lifecycle and Cleanup

Flags accumulate. Each one is a branch that must be tested and understood, and a flag that stays on forever is dead weight. Distinguish operational flags, which are meant to stay and control degradation, from release flags, which should be removed once the release is settled. Tag each flag with its purpose and an owner, and review the list on a schedule to delete release flags that have served their purpose.

For operational flags, document the default state, what turning it off does, and who may flip it. A degradation flag whose effect nobody remembers is a liability during an incident. Keep the off-state safe and reversible, and record the change when a flag is flipped so the incident timeline shows why behaviour changed.

Testing Degraded Paths

The degraded path is code you rarely run, which means it is code likely to be broken. Test it explicitly: with the flag off, assert that the request succeeds and returns the fallback; with the optional dependency erroring, assert that the request still succeeds; with the flag service unavailable, assert the safe default is used. These tests are the only thing that makes the degraded path trustworthy.

Exercise degradation in a low-stakes environment periodically, the way you would rehearse a failover, so the path stays warm and the operators stay familiar. A degradation mechanism that has never been triggered in practice may reveal bugs at the worst moment. This is the same reasoning as rehearsing runbooks before an incident.

// Test the degraded path directly: flag off and dependency failing.
describe('homePage degradation', () => {
  it('serves base content when recommendations are disabled', async () => {
    setFlag('recommendations.enabled', false);
    const page = await homePage('u-1');
    expect(page.recommendations ?? []).toEqual([]);   // fallback, no error
  });

  it('serves base content when the recommendation service errors', async () => {
    setFlag('recommendations.enabled', true);
    recommendations.mockRejectedValue(new Error('upstream down'));
    const page = await homePage('u-1');
    expect(page.base).toBeDefined();
    expect(page.recommendations).toEqual([]);
  });

  it('uses the safe default when the flag service is unreachable', async () => {
    flagService.get.mockRejectedValue(new Error('flag service down'));
    const value = await flag('recommendations.enabled');
    expect(value).toBe(true);                        // documented default
  });
});

Conclusion

Graceful degradation keeps the essential request working by turning off optional features under stress, and feature flags make that switch immediate and reversible. Default flags to the safe state when the flag service is unreachable, pair optional calls with short timeouts and deliberate fallbacks, and distinguish critical from optional dependencies in code. Tag flags with a purpose and an owner, delete release flags, and test the degraded path both with the flag off and with the dependency failing. Then a slow dependency costs richness, not availability.

Sources: Martin Fowler: Feature Toggles, OpenFeature, Release It! (Nygard): stability patterns.

FAQ

What should the default flag value be when the flag service is down?
The safe state. If the flag service is unreachable, the code should take the branch that preserves the core request, not the one that depends on the expensive feature. Failing over to the safe default prevents a flag-service blip from becoming an outage.
How are degradation flags different from release flags?
Degradation flags are operational and meant to persist, controlling what the service drops under stress. Release flags expose new code and should be removed once the rollout is settled. Tag each with its purpose and an owner so release flags do not linger.

Related Examples