Back to Blog
Lesson 6 of the Cloudflare: Cloudflare for Developers: DNS to CDN course
Cloud NativeJuly 7, 20264 min read

Configuring Cache Rules for Advanced Performance and TTL Control

Learn how to create custom Cache Rules in Cloudflare to set TTL values and bypass cache for specific paths, giving you total control over edge performance.

CloudflareCDNPerformanceCache RulesTTLWeb Development

Previously in this course, we explored Global Network and Edge Caching, where we identified the difference between cacheable static assets and dynamic content. Now, we move from passive observation to active control by configuring Cache Rules.

While default settings work for many sites, professional applications often require granular control. You might need to cache a specific API response for five minutes or ensure that sensitive admin routes are never stored on the edge. Cache Rules are the primary tool for this level of precision.

Understanding Cache Rules and TTL

Cache Rules allow you to define exactly how Cloudflare handles requests based on URL patterns, headers, or other request properties. The most critical setting you'll manage is the Time To Live (TTL).

TTL tells Cloudflare how long to keep an asset in its edge cache before checking back with your origin server. A longer TTL improves performance by reducing origin load, but it risks serving stale data. A shorter TTL keeps content fresh but increases traffic to your origin.

Why You Need Custom Rules

Default caching behavior is often broad. By creating custom rules, you can:

  1. Bypass Cache: Ensure dynamic user-specific data is never cached.
  2. Override TTL: Force a longer cache duration for static assets that rarely change.
  3. Control Eligibility: Explicitly define which file types or paths should be eligible for the CDN.

Creating Your First Cache Rule

To create a rule, navigate to Caching > Cache Rules in your Cloudflare dashboard. We will build a rule to bypass the cache for a specific administrative path, /admin/*.

  1. Click Create rule.
  2. Name it "Bypass Admin Cache".
  3. Under When incoming requests match, define your filter:
    • Field: Hostname equals yourdomain.com
    • AND Field: URI Path starts with /admin/
  4. Under Cache eligibility, select Bypass cache.
  5. Click Deploy.

Any request matching this pattern will now skip the edge cache entirely, hitting your origin server directly every time.

Setting Custom TTL Values

For assets like CSS or JS files that have versioned filenames (e.g., main.a1b2c3.js), you can set a long TTL to maximize performance.

  1. Create a new rule named "Cache Static Assets".
  2. Filter for File extension equals js or css.
  3. Under Cache eligibility, choose Eligible for cache.
  4. Set Edge TTL to "Override origin" and set the value to 1 month (2592000 seconds).

Hands-on Exercise: Implementing an API Cache

In our ongoing project, let’s assume you have an API endpoint that returns a list of products. This data only changes once an hour.

Task:

  1. Create a Cache Rule for the path /api/products.
  2. Set the Edge TTL to 3600 seconds (1 hour).
  3. Ensure the Browser TTL is also set to respect the edge settings.
  4. Verify by checking the cf-cache-status header in your browser's Network tab after the first request.

Self-check: If you see HIT after the first request, your rule is working. If you see MISS repeatedly, check your filter logic to ensure it matches the request path exactly.

Common Pitfalls

  • Over-caching: Accidentally caching pages that contain sensitive user information. Always use "Bypass cache" for login, profile, or checkout pages.
  • Ignoring Headers: Your origin server might be sending Cache-Control: private or no-store headers. Cloudflare respects these by default; if your rule isn't working, check your origin headers first.
  • Rule Order: Rules are processed in order. If you have a broad "Cache All" rule at the top, a more specific "Bypass" rule below it might never be reached. Keep your most specific rules at the top.

FAQ

Q: How do I know if my rule is working? Use the browser's Network tab (F12) to inspect the response headers. Look for cf-cache-status: HIT or MISS. If you see BYPASS, your rule is correctly forcing the request to skip the cache.

Q: Can I use Cache Rules to purge files? No. Cache Rules define how content is cached. To remove content from the cache, you need to perform a purge, which we will cover in the next lesson.

Q: What is the difference between Edge TTL and Browser TTL? Edge TTL controls how long Cloudflare keeps the file. Browser TTL tells the visitor's browser how long to keep the file locally. Setting these correctly is key to Edge Caching with Surrogate Keys.

Recap

Cache Rules provide the surgical precision needed for modern web performance. By defining TTLs and bypasses, you protect your origin while optimizing the user experience. You now have the power to decide exactly what lives on the edge and for how long.

Up next: We will dive into Managing Cache via Dashboard and API, where you'll learn how to clear that cache when content changes.

Similar Posts