惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX - 技术
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志
Recent Announcements
Recent Announcements
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
T
The Exploit Database - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
Cisco Talos Blog
Cisco Talos Blog
Microsoft Security Blog
Microsoft Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
Kaspersky official blog
F
Fortinet All Blogs
GbyAI
GbyAI
Forbes - Security
Forbes - Security
The Cloudflare Blog
博客园 - 司徒正美
博客园_首页
量子位
Schneier on Security
Schneier on Security
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Proofpoint News Feed
N
News | PayPal Newsroom
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
T
Tor Project blog
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
Jina AI
Jina AI
Help Net Security
Help Net Security
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
N
News and Events Feed by Topic
I
Intezer
B
Blog
美团技术团队
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 最新话题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
AWS News Blog
AWS News Blog
T
Tailwind CSS Blog
The Last Watchdog
The Last Watchdog
雷峰网
雷峰网
有赞技术团队
有赞技术团队

The Practical Developer

The Libuv Thread Pool Trap: Why Node.js Async APIs Stall Under Load Postgres Covering Indexes with INCLUDE: Eliminate Heap Fetches on Read-Heavy Workloads Postgres DISTINCT ON: The Fastest Way to Get the Latest Row Per Group Postgres Transaction Isolation: The Anomalies Your App Actually Faces in Production Linux TCP Tuning for Node.js Microservices: The Kernel Settings That Stop Silent Connection Drops Under Load Postgres HOT Updates and Fillfactor: Why Not All Writes Are Created Equal Database Connection Pool Leaks: Finding the Promise That Never Returns Its Seat Linux OOM Killer in Production: Why Your Node.js Containers Die Without a Stack Trace Postgres Materialized Views: Refresh Strategies That Do Not Lock Your Dashboards API Dependency Health Checks: Why /health Is Not Enough Authorization with Zanzibar Tuples: How Google Manages Permissions and How To Build the Same Check in Node.js Postgres Advisory Locks: The 20-Character Primitive That Replaces Redis for Coordination Dead Letter Queues: The Message Queue Pattern That Saves You at 2 a.m. File Descriptor Exhaustion: The Kernel Limit That Silently Drops Node.js Connections PostgreSQL Full-Text Search: Dropping Elasticsearch for 90% of Use Cases S3 Presigned Multipart Uploads: Stop Your API Server from Being a File Upload Bottleneck MessagePack vs JSON: The Binary Serialization Switch That Cut Our Internal RPC Overhead by 40% DNS Caching in Node.js: The Silent Cause of Production Latency Spikes Reliable Cron Jobs: The Pattern That Stops Double Runs, Missed Executions, And The 2 AM Page GraphQL Query Complexity: Stop the OOM Query Before It Reaches Your Resolver Node.js Event Loop Lag: The Hidden Metric Behind Random Latency Spikes API Request Validation with Zod: The Schema That Catches Bad Input Before It Corrupts Your Database Load Shedding in Node.js: How to Reject Traffic Before You Drown Request Hedging: Cut Tail Latency In Half Without Overprovisioning Git Bisect: The Automated Binary Search That Finds Breaking Commits in Minutes Node.js Garbage Collection Tuning: Stop Letting V8 Pause Your Event Loop Node.js Server Timeouts: The Settings That Stop Slow Clients from Holding Sockets Hostage Postgres BRIN Indexes: The Time-Series Secret That Shrinks Indexes by 99% Event Sourcing with PostgreSQL: The Pragmatic 80% Solution Node.js Cluster Mode: Scaling the Event Loop Across CPU Cores Postgres Partial Indexes: Stopping Soft Deletes from Ruining Your Query Performance Request Coalescing with the Singleflight Pattern: Stop Drowning Your Database on Every Cache Miss The Bulkhead Pattern: Why One Slow Endpoint Should Not Drown Your Whole Service Node.js AsyncLocalStorage: End-to-End Request Context Without the Propagation Hell Postgres Deadlocks: Logging the Victim, Reproducing the Race, and Fixing the Lock Order Your Node.js HTTP Client Is the Bottleneck: Connection Pool Tuning That Works Optimistic Locking in Postgres: Stop Losing Data to Race Conditions Postgres Read Replicas: Stop Serving Stale Data to Your Users Cursor Pagination: Why Offset Queries Explode at Scale and How to Fix Them Node.js Worker Threads: 60 Lines That Stop a CSV Upload from Timing Out Every Other Request Reliable Webhook Delivery: Architecture for Outbound HTTP You Can Trust Request Timeouts and Deadline Propagation: Stop the Chain of Slowness Advanced Security Practices in Node.js Graceful Shutdown in Node.js: The 40 Lines That Stop 502s During Deploys Finding Node.js Memory Leaks with Heap Snapshots Idempotency Keys in 30 Lines: Stop Your Webhook From Charging Customers Twice Backpressure In Node.js: The Fix For Slow-Motion Queue Meltdowns Retries Done Right: Jitter, Budgets, and the Stampede You Did Not See Coming The Cache Stampede: Why Your "Just Add Redis" Layer Crashes Postgres at 3 a.m. Postgres SKIP LOCKED: An 80-Line Job Queue You Can Run Without Redis Stop Doing Work Nobody Wants: AbortController in Node.js, Done Right The N+1 Query Problem: We Found 23 In One Codebase And Killed Every One I Tried 5 AI Coding Tools for a Month. Here Is What I Actually Use CI/CD From Zero to Production in 30 Minutes With GitHub Actions Node.js vs Bun vs Deno: Which Runtime Should You Pick in 2025? Kubernetes Resource Requests And Limits: The Numbers That Decide If Your Cluster Is Stable The Three Pillars of Observability Are A Myth: What Actually Matters In Production pnpm Vs npm Vs yarn Vs Bun For Monorepos: Which One Earns The Migration In 2024 JSONB Indexing In Postgres: GIN Vs Expression Indexes, And When Each Is The Right Choice A Code Review Checklist That Ends The Same Three Arguments Every Sprint gRPC Vs REST In 2024: When The Switch Pays For Itself React Suspense For Data Fetching: The Pattern That Replaces Half Your Loading State Code The Five-Stage Rollout: How To Ship A Risky Change Without Holding Your Breath GitHub Actions In A Monorepo: Caching, Path Filters, And Secret Boundaries That Actually Work The Blameless Postmortem That Actually Improves Things: A Template And Six Hard-Won Rules Recursive CTEs In Postgres: How To Query A Tree Without N Round Trips Node.js Streams: When They Actually Help, And When They Just Add Complexity Playwright Vs Cypress In 2024: The Honest Comparison Of Which One Earns The Test Time React Server Components: The Mental Model That Makes The "use client" Boundary Obvious Pod Disruption Budgets: The K8s Object That Keeps Your Service Up During Cluster Maintenance Postgres LISTEN/NOTIFY: The Pub/Sub You Already Have And Are Not Using Chaos Engineering Starter Kit: The Five Drills That Don't Need Netflix-Scale Spec-Driven API Development With OpenAPI: How To Stop Drifting From Your Docs Saga Pattern vs Two-Phase Commit: Distributed Transactions Without The Lies Kubernetes Autoscaling Beyond CPU: The Custom-Metric HPA Pattern That Actually Works Postgres Partitioning For Time-Series: The Boring Setup That Saves Your Database Distributed Locks With Redis: An Honest Look At Redlock And When You Don't Need It HTTP/2 vs HTTP/3: What Actually Changes For Your App, And What Doesn't Image Optimization For The Web In 2023: srcset, AVIF, And The Lighthouse Score You Actually Want Kafka vs RabbitMQ: A Decision Tree That Doesn't Hate You UUID vs Bigint Primary Keys In Postgres: The Index Math That Decides For You Flame Graphs: How To Find The Slow Function In 30 Seconds Without Profiling Theatre Postgres Streaming Vs. Logical Replication: Which One Solves Your Actual Problem ESLint Rules That Earn Their Keep: The Twelve I Enable On Every Project Pre-Commit Hooks That Pay For Themselves: Husky, lint-staged, And The Five Rules That Stick Zero-Downtime Database Migrations: The Six-Step Pattern That Rules Them All Circuit Breakers In Node.js: 50 Lines That Stop A Failing Dependency From Taking Down Your Service Postgres VACUUM Is Not Magic: How Your Hot Table Bloats To 80GB And How To Fix It Kubernetes Liveness And Readiness Probes: The Difference That Causes Half Your Outages Rate Limiting In Production: A Token Bucket In 30 Lines Of Redis The Outbox Pattern: How To Stop Losing Events When Postgres And Kafka Disagree Load Testing With k6: The Three Scenarios That Find Real Bugs (Not Synthetic Numbers) Postgres Row-Level Security For Multi-Tenant Apps: The Pattern That Stops You From Leaking Data Rebase vs. Merge: The Team Policy That Ends The Argument Forever OpenTelemetry in Node.js: Distributed Tracing That Actually Helps During an Incident Feature Flags That Pay Rent: The 4 Flag Types And When To Delete Each ETag, Last-Modified, and the Caching Headers Most APIs Get Wrong Connection Pooling Without the Cargo Cult: pgbouncer in 100 Lines of Config JSONB Is Not a Schema: When To Reach For It in Postgres, And When To Stop Bash Strict Mode: The Three Lines That Stop Your Deploy Script From Lying To You
Graceful Degradation: The Pattern That Turns Total Outages into Partial Success
The Practica · 2026-05-23 · via The Practical Developer

Black Friday. The recommendation service, powered by a Python ML model, OOMs under load. The Node.js API gateway calls it to populate “You might also like” on the product page. The call throws. The gateway does not catch it. The entire product page route returns 500. The checkout flow is fine, but 70% of users discover products through recommendations. Revenue drops 40% for two hours until the ML team scales the service.

The recommendation engine was 8% of the page payload. The other 92% (product details, pricing, inventory, reviews) was healthy. But the architecture was all-or-nothing: if any dependency failed, the route failed. That is not a dependency problem. It is a composition problem.

Graceful degradation is the decision to serve a partial success instead of a total failure. It is not a circuit breaker (which stops calling the broken service) and it is not load shedding (which rejects the request). It is the code that says: “The recommendations are unavailable, so show the top-10 bestsellers from yesterday’s cache instead.” The user still sees a product page. The business still makes money. The on-call engineer fixes the ML service in the morning instead of at midnight.

This post is the degradation pattern: the fallback hierarchy, the wrapper code that makes it automatic, the cache strategy that keeps stale data ready, and the monitoring that tells you when you are running degraded.

The fallback hierarchy

Not all failures deserve the same response. A four-level hierarchy keeps your decisions consistent:

Level 1: Stale cache. If the dependency fails, return the last cached response even if it is expired. For recommendations, yesterday’s bestsellers are better than nothing.

Level 2: Pre-computed defaults. Maintain a static fallback for critical paths. A weather app might show “conditions unavailable” but still display the 7-day forecast from the last successful sync. An e-commerce site might show “trending now” instead of personalized recommendations.

Level 3: Simplified response. Omit the failed section entirely but return HTTP 200 with the rest of the payload. The mobile app renders the product page without the “similar items” carousel. This requires frontend discipline: every optional section must handle absence.

Level 4: Empty but valid. Return an empty array, a null field, or a placeholder object that satisfies the schema. The client knows something is missing but the page does not crash.

The rule: never let a Level 1-4 fallback propagate as a 500. A 500 means “I have no idea what happened.” A 200 with degraded data means “I am operational, but this specific feature is limited.”

The degradation wrapper

The implementation is a wrapper around service calls. It enforces a timeout, catches errors, and routes to the fallback. Here is the TypeScript version we use in production:

// degradation.ts
import { setTimeout } from 'node:timers/promises';

type DegradationLevel = 'stale' | 'default' | 'simplified' | 'empty';

interface DegradeOptions<T> {
  name: string;
  timeoutMs: number;
  fallback: T;
  staleCache?: () => Promise<T | undefined>;
  onDegraded?: (level: DegradationLevel, err: unknown) => void;
}

export async function withDegradation<T>(
  operation: () => Promise<T>,
  options: DegradeOptions<T>,
): Promise<T> {
  const { name, timeoutMs, fallback, staleCache, onDegraded } = options;

  try {
    const result = await Promise.race([
      operation(),
      setTimeout(timeoutMs, Symbol('timeout')),
    ]);

    if (result === Symbol('timeout')) {
      throw new Error(`${name} timed out after ${timeoutMs}ms`);
    }

    return result as T;
  } catch (err) {
    // Level 1: try stale cache.
    if (staleCache) {
      try {
        const cached = await staleCache();
        if (cached !== undefined) {
          onDegraded?.('stale', err);
          return cached;
        }
      } catch {}
    }

    // Level 4: empty but valid fallback.
    onDegraded?.('empty', err);
    return fallback;
  }
}

Usage in an Express route:

import { Router } from 'express';
import { withDegradation } from './degradation.js';
import { getRecommendations } from './recommendations.js';
import { redis } from './redis.js';

const router = Router();

router.get('/api/products/:id', async (req, res) => {
  const [product, recommendations] = await Promise.all([
    getProduct(req.params.id),
    withDegradation(
      () => getRecommendations(req.params.id),
      {
        name: 'recommendations',
        timeoutMs: 200,
        fallback: [],
        staleCache: async () => {
          const cached = await redis.get(`recs:${req.params.id}`);
          return cached ? JSON.parse(cached) : undefined;
        },
        onDegraded: (level, err) => {
          console.log(JSON.stringify({
            event: 'degradation',
            service: 'recommendations',
            level,
            productId: req.params.id,
            error: (err as Error).message,
          }));
        },
      },
    ),
  ]);

  res.json({ product, recommendations });
});

The timeout is aggressive: 200ms. If the ML service is healthy, it responds in 30ms. If it is slow, we do not wait. We degrade immediately. The user gets the product page in 220ms instead of 30 seconds.

Three design decisions in this wrapper matter:

1. The timeout is a business decision, not a technical one. 200ms means “recommendations are nice, but they are not worth delaying the page.” For inventory data, the timeout might be 2 seconds because “out of stock” is business-critical.

2. The fallback is typed. It returns the same shape as the success case. The route handler does not need an if (recommendations === null) branch. The frontend receives an empty array and renders nothing.

3. Degradation is logged as a first-class event. Not an error. An error implies someone did something wrong. Degradation implies the system is adapting. You want separate metrics for each.

Stale-while-error caching

The wrapper above uses Redis for stale cache, but you need a specific caching strategy: write-through with a long TTL, and on failure, read the stale value regardless of expiration.

// cache.ts
import { redis } from './redis.js';

export async function getStaleOrFresh<T>(
  key: string,
  fetcher: () => Promise<T>,
  ttlSeconds: number,
): Promise<T> {
  const cached = await redis.get(key);

  if (cached) {
    const parsed = JSON.parse(cached) as T;
    // If the TTL is still valid, return immediately.
    const ttl = await redis.ttl(key);
    if (ttl > 0) return parsed;
    // TTL expired: return stale, but trigger background refresh.
    fetcher().then(fresh => {
      redis.setex(key, ttlSeconds, JSON.stringify(fresh));
    }).catch(() => {});
    return parsed;
  }

  // Cold cache: fetch and store.
  const fresh = await fetcher();
  await redis.setex(key, ttlSeconds, JSON.stringify(fresh));
  return fresh;
}

This is stale-while-revalidate with a twist: on failure, the stale value is served even if it is hours old. The fetcher() promise in the background refresh is fire-and-forget. If it fails, the stale value remains. If it succeeds, the cache is warm again.

For critical fallbacks, pre-compute defaults and store them in a separate key that never expires:

await redis.set('recommendations:default', JSON.stringify(bestsellers));

Then in the wrapper:

staleCache: async () => {
  const cached = await redis.get(`recs:${id}`);
  if (cached) return JSON.parse(cached);
  const defaultRecs = await redis.get('recommendations:default');
  return defaultRecs ? JSON.parse(defaultRecs) : undefined;
},

This gives you two fallback layers: personalized stale data, then global defaults.

Feature flags for controlled degradation

Not every route should degrade the same way. Some features are too important to fake. Use feature flags to control degradation per route or per environment:

const DEGRADATION_CONFIG = {
  recommendations: { enabled: true, timeoutMs: 200, fallback: [] },
  inventory: { enabled: false, timeoutMs: 5000 }, // no fallback; fail fast
  reviews: { enabled: true, timeoutMs: 300, fallback: { count: 0, items: [] } },
  pricing: { enabled: false }, // never degrade pricing
};

export function shouldDegrade(feature: string): boolean {
  return DEGRADATION_CONFIG[feature]?.enabled ?? false;
}

In production, drive this from an environment variable or a feature flag service. During incidents, you can disable degradation for a specific feature if the fallback is causing confusion, or tighten the timeout if the dependency is flapping.

When not to degrade

Graceful degradation is not a universal virtue. There are paths where partial success is worse than total failure:

  • Payments and refunds. Never return “payment probably succeeded.” The user needs certainty. If the payment gateway is down, return 503 and let the user retry.
  • Authentication and authorization. Never degrade to “allow all” because the auth service is slow. A 500 or 503 is correct here.
  • Safety-critical operations. Medical dosing, industrial control, anything where a wrong answer hurts someone. Fail closed, not open.
  • Data mutations with side effects. If you are charging a customer and the inventory check fails, do not default to “assume in stock.” The business rule is: no charge without confirmation.

The rule: degrade reads, not writes. Degrade optional features, not core guarantees.

Monitoring degradation

You cannot manage what you do not measure. Four metrics matter:

1. Degradation rate per service.

rate(degradation_events_total[5m])

Alert when this is above zero for more than 10 minutes. Degradation is a bandage, not a cure. If you are degraded for an hour, the dependency needs fixing, not more fallback.

2. Fallback cache hit rate.

rate(fallback_cache_hits_total[5m]) / rate(fallback_cache_attempts_total[5m])

If this drops below 50%, your stale cache is empty and you are serving Level 4 (empty) fallbacks more often than Level 1. That is a data warmth problem.

3. User-facing latency during degradation.

Degradation should make responses faster, not slower. If your fallback path is slower than the primary path, you have a bug in the fallback logic (common with unoptimized default queries).

4. Revenue or engagement impact.

The ultimate metric. If the recommendation service is degraded to bestsellers, does conversion drop 2% or 20%? This tells you whether your fallback is good enough or needs better defaults.

The operational checklist

Before you declare degradation work done, verify:

  • Every optional dependency has a typed fallback that matches the success shape.
  • Timeouts are set per dependency based on business criticality, not engineering convenience.
  • Stale cache has a separate long-TTL key or a cache-aside pattern that survives primary failure.
  • Feature flags control which routes can degrade.
  • Degradation events are logged and metrics are exported.
  • Alerts fire when degradation rate is non-zero for more than 10 minutes.
  • Load tests confirm that the fallback path is faster than the timeout path.
  • Frontend and mobile clients handle missing optional fields without crashing.
  • Write paths (payments, auth, mutations) do not degrade. They fail fast.

The takeaway

A total outage is not always caused by a total failure. It is often caused by a partial failure that the architecture treats as total. One slow dependency, one missing cache key, one unhandled rejection in a non-critical service, and the entire page goes white.

Graceful degradation is the engineering decision to build fallbacks that are good enough. Not perfect. Not ideal. Good enough to keep the business running while the team fixes the root cause. It is stale recommendations instead of a 500. It is a product page without reviews instead of no product page at all. It is the difference between a blip in metrics and a revenue cliff.

Build the wrapper. Set the timeouts. Warm the fallback cache. And stop letting 8% of your page take down the other 92%.


A note from Yojji

The kind of resilient architecture that serves partial success instead of total failure during dependency outages is exactly the kind of practical engineering Yojji builds into the platforms it ships. Their senior teams specialize in designing distributed systems where fallback strategies, cache hierarchies, and timeout discipline keep services operational through the inevitable failures of real-world infrastructure.

Yojji is an international custom software development company founded in 2016, with offices in Europe, the US, and the UK. Their engineers work across the JavaScript ecosystem, cloud platforms, and event-driven microservices, building the degradation logic and operational monitoring that turn 2 a.m. outages into non-events.