Cloudflare, in pieces
Chapters

Cloudflare / 02

Where
data lives.

Each store makes a different promise about what a reader elsewhere sees after a write. Write from one place, read from another, and find out which answers each one allows.

4 stores · 1 instrument · Sources and limits

Illustration Two readers, one changed value

Reader B reads, reader A writes and is acknowledged, then B reads again.

  • Workers KV. B keeps getting its cached answer, even a cached “missing” for a key read before it existed, until the cache refreshes, which can take 60 seconds or more.
  • R2. Through a binding, B gets the new object right after the acknowledged write. Through a cached URL it gets the old one until the copy is purged.
  • D1. With replication off, or reading the primary directly, B gets the new title. A new unconstrained session may land on a replica that is behind. A read that continues with A’s bookmark waits until the update reaches the replica, then returns the new title.
  • Durable Object. A and B both address room:heron and agree. Change B’s name to room:fox and B reaches a different object with its own state.
Read the mechanism

R2: the bytes

An object is a file’s bytes plus metadata under a key. Reads, writes, deletes and lists are strongly consistent: once a write returns, every reader through R2 gets the new object. A response cached on a custom domain is not R2; purge it, or give the new version a new key. Public access through r2.dev is rate-limited and meant for development; caching needs a custom domain.

Workers KV: the read-mostly value

KV is eventually consistent. A change can take 60 seconds or more to be visible in other locations, and a lookup that found nothing is cached too, so a key you just created can read as missing. Do not use it for anything two readers must agree on now: a seat count, a counter, a permission.

D1: the records

Every write goes to the primary. Read replication is off unless you turn it on, and without the Sessions API every query runs on the primary. A session started with no constraint may run its first query anywhere. A session continued with a bookmark, an opaque token from an earlier query, waits for a replica at least as new as that bookmark.

Durable Objects: the coordinator

Every request for one name in one namespace reaches the same object, with its own private, strongly consistent storage. A different name is a different object, and only that object reads or writes its storage.

Pick by what you keep

Photo bytes: R2
A file has a key and bytes. Put it in an R2 bucket and keep its searchable details in a database.
Searchable records: D1
Titles, owners, and albums fit tables. D1 lets the application query and join them with SQL.
Read-mostly settings: Workers KV
A value looked up by key, read often, and changed rarely fits KV, as long as a reader may see the old value for a while after a change.
A shared live room: Durable Objects
A room needs one place where its decisions are made. A Durable Object keeps the room’s code and its storage together, under the room’s name.

Sources and limits

Cloudflare pages used

Documented and illustrated

Left out on purpose