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

推荐订阅源

D
Docker
Apple Machine Learning Research
Apple Machine Learning Research
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
M
MIT News - Artificial intelligence
腾讯CDC
B
Blog RSS Feed
H
Help Net Security
J
Java Code Geeks
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
博客园_首页
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
博客园 - Franky
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 叶小钗
Martin Fowler
Martin Fowler

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Edge Caching Strategies: From CDN to Workers Cache for Gl...
sweet · 2026-06-11 · via DEV Community

sweet

Caching is the single most impactful performance optimization for SaaS applications — it reduces latency, lowers origin load, and improves user experience. Cloudflare provides a multi-layered caching stack (CDN, Workers Cache API, KV, D1 query caching, Cache Reserve) that can serve content from edge locations within 30ms globally. This guide covers caching strategies at each layer, with practical patterns used at tanstackship.com.


The Caching Stack

Cloudflare's caching infrastructure has multiple layers, each with different characteristics:

Layer Latency Storage Data Freshness Use Case
CDN Cache ~5ms (cache hit) Configurable TTL-based Static assets, HTML
Workers Cache API ~1ms (in-memory) 128MB per Worker Manual API responses, dynamic content
Workers KV ~10ms (global) Unlimited Eventually consistent Configuration, feature flags
D1 Query Cache ~2ms (prepared) Database-level Per-query Database query results
Cache Reserve ~10ms Unlimited persistent TTL + LRU Large objects, images

Layer 1: CDN Caching

Default Edge Caching

// wrangler.jsonc — configure caching rules
{
  "name": "tanstack-ship",
  "routes": [{ "pattern": "tanstackship.com/*", "zone_id": "YOUR_ZONE" }],
  "assets": {
    "bucket": "./dist/public",
    "browser_ttl": 31536000, // 1 year for assets
    "serve_single_page_app": false
  }
}

Cache-Control Headers

// Set cache-control headers from server functions
export const getPublicProduct = createServerFn({ method: "GET" }).handler(
  async ({ data, context }) => {
    const product = await context.env.DB.prepare(
      "SELECT * FROM products WHERE id = ? AND published = 1"
    ).bind(data.id).first()

    // Cache public product data for 5 minutes on the edge
    context.response.headers.set("Cache-Control", "public, s-maxage=300, max-age=60")

    return product
  }
)

Cache Directive Purpose Value
s-maxage Edge cache TTL (CDN) 300 (5 minutes)
max-age Browser cache TTL 60 (1 minute)
stale-while-revalidate Serve stale while fetching fresh 86400 (1 day)
stale-if-error Serve stale if origin errors 604800 (1 week)

Layer 2: Workers Cache API

For dynamic content that cannot be CDN-cached but benefits from in-memory caching:

// server/middleware/cache.ts
interface CacheOptions {
  ttl: number           // Time to live in seconds
  key: string           // Custom cache key
}

export async function withWorkerCache<T>(
  handler: () => Promise<T>,
  options: CacheOptions,
  request: Request
): Promise<T> {
  const cacheKey = new Request(`https://cache.tanstackship.com/${options.key}`, request)
  const cache = caches.default

  // Try cache first
  const cachedResponse = await cache.match(cacheKey)
  if (cachedResponse) {
    const data = await cachedResponse.json()
    console.log(`Cache HIT for ${options.key}`)
    return data as T
  }

  console.log(`Cache MISS for ${options.key}`)

  // Execute the handler
  const data = await handler()

  // Store in cache
  const response = new Response(JSON.stringify(data), {
    headers: {
      "Content-Type": "application/json",
      "Cache-Control": `public, max-age=${options.ttl}`,
    },
  })

  // Wait for cache storage (don't block the response)
  request.waitUntil?.(cache.put(cacheKey, response))

  return data
}

Usage in Server Functions

// Cache frequently accessed data
export const getDashboardStats = createServerFn({ method: "GET" }).handler(
  async ({}, { context }) => {
    return withWorkerCache(
      async () => {
        // Expensive query — only runs on cache miss
        const [users, revenue, subscriptions] = await Promise.all([
          context.env.DB.prepare("SELECT COUNT(*) as count FROM users").first(),
          context.env.DB.prepare("SELECT SUM(mrr) as total FROM subscriptions WHERE status = 'active'").first(),
          context.env.DB.prepare("SELECT status, COUNT(*) as count FROM subscriptions GROUP BY status").all(),
        ])
        return { users, revenue, subscriptions: subscriptions.results }
      },
      { ttl: 300, key: "dashboard/stats" },
      context.request
    )
  }
)


Layer 3: Workers KV for Global Configuration

KV is ideal for configuration data that changes infrequently:

// Store pricing configuration in KV
await context.env.KV.put("pricing:starter", JSON.stringify({
  monthlyPrice: 29,
  features: ["basic_analytics", "email_support"],
}))

// Read from any Worker globally with ~10ms latency
const pricing = JSON.parse(
  await context.env.KV.get("pricing:starter") ?? "{}"
)

When to Use KV vs D1 vs Cache API

Data Type Storage Read Latency Consistency TTL
Session tokens KV ~10ms Eventually consistent Session duration
Pricing config KV ~10ms Eventually consistent Manual update
Feature flags KV ~10ms Eventually consistent 30s-5min cache
Blog content D1 ~5ms Strong N/A
User dashboard Cache API ~1ms Strong 1-5min
Static assets CDN ~5ms Strong 1 year

Layer 4: Stale-While-Revalidate

The most important caching pattern for SaaS:

export const getAnalyticsData = createServerFn({ method: "GET" }).handler(
  async ({ data, context }: { data: { timeframe: string } }) => {
    // Serve from cache, then refresh in background
    context.response.headers.set(
      "Cache-Control",
      "public, s-maxage=60, stale-while-revalidate=86400, stale-if-error=604800"
    )

    // User sees cached data instantly
    // Worker refreshes cache in background
    const result = await context.env.DB.prepare(`
      SELECT date, pageviews, unique_visitors
      FROM analytics_summary
      WHERE date >= datetime('now', ?)
    `).bind(
      data.timeframe === "7d" ? "-7 days" : "-30 days"
    ).all()

    return result.results
  }
)


Cache Invalidation

Manual Cache Purging

// server/admin/cache.ts
export const purgePageCache = createServerFn({ method: "POST" }).handler(
  async ({ data, context }: { data: { url: string } }) => {
    // Purge specific URL from CDN cache
    const formData = new FormData()
    formData.append("files", data.url)

    const response = await fetch(
      `https://api.cloudflare.com/client/v4/zones/${context.env.CLOUDFLARE_ZONE}/purge_cache`,
      {
        method: "POST",
        headers: {
          "Authorization": `Bearer ${context.env.CLOUDFLARE_API_TOKEN}`,
        },
        body: JSON.stringify({ files: [data.url] }),
      }
    )

    return response.ok ? { purged: true } : { purged: false }
  }
)

Cache Tags (Surrogate Keys)

// Tag cache entries for granular invalidation
context.response.headers.set(
  "Cache-Tag",
  `user-${userId},org-${orgId},page-dashboard`
)

// Purge all cache entries with a specific tag
// POST /api/purge?tags=user-123


Cache Strategy Decision Tree

Is the data user-specific?
├── YES → Do not cache at CDN level
│   ├── Use Workers Cache API with user-specific key
│   └── TTL: 30s-5min depending on staleness tolerance
└── NO → Can benefit from CDN cache
    ├── How often does it change?
    │   ├── Never (static assets) → CDN cache 1 year
    │   ├── Rarely (pricing, config) → CDN cache 1 hour + purge on update
    │   └── Frequently (dashboard data) → CDN cache 1-5min
    └── Is stale data acceptable?
        ├── YES → stale-while-revalidate for best performance
        └── NO → Short TTL with Workers Cache API fallback


Cache Hit Rate Optimization

// Log and monitor cache performance
export const getCacheMetrics = createServerFn({ method: "GET" }).handler(
  async ({}, { context }) => {
    const metrics = await context.env.ANALYTICS.query({
      sql: `
        SELECT
          blob1 as cache_status,
          count() as count,
          avg(double1) as avg_latency
        FROM analytics_cache
        WHERE timestamp > now() - INTERVAL '1' HOUR
        GROUP BY blob1
      `,
    })

    return metrics.rows
  }
)

Cache Status Meaning Target % Action If Below Target
HIT Served from edge cache > 70% Increase TTL, add more cacheable endpoints
MISS Not in cache, fetched from origin < 20% Review cache key, pre-warm cache
STALE Served stale, refreshing in bg < 10% Increase stale-while-revalidate window
DYNAMIC Bypassed by explicit no-cache < 5% Audit no-cache declarations

Production Caching Checklist

  • [ ] Static assets cached for 1 year with content hash in filename
  • [ ] HTML pages cached with 5-60s TTL + stale-while-revalidate
  • [ ] API responses cached with appropriate TTL based on update frequency
  • [ ] Workers Cache API used for user-specific but slow-changing data
  • [ ] KV used for global configuration and feature flags
  • [ ] Cache tags set for granular invalidation
  • [ ] Cache hit rate monitored and alerted on drop > 10%
  • [ ] Stale-while-revalidate enabled for critical endpoints
  • [ ] Cache pre-warming for known traffic patterns
  • [ ] Cache key strategy reviewed for query parameter variance

Conclusion

Edge caching is not a single configuration — it is a multi-layered strategy. The right approach depends on the data: static assets should be cached aggressively, user-specific data should use Workers Cache API, configuration should live in KV, and database queries should be cached only where staleness is acceptable.

The Cloudflare stack makes this strategy accessible: CDN cache for the first layer, Cache API for dynamic content, KV for configuration, and Cache Reserve for large objects. Combined, they can serve 90%+ of requests from edge cache with <10ms latency.

For a production SaaS with an optimized caching strategy, see tanstackship.com.

Related Resources