Skip to the workspace
Pebellslearn by building
Workers and Workers KV  ›  Capstone Project

Workers KV — Fast Reads, Patient Writes

Key-value Operations 15 minWorkers KV stores values by key for very fast global reads, with metadata, expiry and paged listing, but writes spread to other locations slowly, so it suits data that tolerates brief staleness.

cloudflare / Workers KV — Fast Reads, Patient Writes

Explore each scene at your own pace.

01 · The rule

The rule

Workers KV — Fast Reads, Patient Writes

Hoplink is the perfect KV workload: a link like cat is created once, then read thousands of times from all over the world. Workers KV is a global key-value store built for exactly that pattern. Using it well means knowing both its four operations and the one promise it does not make.

The rule

Read often, write rarely, and accept that writes spread slowly.

The binding env.LINKS gives you four core operations:

// write, with optional expiry and metadata
await env.LINKS.put("cat", "https://example.com/all-about-cats", {
  expirationTtl: 60 * 60 * 24 * 30,        // 30 days, in seconds
  metadata: { owner: "priya", created: "2026-09-01" },
});

const target = await env.LINKS.get("cat");            // string or null
const { value, metadata } = await env.LINKS.getWithMetadata("cat");
await env.LINKS.delete("cat");

// a second index key per link, like "priya:cat", lets you list one owner's links
const page = await env.LINKS.list({ prefix: "priya:" });
// page.keys, page.list_complete, page.cursor

What each part means:

  • Keys and values. Keys are strings; values can be text, JSON (get(key, "json")), or binary data.
  • Metadata is a small JSON object stored beside the value. list() returns it with each key, so you can show "created by priya" without reading every value.
  • Expiry. expirationTtl (seconds from now, at least 60) or expiration (a timestamp) makes KV delete the key for you.
  • list() returns keys in pages. Keep calling with cursor until list_complete is true.
  • Bulk operations. Wrangler can load thousands of pairs at once with wrangler kv bulk put links.json --binding LINKS, handy for importing links from an old system.

The missing promise is strong consistency. KV caches values in the locations that read them. After a put, a reader in the same location usually sees the change quickly, but other locations can keep serving the old value for up to about a minute. Each key also accepts only about one write per second.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.