I can cache on Cloudflare CDN JSON files. what about JSON API responses?

Hi, basic question (but on stackoverflow answers were quite complicated)

Can I cache on cloudflare CDN my JSON API responses?

E.g user requests my-website.com/api/blog/article-235346456 (behind that sits an express cloud function that routes the request and pulls the data from my firestore database) which returns the data of that article 235346456 in .JSON as a response. Since it doesn’t change often I don’t want to use my database unnecessary (i get charged at firestore for reads). So I want to cache that with Cloudflare CDN. If user changes something in the article, I just purge the cloudflare cache.

Is that possible? I think yes. I also found a tutorial How I used Cloudflare to reduce API response time to
But I just want to make sure it’s true. So I asked here

I know that it takes some seconds until CDN is purged & refreshed everywhere. But I don’t care since I don’t need that luxus in my app. If I would need that I’d use the Workers KV (which is quite expensive, but it’s a luxus so it of course has its justified cost)

Thanks a lot & kind regards

JSON responses can definitely be cached! You just need to create a Page Rule with Cache Level set to Cache Everything and Edge Cache TTL set to the maximum time a response may be cached (there’s no guarantee it’ll be cached for that long as it might be purged due to low demand) - this usually shouldn’t be more than an hour unless the response really doesn’t change often.


thanks buddy!!
one more question: can I use instead of google cloud functions, Cloudflare Workers?
or may that throw some kind of error?

The architecture of Google Cloud Functions (Node.js) is quite different from that of Cloudflare Workers (V8 Engine). Depending on what you’re doing, your code might be (almost) directly transferable, require some modifications or not work at all. If you join the Cloudflare Workers Community Discord we’ll be able to guide you :slightly_smiling_face:

1 Like

thanks a lot buddy!! I really appreciate your help sempai! :heart: :heart: :heart: :heart: :heart:

I know Workers offer their own stuff like the Cache API and Workers KV where I could cache .json data. but then I’d have to always request a worker, and unlike the CDN they cost money. (ok they are extremely cheap to be fair (and the Cache API is completely free), but you know my coding heart just wants optimization optimization optimization; why spending money if I can solve that for free with free Cloudflare cdn. If I have to cache private data, then I will go for sure with Workers + Cache API because I have to do auth. but for public data the Cloudflare CDN is the thing I need.
Or is my noob brain thinking something wrong? I am always happy about opinions critics)

If the requests you’re handling are very cacheable (many requests to a few different URLs) then Cloudflare Cache -> Google Cloud Functions -> Firebase might end up cheaper. If your requests aren’t very cacheable (a few requests to many different URLs) then Cloudflare Workers + Cache API -> Firebase would probably win, even if they’re invoked on each requests.

Again, I’d suggest you join the Cloudflare Workers Community Discord so we can better take a look at your use case!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.