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
| Feature | Cloudflare | Fastly | CloudFront | Bunny.net |
|---|---|---|---|---|
| Edge locations | 330+ | 150+ | 600+ | 100+ |
| Pricing model | Free/Pro/Business | Usage + minimum | Usage | Per-TB flat rate |
| Custom VCL | No (Workers) | Yes (VCL) | Yes (Lambda@Edge) | No |
| Cache API | Workers Cache API | PURGE API | Invalidation | PURGE API |
| DDoS protection | Built-in | Add-on | AWS Shield | Add-on |
| Setup complexity | Simple | Medium | Medium | Simple |
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:
# 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 Type | TTL | Cache Level | Cache Key |
|---|---|---|---|
| Static assets (CSS/JS) | 1 year | Edge + Browser | Full URL |
| Images | 30 days | Edge + Browser | URL + dimensions |
| API responses | 0-60s | Edge only | URL + headers |
| HTML pages | 0-300s | Edge only | URL + 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.