Cloudflare D1 vs KV β When to Use What
Cloudflare gives you two storage options: D1 (SQL database) and KV (key-value store). They solve different problems. Here's when to use each β based on real patterns from our production applications.
At a Glance
| Feature | D1 | KV |
|---|---|---|
| Type | Relational (SQLite) | Key-value store |
| Query language | Full SQL | get/put/list/delete |
| Read latency | 5-30ms | <1ms (cached) |
| Write latency | ~30ms | ~500ms (eventual) |
| Consistency | Strong (single region) | Eventually consistent |
| Best for | Structured data, queries | Caching, sessions, flags |
| Free tier | 5M reads/day, 100K writes | 100K reads/day, 1K writes |
Use D1 When...
You need to query by multiple fields: "Show me all emails where category='contact' AND read=0 ORDER BY created_at DESC LIMIT 20" β this is SQL territory.
You need aggregations: COUNT(*), SUM, AVG, GROUP BY β analytics, reporting, leaderboards.
You need relationships: Users have posts, posts have comments β foreign keys, JOINs.
You need strong consistency: A write must be immediately visible to the next read (financial data, counters, sequences).
Use KV When...
You know the exact key: Session lookup by token, feature flag by name, user preference by UUID β O(1) access.
Reads massively outnumber writes: Config values, cached API responses, static content that rarely changes.
You need global low-latency reads: KV replicates globally. Sub-millisecond reads from anywhere in the world.
You need TTL expiration: Temporary data like rate limit counters, cache entries, or OTP tokens that auto-delete.
The Combined Pattern (Best of Both)
π‘ Our Production Pattern
Write to D1 (source of truth) β Cache the result in KV with a TTL β Reads hit KV first (fast) β If KV misses, query D1 (slower but always fresh) β Update KV with the result. This gives you SQL query power with global KV read speed.
On ScrabbleWordsFinder, we use D1 for all structured data (emails, clicks, games, leaderboards, rankings) and KV for admin session tokens and cached API responses.
Real Examples from Our Projects
Contact form β D1
Structured data, filterable by category/status, editable via admin CRUD.
Admin session β KV
Lookup by session token, TTL expiration (7 days), global availability.
Click analytics β D1
20-field records, GROUP BY queries, time-series filtering, aggregation.
Crypto prices β KV
Cached API response with 30-second TTL. Fast reads, infrequent updates.
Building on Cloudflare?
We architect and deploy full-stack applications on Cloudflare's edge. D1, KV, Workers AI β the full stack.
Let's Build Together β