Cloudflare CDN: How Caching Works for Performance — Complete Guide
In this tutorial, you'll learn about Cloudflare CDN: How Caching Works for Performance. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
This tutorial explains how the Cloudflare content delivery network caches your static assets at edge locations worldwide, how Caching decisions are made based on HTTP headers, and how you can optimize cache hit ratios for faster page loads.
Why CDN Caching Matters
Every millisecond of load time affects user experience and conversion rates. A CDN like Cloudflare stores copies of your static files (images, CSS, JavaScript, fonts) at hundreds of edge locations around the world. When a Visitor requests a file, it is served from the nearest edge data center instead of traveling all the way to your origin server. This reduces latency by 50-80% for global audiences and cuts origin server load by 60-90% for cacheable content.
Real-world use: DodaZIP serves file previews and thumbnails through Cloudflare's CDN. With a 90% cache hit ratio, the origin server handles only 10% of the traffic it would without Caching, reducing bandwidth costs by thousands of dollars per month.
How Cloudflare Caching Works
flowchart LR
A[Visitor requests file] --> B[Cloudflare edge]
B --> C{Cached at edge?}
C -->|Yes| D[Serve from cache]
C -->|No| E[Forward to origin]
E --> F[Origin responds]
F --> G[Cache file at edge]
G --> D
D --> H[Deliver to visitor]
style B fill:#f90,color:#fff
style D fill:#f90,color:#fff
What Cloudflare Caches by Default
Cloudflare caches static content automatically based on file extension and response headers. By default, these file types are cached:
| File Type | Extensions |
|---|---|
| Images | .jpg, .jpeg, .png, .gif, .webp, .avif, .svg |
| Fonts | .woff, .woff2, .ttf, .otf, .eot |
| Media | .mp4, .webm, .ogg |
| Documents | .pdf, .doc, .docx |
| Code | .js, .css, .ts, .tsx, .jsx |
| Data | .json, .xml, .csv |
Cache-Control Headers
Cloudflare respects the Cache-Control header sent by your origin server. These headers determine how long content stays cached and whether it can be revalidated.
# Strong caching for versioned assets
Cache-Control: public, max-age=31536000, immutable
# This tells Cloudflare to cache for 1 year.
# The "immutable" flag means the file never changes (use with versioned filenames like styles.a1b2c3.css).
# No caching for dynamic content
Cache-Control: no-cache, no-store, must-revalidate
# Tells Cloudflare to never cache this response.
# Always forward the request to origin. Use for API responses and user-specific pages.
# Check what Cloudflare cached for a URL
curl -sI https://example.com/style.css | grep -i "cf-cache-status"
# Expected output:
# cf-cache-status: HIT
# Possible values: HIT, MISS, EXPIRED, REVALIDATED, DYNAMIC, BYPASS
Cache Keys and Vary Headers
Cloudflare uses the URL as the default cache key. If you serve different content based on headers (like Accept-Encoding for gzip), use the Vary header.
# Origin response that varies by encoding
HTTP/2 200
Vary: Accept-Encoding
Cache-Control: public, max-age=86400
Content-Type: text/css
# Cloudflare will cache separate copies for gzip and non-gzip clients
Checking Cache Status
Every Cloudflare response includes headers that tell you whether the content was served from cache.
curl -sI https://example.com/logo.png
# Expected headers:
# cf-cache-status: HIT
# cf-ray: 7a2b3c4d5e6f7g8h
# age: 85432
The age header shows how many seconds the cached object has been in the edge cache. cf-ray is a unique identifier for every request through Cloudflare's network.
FAQ
Practice Questions
- What is the difference between cf-cache-status HIT and MISS?
- How does the Cache-Control header affect Cloudflare's Caching behavior?
- Why is it important to use versioned filenames (like styles.a1b2c3.css) with long max-age values?
Summary
Cloudflare's CDN caches static content at hundreds of edge locations worldwide. The Caching behavior is controlled by Cache-Control headers from your origin server. Cloudflare caches content based on file extension by default, and you can customize Caching rules with page rules or cache rules. A high cache hit ratio means faster load times and lower origin server costs.
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro — security-first tools for the modern web.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro