Basically the cacheTTL you use for the fronting cache is the same as the cacheTTL for KV. This means that every time you read from KV it's a cold read whereas KV takes great pains to make sure that you never see cold reads for frequently accessed keys.
This guide provides best practices for optimizing Workers KV latency as well as covering advanced tricks that our customers sometimes employ for their …
Note that this change also saves you more money. Because you're sampling at 1%, you will have a small number of requests that end up going through to KV & updating the value you have cached. The current behavior has a stampeding herd every 1 minute where ALL requests suddenly go to KV.
You can make the sampling threshold and the fronting cache duration tunable but cap the fronting cache duration at 50-55s at most. I would also make sure to call out in the docs that customers should set long cacheTTL as described in that document.
And ultimately my goal is for there to be no pricing difference of putting the Cache API in front, but we're still a ways away from realizing that vision. We're working on making KV faster than the cache API first.
Very good point. Skye is correct that performance is not the #1 priority for BetterKV, pricing is, but I do also definitely see your point. I’ll probably add your suggestions as a performance mode that people can enable
It's also net a cost savings. Would you be open to making it the default & the user needing to turn it off? Even if you don't want to do the probabilistic piece, can you shorten the cacheTTL so that it's under what you give KV? Otherwise, BetterKV is not really different than cache+R2.
But yeah, being more likely to refreshing the value as you approach the expriy end date is a significant cost saving measure. Counter-intuitive but true. It's how KV has to operate under the hood for similar reasons. We technically use a bounded exponentially increasing algorithm where after 5s there's an infinitesimaly small probability of a refresh that doubles with every passing second up to 100% at something like 45s or 50s at which point every read has a 100% probability of causing a background refresh. That way only a small subset of requests end up triggering a refresh while the rest just always serve from the cache without any stampeding herd. We have other tricks we're layering in as well, but they're not available externally yet.
How old is this writeup? I did not even know that putting cache in front of KV was a thing at some point, and from the time I started using (~2 years now), I always assumed KV is parallel to Cache. In other words, since KV is already at the edge, what's the point of cache in front of a cache?
Now that you have exposed the cache status of reads, this becomes a lot easier to implement - previously when I thought about a probabilistic update, using the time the read took as an indicator for hit/miss was rather messy, so thanks for implementing
I have defined id, preview_id in wrangler.toml like this, is it ok to upload it as is where it can be seen by unspecified people? Or should I put them in .dev.vars?
@zegevlier Thank you. Is it OK to write directly in wrangler.toml and make it public for things like api key provided by cloudflare, not just kv? I'm curious because GCP and AWS keep things like api_key secret.
In general (this might not always be true, but it's usually a good rule of thumb. As far as I know it always holds for Cloudflare stuff), IDs are safe to share, keys and secrets are not.