Using Cloudflare Workers KV for Simple Key-Value Storage
Workers KV is a globally replicated key-value store built into Cloudflare. It’s eventually consistent and optimized for read-heavy workloads — a good fit for config, feature flags, or cached content.
Setting up
Create a namespace via the Cloudflare dashboard or Wrangler:
wrangler kv namespace create MY_STORE
Bind it in wrangler.toml:
[[kv_namespaces]]
binding = "MY_STORE"
id = "abc123..."
Reading and writing
export default {
async fetch(request: Request, env: Env) {
// Write
await env.MY_STORE.put('greeting', 'hello');
// Read
const value = await env.MY_STORE.get('greeting');
return new Response(value ?? 'not found');
},
};
Things to keep in mind
- Eventual consistency: writes propagate globally within ~60 seconds. Don’t use KV where you need immediate read-after-write consistency.
- Size limits: values up to 25 MB, keys up to 512 bytes.
- Reads are free at the edge: KV is great for data you write infrequently but read constantly.
For strong consistency, look at Durable Objects instead.