Optimizing CDN Server Caching
Caching Issues Due to Dynamic Content Classification
While analyzing the API response speed of our game server recently, I discovered that, unlike static files, JSON data was not being cached. The cause was that, under Cloudflare's default policy, text-based data such as HTML or JSON is classified as dynamic content.
The CF-Cache-Status: DYNAMIC status in the response header means that user requests bypass the CDN and go directly to the Origin server, which consequently increases the load on the Origin server.
https://developers.cloudflare.com/cache/concepts/default-cache-behavior Default cached file extensions "Cloudflare only caches based on file extension and not by MIME type. The Cloudflare CDN does not cache HTML or JSON by default. Additionally, by default Cloudflare caches a website's robots.txt."
Phase 1: Implementing Forced Caching Using Cache Rules
While static assets (CSS, JS) are easy to version-manage (Cache Busting) via filename hashing, it is difficult to apply this method to API endpoints because the URL must remain fixed as a unique identifier for the resource.
Cache BustingA method to force the user's browser to download the latest version of a file.
As a primary approach to solving the above issue, we utilize Cloudflare's Cache Rules settings. This forces an Edge cache TTL for requests with specific extensions (*.json), thereby reducing the frequency of Origin requests.
s-maxage(Edge TTL): Cache validity time for intermediate servers.max-age(Browser TTL): Cache validity time for the user's browser.
Trade-off - Data Consistency Issues
However, setting a long TTL inevitably entails the side effect of reduced data freshness. Even if the original data is modified due to game balance patches, etc., the CDN and browser cannot recognize that the file has changed as long as the URL structure based on the unique ID is maintained. This results in stale data being continuously sent to users during the pre-configured long s-maxage period, causing critical information inconsistency issues in service operation.
Phase 2: Automated Cache Purge Strategy
To fundamentally solve the above issue, we integrate cache deletion logic into the deployment pipeline.