When Edge Meets Persistence: Using Cloudflare Workers KV for Read‑Heavy Caching
Learn how Cloudflare Workers KV can turn a stateless edge function into a fast, globally‑replicated cache. The post walks through a real example, explains the write‑once/read‑many pattern, and highlights when KV isn’t the right tool.
28 Mar 2026, 15:40 UTC

Problem: Stateless Edge Functions Need State
Edge workers are great for low‑latency request handling, but they are inherently stateless. If you need to keep a cache of configuration, feature flags, or user‑specific data close to the request, you must bring persistence into the edge. Cloudflare’s KV (Key‑Value) store is marketed as a solution, but it isn’t a drop‑in replacement for a traditional database. Understanding its strengths and limits is essential before you decide to store critical data there.
Thesis: KV is a Read‑Heavy, Write‑Once Cache
KV is engineered for scenarios where you write a value once and read it many times. The architecture replicates data globally, so reads are fast from any edge location, but writes can take up to 60 seconds to appear everywhere. This makes KV ideal for static configuration, feature flags, or content that changes infrequently. It is not suitable for real‑time counters, financial balances, or any use case that demands strong consistency.
1. The Edge Persistence Gap
Traditional databases (PostgreSQL, DynamoDB, etc.) offer ACID guarantees but introduce network hops that add latency. In contrast, KV stores data in a distributed key‑value store that is co‑located with the Cloudflare edge network. A read from a KV key is served from the nearest data center, eliminating the round‑trip to a remote database. However, the trade‑off is that writes must propagate to all replicas, which introduces a delay and a cost per write.
2. Write‑Once, Read‑Many Pattern
Because KV is eventually consistent, the recommended engineering pattern is:
- Write once – update the key only when the underlying data changes.
- Read many – serve the cached value to every request.
This pattern keeps write traffic low, reduces replication cost, and ensures that reads are served from the local replica. It also aligns with the typical use case of serving configuration or static assets that change on a schedule.
3. Practical Example: Dynamic Config Cache
Below is a minimal setup that demonstrates how to cache a JSON configuration file in KV and serve it from a Worker. The example uses Wrangler, Cloudflare’s CLI, and the KV API.
- Create a KV Namespace (run once):
wrangler kv:namespace create CONFIG --preview # Note: replace CONFIG with a descriptive name.After creation, Wrangler will add a binding in
wrangler.toml:kv_namespaces = [ { binding = "CONFIG", id = "" } ] - Upload initial config:
wrangler kv:put CONFIG app-settings '{"featureA":true,"maxItems":10}'This writes a stringified JSON object to the key
app-settings. - Deploy the Worker:
wrangler publish - Worker code (index.js):
addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)) }) async function handleRequest(request) { // Try to read from KV const kvValue = await CONFIG.get('app-settings') if (!kvValue) { return new Response('Configuration missing', { status: 500 }) } const config = JSON.parse(kvValue) // Use config to drive logic const body = `Feature A is ${config.featureA}. Max items: ${config.maxItems}` return new Response(body, { status: 200 }) }
When a client requests the Worker, the KV read is served from the nearest edge location. If the config changes, you perform a new wrangler kv:put and the new value propagates globally. Clients may see the old value for up to 60 seconds, which is acceptable for most feature‑flag scenarios.
4. Trade‑offs & Limitations
| Aspect | KV | Traditional DB |
|---|---|---|
| Consistency | Eventual (≤60s delay) | Strong (ACID) |
| Write Cost | Higher (per write, replication) | Lower (single region) |
| Read Latency | Edge‑level (~<10ms) | Depends on region, can be >50ms |
| Value Size | ≤10MB per key (current limit) | No hard limit, but memory/storage costs apply |
| Use Case | Config, feature flags, static content | Transactions, real‑time counters, financial data |
Key points to check before choosing KV:
- Do you need immediate consistency after a write?
- Is the write frequency low compared to reads?
- Are values small enough to fit within the per‑key size limit?
- Will you exceed the per‑namespace quota (currently 50 GB for free tier)?
For example, a real‑time leaderboard that updates every second would suffer from propagation delays and increased cost. In that case, a dedicated database or a Redis cluster would be preferable.
5. Actionable Checklist
- Define the write frequency:
≤1 write per minute per keyis safe. - Verify value size:
JSON.stringify(value).length < 10MB. - Set up a
wrangler.tomlKV binding and test a read/write cycle locally. - Monitor KV usage in the Cloudflare dashboard:
KV Usagetab shows reads, writes, and quota. - Implement a fallback strategy: if
CONFIG.get()returnsnull, serve a cached in‑memory copy or a default response. - Document the eventual consistency window in your architecture guide so downstream services know to tolerate stale reads.
By following these steps, you can confidently use Cloudflare Workers KV as a low‑latency, read‑heavy cache that scales globally without the operational overhead of a traditional database.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.