Looking for a product/solution
Hi,
I have an API which hits around 300 million requests a month. Most of these are cached through cloudflare but I am looking for a product which does the description below.
The endpoint is api.example.com/profile/{uuid}
The flow goes like this
- If {uuid} has a profile display their profile
- If not, display a default profile
Now - Ideally I'm basically looking for a cloudflare solution for "Display the default UNLESS its in this list/cache/something"
I thought workers and KV/R2 but 300 million a month is around $100? Any suggestions appreciated.
2 Replies
I remember we had this conv a bit before: https://discord.com/channels/595317990191398933/909458221419356210/1280653792005853278
I would preface this with I honestly don't think for 300 million requests a month with whatever bandwidth use that even $300 or so is unreasonable (Cloudfront's request pricing alone basically be that) but I understand small budgets and that CF's pricing stuff can sometimes be a bit confusing. A lot is given away free and then some stuff isn't and those hard lines can be confusing.
Important bit of context you didn't add here but did before
All this depends on if cloudflare workers only respond to "MISS" cache requests? Then its a lot smaller around 50 million cache miss requests a month which is much more doable.Let's break down all your options. I'll exclude the included quotas of Workers Paid/R2/etc because it gets hard to factor it into everything constantly: Workers: Workers are $90 for 300 mil requests + whatever cpu time usage (which wouldn't be very high for this). Assuming no cache: KV for 300 mil requests would be $150 for read requests alone R2's a little cheaper but only one location at $108 Now, you can add cache in front of both. KV has a lib for it: https://flareutils.pages.dev/betterkv/old, or Cache API direct: https://developers.cloudflare.com/workers/runtime-apis/cache/. There's an R2 example of Cache API here: https://developers.cloudflare.com/r2/examples/cache-api/. Cache costs you nothing. I would expect to see the same cache % as you see now as long as you do not currently have tiered caching on. With Cache assuming 50 mil uncached requests, KV would be $25 and R2 would be $18. R2: There's R2 based options with R2 Custom Domains, you aren't charged for cache hits, only misses. Assuming you have a cache rule forcing cache and your current amount of staleness with 50 million misses/month works for you. Cloudflare Snippets are small limited workers, and are in Alpha and not recommended for production use. They require Pro, but no per request charges or cpu time charges. You could use them with a setup like this to fetch a R2 Custom Domain and then have your fallback, just like in a normal worker. (side note: You can't use KV from Snippets realistically, no bindings and the KV Rest API only runs at core locations w/ no cache) Or, if you can accept Pro's static cost, you can use Custom Error Responses for 404's in front of your R2 Custom Domain: https://developers.cloudflare.com/rules/custom-error-responses/, returning your default json. Your cache rules can drive the exact ttl for 404s or 200's/etc specifically You'd have to move/copy/replicate all your json configs into R2, and R2 only exists in a single region, so without cache you've got some latency penalties. KV has two central stores (EU/US) Other than that all, you can also have cache in front of your Worker executing in some special setups. Pages and Pages Functions on Custom Domains do for example, this is because they use CF For SaaS, so CF goes through the custom domain zone/applies cache/rules/etc and then the pages.dev zone where it runs the worker. You could use CF For SaaS directly or Pages w/ a Custom Domain and a cache rule to "have cache in front of your worker" R2 Custom Domains + Custom Error Responses might be a simple enough of a setup to play with if you don't need any custom logic, can remove worker/function costs entirely
Cloudflare Docs
Cache | Cloudflare Workers docs
Control reading and writing from the Cloudflare global network cache.
Cloudflare Docs
Use the Cache API | Cloudflare R2 docs
Use the Cache API to store R2 objects in Cloudflare’s cache.
I think this is what slightly confused me about Cloudflare workers and its cache. Surely workers have to run to access the cache which I thought counts as a request. but this is my assumption...
- Workers run for the first time
- It puts it in the cache
- Future requests hit cache?