PerformanceCDNCloudflareCaching

Performance: CDN Selection and Configuration Guide

Choose and configure the right CDN for your SaaS, comparing Cloudflare, Fastly, AWS CloudFront, and Bunny.net for edge caching and global delivery.

Alex Chen
Alex Chen
July 8, 202611 min read
Also available in:Deutsch · 中文

TL;DR: A CDN is essential for global SaaS performance, caching static assets and dynamic content at edge locations near users. This guide compares major CDN providers, covers cache configuration, purge strategies, and custom origin setup.

Introduction

A Content Delivery Network (CDN) caches your content at edge servers worldwide, reducing latency for users regardless of location. For SaaS applications, the CDN is the first layer of performance optimization.

CDN Provider Comparison

FeatureCloudflareFastlyCloudFrontBunny.net
Edge locations330+150+600+100+
Pricing modelFree/Pro/BusinessUsage + minimumUsagePer-TB flat rate
Custom VCLNo (Workers)Yes (VCL)Yes (Lambda@Edge)No
Cache APIWorkers Cache APIPURGE APIInvalidationPURGE API
DDoS protectionBuilt-inAdd-onAWS ShieldAdd-on
Setup complexitySimpleMediumMediumSimple

Cloudflare Cache Configuration

Cloudflare's cache rules are configured through wrangler.jsonc or the Cloudflare dashboard. The following configuration sets up cache rules for static assets with a 24-hour TTL and stale-while-revalidate for instant cache refreshes:

toml
# wrangler.jsonc
{
  "rules": [
    {
      "description": "Cache static assets",
      "expression": "(http.request.uri.path ~ '\\.(css|js|png|jpg|svg|woff2)$')",
      "actions": {
        "cache": {
          "ttl": 86400,
          "serve_stale": {
            "status": [200],
            "stale_while_revalidate": 3600
          }
        }
      }
    }
  ]
}

The serve_stale configuration with stale_while_revalidate ensures users never see a cache miss — the edge serves the stale content while fetching a fresh version in the background. This is critical for assets that are cached for long periods.

Cache Strategy by Content Type

Content TypeTTLCache LevelCache Key
Static assets (CSS/JS)1 yearEdge + BrowserFull URL
Images30 daysEdge + BrowserURL + dimensions
API responses0-60sEdge onlyURL + headers
HTML pages0-300sEdge onlyURL + cookies

Conclusion

Cloudflare provides the best combination of edge coverage, performance features, and simplicity for most SaaS applications. Start with default caching and tune TTLs based on content freshness requirements. For more cache optimization techniques, see our Edge Caching Strategies Guide, and learn how to reduce Time to First Byte in the Performance: Reducing TTFB guide. The HTTP/2 and HTTP/3 Optimization article covers protocol-level performance improvements, and the Core Web Vitals Optimization Guide ties CDN strategy to user experience metrics.