Course contentsShow
← System Design
Lesson 1571 of 1,91938. Real-World System: PastebinFree lesson

Cache Invalidation on Update or Delete

Purging CDN cache when pastes are deleted or modified, using purge APIs and handling eventual consistency.

Cache Invalidation on Update or Delete

What you'll learn: How to ensure CDN caches reflect the latest state when pastes are modified or deleted, using purge APIs and managing eventual consistency.


The Challenge

When you deliver pastes through a CDN (as covered in lesson 1569), the content gets cached at edge servers worldwide. But what happens when a user deletes their paste or updates it? Without action, the old version remains cached and served to users until the TTL expires—possibly for hours or days. This creates a data consistency problem.

Purging CDN Cache

Most CDNs provide purge APIs (also called invalidation APIs) that let you explicitly remove cached content:

POST /purge
{
  "urls": ["https://cdn.pastebin.com/paste/abc123"]
}

When a paste is deleted or updated in your system, trigger a purge request to the CDN. This ensures the next request fetches fresh data (or returns a 404 for deletions).

Handling Eventual Consistency

CDN purges are not instantaneous. It takes time for invalidation requests to propagate to all edge servers globally (often 5-30 seconds). During this window:

  • Some users may still see the old paste
  • You might serve stale content despite deletion

Mitigation strategies:

  • Set shorter TTLs for mutable pastes (e.g., 5 minutes instead of 24 hours)
  • Use cache-busting: append version parameters (?v=2) to URLs when content changes
  • Return Cache-Control: no-cache headers for authenticated/private pastes
  • Implement a "soft delete" grace period before actual deletion

Think of it like updating a billboard: even after you request removal, it takes time for workers to reach every location and take it down.

Key Takeaway:
Cache invalidation requires actively calling CDN purge APIs on updates/deletes, but expect eventual consistency—plan for a propagation window where stale content may briefly persist.