A Cloudflare KV cache in front of your API cuts database load without touching your front end or back end.

I published this walkthrough in September 2025. It shows how I put a caching layer between an app and its database using Cloudflare Workers and Workers KV, with no change to either side of the code. It's for technical founders with a read-heavy app and a database bill that keeps growing.

The business case

Databases are where cloud bills hurt. With most cloud vendors you pay for performance. More traffic means more load, and when the database can't keep up, you scale it up and pay for the bigger tier. In the video I said that on AWS, Google Cloud or Azure, the database is the expensive part.

A lot of that load is waste. In the demo I clicked through an app filled with generated dummy data. Every page reloaded the same dropdowns. My estimate was that it's the same information about 90% of the time. If those reads come from a cache, the database handles fewer requests and can stay smaller. A smaller database costs less.

The ideal fix is a code change in your own app, and I said so. This video shows a second route for when you can't or don't want to change code yet. Neither the front end nor the back end knows the cache exists.

The setup it fits

The setup is the common one. A front end of any kind, from a .NET app to Next.js to a plain HTML page, calls an API. The API talks to a database, whether that's SQL, PostgreSQL or MongoDB. In my diagram the API was Azure Functions, but any API works.

A request checks KV first. On a hit, the data goes straight back to the user. On a miss, the request makes the round trip to the database through your API, and the response gets saved into KV on the way back out.

Route traffic through Cloudflare

Step one is using Cloudflare for DNS, so traffic for your API passes through its global network. In the dashboard you turn on the proxy for the API's DNS record, the orange cloud. With the proxy off, Cloudflare only answers DNS and doesn't inspect the traffic. Cloudflare's proxy docs describe the same split.

Proxying also gets you inspection that can block attacks before they reach your server. I said some of that comes with paid security tiers and some is free. I also said routing through Cloudflare is arguably faster, because of the global network.

Then you add a rule that watches for certain paths. My app's convention for dropdowns is /api/ux/dropdown. Any request on that path goes to a Worker. The Worker checks the cache and returns a hit. On a miss it forwards the request to your existing endpoint. It doesn't query the database itself, because then you'd be copying your back-end code.

Jump to

I walk through the diagram, the Worker code and the dashboard rule in the full video on my channel: Cut Your Database Costs with Cloudflare KV on YouTube.

Keys that always match

Invalidation is the hard part. Someone updates a record, and the cached copy is now stale. You have two options. A code change that clears the cache on update is the approach I called the recommended one. The no-code option is to watch your create and update endpoints through Cloudflare the same way you watch reads.

Either way, the key for a piece of data has to be identical every time. For reads, I take the unique query string and hash it. A hash gives the same output for the same input, and changing one letter gives a completely different result. So every unique URL gets its own key.

For an update, the request is a PUT or POST with the record ID inside it. Extract that ID and hash it so it matches the key the read produced. You may need a separate Worker for this. My app is multi-tenant, so my cache key combines the action and the tenant ID.

What the Worker does

The Worker reaches KV through its environment binding and puts the value under the key. My version adds two lists. One holds URLs that are allowed to be cached. The other holds URLs that never are, like real-time status or live inventory. Before caching anything, the Worker checks those lists.

This is separate from Cloudflare's built-in caching rules, which I described as broader and less fine-grained. In the video I used a Redirect Rule with a custom filter on the URL path, sent as a 301 to the Worker's URL. Filters can also match a query string, a country or HTTP headers. A redirect sends the browser to a new address. Worker routes are Cloudflare's documented way to run a Worker in front of a proxied hostname without one, so check both options before you pick.

KV fits this job. Cloudflare's KV docs call it eventually consistent and ideal for read-heavy data such as configuration. A change can take 60 seconds or more to show up in other locations. That's fine for dropdowns and wrong for anything live.

Serve fast, refresh later

The tool I pointed out at the end is ctx.waitUntil. It lets a Worker keep working after it has sent its response. One pattern returns the cached copy at once, even if it's a little stale, and refreshes it in the background. The other always returns the cache, then fetches fresh data and swaps it in if anything changed.

Where it fits

This works for dropdowns, settings and configuration that rarely change. It's a poor fit for apps with heavy writes, because you won't get the benefit. For read-heavy apps I recommended it without hesitation.

I said it significantly reduced my own database spend. I didn't give a figure. I also warned that some headers change on the way through, and that can affect security on your back end. Test it in a dev environment before production.

Cloudflare has outages too, so keep the API reachable on its own. For more on the platform, read how I mapped a SaaS architecture on Cloudflare and how I explain Cloudflare for non-technical founders.

Disclosure: in April 2026 I said Cloudflare gave me a $25,000 credit as sponsorship. No affiliate links.

If you want to build on it yourself, take the free Cloudflare course.

About Dwain

Dwain Browne

I'm Dwain Browne, Founder of LeadScore AI and SnapSuite. I'm a Toronto-based software architect with 25+ years across software, sales, operations and entrepreneurship. SnapSuite helps contractors and trades teams get paid faster, and LeadScore AI shows you which leads deserve your next call. I also run Vibe Code to Profit, a community helping people build software with AI.

YouTube · LinkedIn · Book a call