Skip to content

Lesson 6 of 9 · 27m

Workers KV, Durable Objects, and Choosing the Right State Primitive

Match Cloudflare state primitives to access patterns, consistency requirements, hot keys, and coordination problems without overbuilding.

The easiest way to misuse an edge platform is to treat every storage primitive as “a database in the cloud.” Workers KV, Durable Objects, R2, queues, and external databases solve different problems. Choosing well makes the system feel simple. Choosing badly creates consistency surprises, hot-key bottlenecks, and operational work you did not budget for.

Workers KV: distributed read-mostly configuration

KV is designed for high-read, low-write data that tolerates propagation delay: configuration, feature flags, routing tables, lightweight metadata, and cached reference data.

const flags = await env.CONFIG.get("checkout-flags", "json")
if (flags?.newTotalsPipeline) {
  return handleNewTotals(request, env)
}

Do not use it for rapidly changing counters, inventory, sessions requiring immediate global revocation, or per-user high-frequency writes. A write can take time to propagate globally, and hot writes are not the intended shape.

Durable Objects: coordination with a home

Durable Objects provide strongly consistent state and coordination within an object instance. They are useful when many requests need ordered access to one logical entity: a room, cart, document session, rate-limit bucket, workflow, or multiplayer lobby.

const id = env.CARTS.idFromName(cartId)
const stub = env.CARTS.get(id)
return stub.fetch(request)

The crucial idea is object identity. Requests for the same object key are coordinated through the same logical object. That gives you serialization without designing your own lock service. It does not create a general-purpose globally consistent database; cross-object invariants still need care.

R2: object storage, not queryable application state

R2 stores blobs: images, exports, backups, large static assets, and immutable artifacts. It is appropriate when the object is addressed by key and delivered or processed as a unit. It is not appropriate when you need indexed queries, transactional mutation, or partial relational consistency.

External databases remain legitimate

Sometimes the right answer from a Worker is a carefully controlled call to a regional or central database. That is not failure. The platform gives you placement and glue; it does not invalidate relational modeling. The design questions become connection management, round-trip count, timeout behavior, and whether reads can be cached or replicated closer.

configuration        -> KV
coordination         -> Durable Object
large immutable blob -> R2
transactional truth  -> database or tightly scoped service
durable async work   -> queue

Hot keys and object granularity

Even coordinated primitives have throughput boundaries. A single global object named inventory turns every purchase into one serialization point. Shard by natural entity where semantics allow it: inventory:event:42, cart:user:918, room:abc123. Do not shard so finely that cross-entity invariants become impossible; choose the object boundary at the invariant boundary.

Sessions deserve special skepticism

Teams often want edge-local sessions because they are fast. But revocation, rotation, device loss, and anomaly response are global security operations. A common compromise is signed tokens verified at the edge plus centralized revocation or short lifetimes. If immediate global revocation is required, design for it explicitly instead of discovering during incident response that propagation is eventual.

Field notes

  • KV is not a cache you control entry-by-entry with strong immediacy; platform propagation behavior is part of the semantics.
  • Durable Object migration, object placement, and client geography affect latency. Test from the regions your users occupy.
  • Do not store large mutable documents in a coordination primitive merely because it is convenient. Separate coordination metadata from bulk content.
  • Write down why each primitive was chosen. Six months later, every storage system looks like a nail to a new hire holding a feature hammer.

Was this lesson helpful?

Sign in to rate