Edge Caching Deep Dive: Cache Keys, Purge Strategies, and Stale-While-Revalidate

Team 6 min read

#edge-caching

#cdn

#webperf

Introduction

Edge caching sits at the edge of a content delivery network, serving content from the closest node to the user. When designed well, it dramatically reduces origin load, lowers latency, and improves resilience. This post dives into three核心 aspects of effective edge caching: cache keys (how we identify what to store and reuse), purge strategies (how and when we invalidate stale content), and stale-while-revalidate (SWR) techniques that let users stay fast while caches refresh in the background.

Cache Keys: What to Key On and Why

Cache keys determine what value is stored and retrieved from the edge. A well-designed key balances cache hit potential with correctness.

Key design principles

  • Stability vs. specificity: Use stable parts of the request (path, method) and be careful with parameters that vary per user.
  • Normalize inputs: Decide whether to include query strings, headers, or cookies in the key. Each choice changes cache granularity.
  • Versioning: If you deploy content updates, consider key versioning to avoid serving stale assets unintentionally.
  • Vary by content type: HTML, JSON, and images often require different key strategies; what’s appropriate for one may break caching for another.

Common patterns

  • Public content: Use path-based keys with optional normalized query segments. Example: host + path, optionally including essential query parameters only if they affect content.
  • Asset caching: Include content hashes in asset URLs (e.g., /static/app.abc123.js) so a new hash creates a new key automatically.
  • Personalization: Do not share personalized responses via the same cache entry. Keep private content behind private caches or bypass caching for such requests.
  • Headers and language: If content varies by Accept-Language or specific headers, decide whether to include those in the key or to create separate caches per variation.

Practical considerations

  • Hash long keys: If your key grows long due to many headers or query params, consider hashing the key to a fixed length while storing the mapping to the real parameters in a separate manifest.
  • Be cautious with cookies: Cookies often cause cache fragmentation. Prefer cookie-free paths for public assets and isolate personalization behind tailored responses or edge logic.
  • Clear mapping when origin changes: If the origin content changes in a way that would alter the response, ensure a consistent path to purge or invalidate old keys.

Purge Strategies: When and How to Invalidate

Purging ensures stale content does not linger beyond its useful life. Different strategies suit different content types and workloads.

Types of purges

  • Event-driven purge: Trigger purges when content changes (deploys, CMS updates, asset revisions). This minimizes serving stale content.
  • Pattern-based purge: Invalidate groups of related keys (e.g., /blog/, /assets/) rather than individual URLs.
  • Time-based purges: Regularly prune content that has TTL-based expiration, ensuring old entries do not linger indefinitely.
  • On-demand purge: Expose a secure API to purge specific URLs as needed (e.g., after a critical hotfix).

Best practices

  • Prefer purge+rehydration: Purge stale entries and optionally warm the cache with fresh content to reduce cold-start latency.
  • Use cache versioning: When content changes, increment a version in the URL or a cache key prefix to automatically separate new and old content.
  • Separate purge domains: If possible, isolate purges to a specific edge zone or cache layer to avoid broad purges that affect performance globally.
  • Think in tags, not just URLs: If your CDN supports tag-based purges, associate related assets with a purge tag for efficient invalidation.

Operational tips

  • Instrument purge events: Track purge counts, hit rates after purges, and any collateral cache misses caused by broad purges.
  • Validate post-purge behavior: After a purge, ensure the next user request fetches fresh content and that SWR windows behave as expected.
  • Pre-warm after purge: If feasible, proactively fetch and cache the updated content to reduce perceived latency.

Stale-While-Revalidate: Rationale and Config

Stale-While-Revalidate (SWR) lets edge caches serve stale content while revalidating in the background, delivering fast responses without compromising eventual correctness.

How SWR works

  • A request is served from cache even if the entry is technically stale.
  • In parallel, the cache fetches fresh content from the origin and updates the cache for subsequent requests.
  • The user sees the cached (stale) content during the SWR window, avoiding a full rebuild of the response.

Common Cache-Control patterns

  • Cache-Control: public, max-age=300, s-maxage=600, stale-while-revalidate=120
    • max-age affects browser caches.
    • s-maxage governs shared caches (CDNs, reverse proxies).
    • stale-while-revalidate specifies how long a stale response can be served while a revalidation occurs in the background.
  • For dynamic content, carefully balance stale content risk with user-perceived performance. Shorter max-age and a reasonable SWR window often work well.

When to use SWR

  • Public, read-mostly content that updates intermittently.
  • Content where serving slightly stale data is acceptable to maintain low latency.
  • Scenarios where revalidation can be parallelized without risking inconsistency at the edge.

Common pitfalls

  • SWR does not remove the need for correctness; ensure revalidated content is actually refreshed and not stale indefinitely.
  • Be cautious with personalization: SWR can leak personalized data if not properly isolated from shared caches.
  • Monitor SWR windows to prevent serving excessively stale content in fast-changing sections of your site.

Practical Patterns: Designing for Real-World Apps

  • Versioned URLs for assets: Use content hashes in asset paths to create a natural cache key version.
  • Separate caches for HTML and assets: Different TTLs and purge strategies reduce blast radius when content changes.
  • Vary-key strategy: For multilingual sites, create separate cache entries per language, rather than mixing languages in a single key.
  • Personalization boundaries: Keep user-specific content out of shared edge caches or use private caches with appropriate privacy controls.
  • Observability-first approach: Instrument cache-hit ratios, purge counts, SWR effectiveness, and latency reductions to continuously optimize.

Testing and Observability

  • Validate cache keys: Ensure that different request variations produce distinct keys and expected cache hits/misses.
  • Purge verification: After a purge, verify that subsequent requests fetch fresh content and that the SWR window behaves correctly.
  • Measure performance impact: Compare response times and origin fetches before and after applying cache-key refinements and purge strategies.
  • Validate SWR behavior: Confirm that stale content is served during the SWR window and that the cache updates promptly afterward.
  • Use synthetic and real-user monitoring: Combine controlled tests with real-user data to gauge real-world effectiveness.

Implementation Notes: Platform-Neutral Tips

  • Cloud/CDN configuration: Most CDNs allow you to customize cache keys (which headers, query parameters, or cookies to forward) and define TTLs. Use these settings to align with your chosen key strategy.
  • Purge APIs: If supported, leverage programmatic purge endpoints to invalidate content as part of your deployment or CMS events.
  • Cache-Control pragmatics: Use appropriate combinations of max-age, s-maxage, and stale-while-revalidate to balance browser and edge caching behavior.
  • Security considerations: Avoid exposing sensitive personalization in edge caches. Use header-based or cookie-based bypass for protected content.

Conclusion

Edge caching, when paired with thoughtful cache key design, disciplined purge strategies, and practical SWR usage, can deliver dramatic performance gains without sacrificing correctness. Start by mapping how your content updates, how users access it, and where you can safely tolerate stale data. Use that map to craft robust keys, targeted purge rules, and SWR configurations that keep content fresh and responses fast across the globe.