BACK_TO_COURSE
Where the Data Goes: D1, KV, R2
Pick Storage with Three Questions
Three Boxes
- Is it a file? Photo, PDF, export, audio. Use R2.
- Is it the truth? Orders, users, anything you'd cry about losing. Use D1.
- Is it a copy you can rebuild, read a lot, and stand being a minute old? Use KV.
| D1 | KV | R2 | |
|---|---|---|---|
| What it is | SQL database (SQLite) | Global key lookup | File storage |
| Good at | Queries, relationships, rules | Fast reads everywhere | Big files, free downloads |
| Bad at | Huge single databases | Anything that must be current | Searching inside |
| Fresh after a write? | Yes, on the main database | Can lag 60 seconds or more | Yes |
| Size limit | 10 GB per database (500 MB free) | 25 MiB per value | Built for large files |
| Free each day or month | 5M rows read, 100K written a day | 100K reads, 1,000 writes a day | 10 GB stored, 1M writes and 10M reads a month |
Wire them up in wrangler.jsonc:
{
"name": "my-app",
"main": "src/index.js",
"compatibility_date": "2026-08-04",
"observability": { "enabled": true },
"kv_namespaces": [{ "binding": "CACHE", "id": "<id>" }],
"r2_buckets": [{ "binding": "FILES", "bucket_name": "my-files" }],
"d1_databases": [
{ "binding": "DB", "database_name": "my-db", "database_id": "<id>" }
]
}
You can mix them. A receipt app keeps the order row in D1, the PDF in R2, and the exchange rates in KV. The row stores the file's key, not the file.
TIP
You don't have to use D1. domdhi.com keeps its main data in Postgres (Supabase), caches outside lookups in KV, and puts uploads in R2. If you already have a database that works, keep it. Cloudflare's Hyperdrive exists to connect Workers to one.
QUIZ
Customers upload receipts as PDFs, and you need to look them up by order. What goes where?
Sign in to track lesson progress and earn a certificate.SIGN IN TO SAVE PROGRESS