JAlcocerTech E-books

The Cloudflare Stack: Pages, Workers, KV, R2 and D1

Cloudflare sells many products. For a small business’s web presence, five matter, and you may only need three of them.

The five building blocks

ProductWhat it isThink of it asI use it for
PagesStatic site hosting with git builds or direct uploadYour web server and CDNAll the sites
WorkersSmall functions at the edge, triggered by HTTP or cronYour API and schedulerThe forms backend, daily rebuilds
KVGlobal key-value store, eventually consistentYour inboxLeads, the do-not-send list, spam
R2S3-compatible object storage, no egress feesYour file storeNot yet (see below)
D1SQLite as a service, bound to WorkersYour databaseNot yet (see below)

Around them, Cloudflare also handles DNS, TLS certificates, caching, DDoS protection, cookieless analytics, and Tunnels, which let a server at home be reachable without opening a port on your router.

Pages: static first

Every site is a static build (Astro or Hugo) served by Pages. A page is a file, so there’s no server to patch, nothing to scale and almost nothing to attack.

Two ways to deploy:

  • Git-connected: Pages builds on every push. Good for content sites. It also gives you deploy hooks, URLs that trigger a build, which is how scheduled posts work (chapter 5).
  • Direct upload: you build locally and upload dist/ with wrangler pages deploy. Good when the build needs things the Pages builder doesn’t have. The trap: you deploy whatever is in your folder, drafts included. Build from a clean git worktree of a commit instead.

Pages also reads two plain files from your build output: _headers (security and caching headers) and _redirects. Commit them, or a clean build ships without them.

Workers: the only code that runs

A Worker is a fetch(request, env, ctx) function. Mine is about 1,450 lines of TypeScript in eleven files, with no framework, using web standards (Request, Response, URL, crypto.subtle). The only Cloudflare-specific parts are the bindings in env (the KV namespace, secrets) and ctx.waitUntil, which lets work continue after the response is sent.

The key trick is zone routes. A Worker route like www.example.com/api/forms/* takes precedence over the Pages site on the same hostname. So every site can post its forms to its own domain, same-origin, with no CORS and no third-party URL in the HTML. Chapter 2 builds on this.

A second Worker runs on a cron trigger and nothing else: once a day it calls each site’s deploy hook so future-dated posts go live on their day.

KV: an inbox, not a database

KV stores values under keys, replicated globally. Reads are fast everywhere; writes take up to a minute to be visible everywhere. Two features make it a good lead inbox:

  • Per-key expiration. Every lead is written with a TTL that matches the privacy policy (90 days for a qualifier, two years for an enquiry). The platform deletes it on time; no cleanup job to forget.
  • Metadata on keys. A small JSON object stored with each key, returned by list(), enough to show a list of leads without reading every value.

Keys are designed to be listed by prefix:

<site>:<kind>:<iso-timestamp>:<uuid>    a lead
suppress:<sha256 of the email>           the do-not-send list
spam:<site>:<iso>:<uuid>                 blocked submissions, kept briefly
report:<id>                              pending audit report requests

What KV is bad at: querying. “All growth-site leads with marketing consent from last month” means listing and reading keys. That’s fine for tens of leads a month. For real queries, mirror the data into SQLite (chapter 4) or use D1.

R2: when you have files

R2 is object storage with an S3-compatible API and no fees for downloads. I don’t use it yet, because nothing in the engine stores files. It’s where these would go:

  • Generated reports that are too big or too long-lived for KV.
  • Downloadable assets (PDF versions of ebooks, lead-magnet files) behind signed or expiring links.
  • Off-site backups of the homelab’s SQLite database: a nightly copy to an R2 bucket is a two-line job with any S3 client.

D1: when you need SQL at the edge

D1 is SQLite that a Worker can query. Reach for it when the Worker itself needs to query leads: deduplicating by email across sites, rate limits per address, or an admin page that filters and sorts. As long as only humans query the data, and they can do it from a mirror at home, KV plus a SQLite copy is simpler.

The honest decision rule I use:

NeedUse
Store a record, read it back by key, expire itKV
Store files, serve downloadsR2
The edge code must filter, join or countD1
Humans need reports and analysisA SQLite mirror you own

What it costs

At the time of writing, the free plans cover a small business comfortably: Workers allow 100,000 requests a day, KV 1,000 writes a day, and Pages 500 builds a month with unlimited static traffic. One daily rebuild per site and tens of leads a month are nowhere near those limits. Check the current limits before you rely on them; free tiers change.

The only bill in my engine is email delivery (chapter 3).

The trade-off

Everything public runs on someone else’s infrastructure. That’s the point, and it’s the risk. Chapter 6 covers the rules that keep the exit cheap, and chapter 7 shows what the exit looks like.

Next: one forms backend for every site.