Setting the file. One moment.
Chapter 04 · Cloudflare Deploy
Subchapter 4.117
references/kv/README.mdMarkdown3 KBView on GitHub
Globally-distributed, eventually-consistent key-value store optimized for high read volume and low latency.
KV provides:
Use cases: Config storage, user sessions, feature flags, caching, A/B testing
| Need | Recommendation |
|---|---|
| Strong consistency | → Durable Objects (opens in a new tab) |
| SQL queries | → D1 (opens in a new tab) |
| Object storage (files) | → R2 (opens in a new tab) |
| High read, low write volume | → KV ✅ |
| Sub-10ms global reads | → KV ✅ |
Quick comparison:
| Feature | KV | D1 | Durable Objects |
|---|---|---|---|
| Consistency | Eventual | Strong | Strong |
| Read latency | <10ms | ~50ms | <1ms |
| Write limit | 1/s per key | Unlimited | Unlimited |
| Use case | Config, cache | Relational data | Coordination |
wrangler kv namespace create MY_NAMESPACE
# Add binding to wrangler.jsonc// Write
await env.MY_KV.put("key", "value", { expirationTtl: 300 });
// Read
const value = await env.MY_KV.get("key");
const json = await env.MY_KV.get<Config>("config", "json");| Method | Purpose | Returns |
|---|---|---|
get(key, type?) | Single read | string | null |
get(keys, type?) | Bulk read (≤100) | Map<string, T | null> |
put(key, value, options?) | Write | Promise<void> |
delete(key) | Delete | Promise<void> |
list(options?) | List keys | { keys, list_complete, cursor? } |
getWithMetadata(key) | Get + metadata | { value, metadata } |
| Task | Files to Read |
|---|---|
| Quick start | README → configuration.md |
| Implement feature | README → api.md → patterns.md |
| Debug issues | gotchas.md → api.md |
| Batch operations | api.md (bulk section) → patterns.md |
| Performance tuning | gotchas.md (performance) → patterns.md (caching) |