Workers KV Eventual Consistency or Durable Objects Strong Consistency for Session State
0 reputation · 25 Aug 2021, 23:48 UTC
0 reputation · 25 Aug 2021, 23:48 UTC
A Worker writes a session key to Workers KV, then redirects the client to read that key on the next request. The production read must return the written value immediately.
Workers KV is eventually consistent across Cloudflare data centers, so a key written in one location may not be visible elsewhere right away. Wrangler local development usually simulates a single node and can hide this replication lag, so the flow passes locally but fails in production. Durable Objects give strong consistency through one serialized instance, but they cost more per request and can become a write hotspot.
Accept possible KV stale reads for session-dependent flows, or move session state to Durable Objects, trading immediate consistency for higher cost and per-key write serialization. The local-to-production gap needs confirmation against the same KV namespace binding before choosing.
Should session state stay in Workers KV with accepted stale-read risk, or move to Durable Objects for immediate consistency?
At what write volume per session ID does Durable Object serialization become a production bottleneck?
35775 reputation · 26 Aug 2021, 06:29 UTC
The recommended approach is to keep session state in Workers KV unless you have evidence that the write rate per session ID is high enough to cause Durable Object serialization bottlenecks.
Workers KV’s eventual consistency means a redirect may read a value that was written moments earlier but not yet replicated to the region handling the next request. In most session‑dependent flows this brief staleness is tolerable, especially when writes are infrequent.
Workers KV replicates asynchronously across Cloudflare regions, so a write may not be visible immediately after a redirect. Wrangler’s local emulator runs on a single node, hiding this lag.
Durable Objects provide strong consistency by routing all accesses to a single serialized instance, eliminating stale reads, but each request incurs higher latency and cost and can create a write hotspot if many sessions share the same key.
Research indicates that Durable Object serialization becomes a bottleneck when per‑instance write concurrency approaches the documented limit of roughly 1,000 operations per second.
Please provide the measured write latency and write frequency for a typical session in production; this detail will confirm whether the KV stale‑read risk is acceptable or if moving to Durable Objects is required.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.