Caching Workers KV responses

From the docs it’s pretty clear that KV caches reads in each region, so caching within the worker code is unnecessary for performance. However, from a billing standpoint wouldn’t it make sense to use the cache API to cache every interaction with KV, since that’s where the expense is and cache ops are free?

In addition, the cache api docs don’t make this totally clear: can a workers script store any computed value in the cache by just wrapping it in a synthetic response object?

You can absolutely cache KV content, however, it might take much longer than the 10 seconds of global replication before the KV values from cache can be read. If that’s not a problem, then cache away!

Thanks for the reply. Follow-up: Cloudflare plans show a “minimum cache expire ttl”. Does this minimum time apply to cache items added via the workers api? We’re targeting ~60s max refresh delay on keys that see extremely spikey usage, so being able to cache KV responses with 60s ttl would be ideal.

My experience is that 60 seconds works well. For burstable runs, I use a global variable that is an array. solution with an expiration time.

since that’s where the expense is and cache ops are free

Note that cache ops are not free they count as a workers requests, but they are cheaper than a KV read.

Do you mean they count toward the request quota? There’s no per-access cache cost, correct?

I’m considering you are on a paid plan since free has a limitation of 1000 requests per 10 minutes.

Every time a worker runs, regardless if data is generated or is being returned from the cache that counts towards plan (0.50/million requests per month).

Additional KV operations are priced as follows:

  • $0.50 per million additional Worker KV reads
  • $5.00 per million additional Worker KV writes
  • $5.00 per million additional Worker KV deletes
  • $5.00 per million additional Worker KV list operations

Additionally, there’s no KV access on the free plan, so they must be. :slight_smile:

1 Like

But…my free plan lets me use KV name spaces and variables.

You’re not paying the $5 for workers, yet you can create namespaces?

Oh…the Workers free plan. Sorry, I pay the $5 for workers (such a bargain!), and that covers all my plans, Free and Paid.

2 Likes

Cool, that makes sense :slight_smile: As long as you’re paying the $5 for workers, you can use them across any workers on your account, the workers.dev ones as well as the ones included in the $5 :slight_smile:

1 Like

Thanks, but I’m referring to the cache access itself, not the base cost of workers or KV. My understanding is that there’s a 50 put/request cache access quota, but no per-cache-access fee during the execution of a worker.