How can I better use KV?

Hi there!
I’m Eunjae {redacted}. I’ve recently started using CF worker and the KV.

I’m running a multi-tenant SaaS and caching the api response for quicker rendering.

It’s just very normal flow.

Whenever something has to change on a page, I need to clear the cache. However, it seemed like it took a certain amount of time until it actually clears the cache.

Because Workers KV stores data centrally and uses pull-based replication to store data in cache, it is generally good for use cases where you need to write relatively infrequently, but read quickly and frequently. It is optimized for these high-read applications, only reaching its full performance when data is being frequently read.

https://developers.cloudflare.com/workers/learning/how-kv-works/

According to the doc, I guess it makes sense because the cache lives on the edge, and it can take time to clear them all.

For your information, clearing cache doesn’t happen a lot. I mean, it requires a human interaction on my service. It’s not something I need to clear the cache every minute, or so.

What would you recommend me to solve this situation?

It sounds like you understand how KV is working, and that it can take up to ~60 seconds to writes to be acknowledged everywhere.

I’m not really sure there’s a “solution” here all the time you’re using KV, since the eventual consistency model is by design. If you care about extreme consistency, Durable Objects may be a better fit.

1 Like

Oh I didn’t know about Durable Objects. I could probably use it to store cache key, and keep the rest still in KV. Then the rotated cache key will make my worker miss the cache, and then load the raw data onto the KV with the new cache key. That’ll likely enhance the situation.

Thanks for the reply!

1 Like

It’s better to use cryptographic key resolution with your validity & store parameters built into the intermediary keys. On requests your worker can compute the intermediary key and determine its existence and validity without needing to do a kv read try to the source-of-truth and without needing to so lookup the keys in any strong consistency db first.

It’s always faster to resolve something logical with compute rather than having to wait on network latency to reach out over the internet to some distant source of truth or having to slow down your system with a slow strong-consistency database. Even if your DB was on the same physical machine, the time required to connect, execute the query, and return the results is slower in some circumstances where that data may not exist or maybe thown out after validating it.

1 Like

Hi thanks for the reply.

If I understand your comment correctly, I have a cache key. Whenever I need to invalidate all the cache, I just rotate the cache key, then all the key values tied to the previous cache key become unused. But the problem was to invalidating the cache key because it can take up to a minute.

That is my issue and I want to make it quicker :slight_smile:

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