In Workers KV, why can a feature flag an admin just switched off still read as on in other locations, and what does cacheTtl change?
answer
- central stores, cached at the edge
- a location keeps what it last read
- not-found answers are cached too
- default 60, minimum 30
basics
~20 sWorkers KV writes go to central stores while each location serves its cached copy, so other locations may see the old value for 60 seconds or more. cacheTtl sets that cache time: 60 seconds by default, 30 at minimum.
solid answer
~40 sKV keeps data in a few central stores and caches it in the locations that read it. A write is usually visible at once where it was made, though that is not guaranteed, but a location that recently read the key keeps serving its cached copy until it times out, so the change can take 60 seconds or more to show elsewhere. Lookups that found nothing are cached as well, so a newly created key can read as `null` for the same window. The `get()` option `cacheTtl` sets how long a result is cached at the location: 60 seconds by default, 30 at minimum (lowered from 60 in January 2026). Raising it cuts cold-read latency for rarely changed data and lengthens staleness; 30 seconds shortens the window but never makes KV strongly consistent.
code
javascript · 10 linesexport default {
async fetch(request, env) {
// Changes weekly: let each location keep its copy for an hour.
const theme = await env.FLAGS.get("site:theme", { cacheTtl: 3600 });
// Kill switch: the shortest window KV allows.
const checkoutOn =
(await env.FLAGS.get("flags:checkout", { type: "json", cacheTtl: 30 })) ?? false;
return Response.json({ theme, checkoutOn });
},
};go deeper
Remember that KV reads can be stale for a minute or more after a write in another location, and that cacheTtl defaults to 60 seconds.
Explain central stores plus per-location caching, why negative lookups are cached, and how cacheTtl trades read latency against staleness, with 30 seconds as the floor.
Diagnose a flag that will not switch off everywhere by asking which locations read it recently and with which cacheTtl, and design rollouts that tolerate both values.
Set a staleness budget per class of data and refuse KV for anything whose correctness depends on read-after-write across locations.
## Where a KV write goes and where a read comes from Workers KV stores each namespace in a **small number of central data stores**. Cloudflare runs Workers in hundreds of locations, and KV does not push every write to all of them. Instead: - A **write** (`put()` or `delete()`) is sent to the central stores. - A **read** (`get()` or `getWithMetadata()`) is served from cache when a copy exists nearby, in the location or a regional tier (a **hot read**); otherwise it falls through a central tier to the central stores (a **cold read**). - After a read, the result stays cached at that location for a while, and KV refreshes popular keys in the background so hot reads do not stall. This design is why KV reads are fast everywhere, and it is also why KV is **eventually consistent**: a location answers from what it cached, not from the latest write. ## The staleness window in numbers The documentation states the behaviour carefully: 1. At the location where the write happened, the change is **usually** visible immediately, but this is not guaranteed and should not be relied on. 2. In other locations, changes **may take up to 60 seconds or more** to be visible, as their cached copies time out. 3. Visibility takes longer in locations that **recently read a previous version** of the key, because they hold that version in cache. 4. **Negative lookups are cached too.** If Frankfurt read `flags:checkout` and got `null`, then London created the key, Frankfurt can keep answering `null` for the same window. For the reserved scenario, an admin turning a flag off during an incident, this means many locations see the change within about a minute, but the documentation promises no fixed bound, and a location that had just read the flag can take longer. The admin's own next request may land on a different location, or on one with a cached copy, so even the person who made the change can briefly see the old value. ## What cacheTtl controls `cacheTtl` is an option on `get()` and `getWithMetadata()`: ```javascript const theme = await env.FLAGS.get("site:theme", { cacheTtl: 3600 }); ``` It is the number of **seconds a KV result is cached at the location it was read from**. | Setting | Effect | |---|---| | omitted | 60 seconds, the default | | `30` | the minimum; shortest staleness window, more cold reads | | `3600` or more | fewer cold reads for write-once or write-rarely data, longer staleness | | below `30` | rejected with an error | The maximum is `Number.MAX_SAFE_INTEGER`. A long `cacheTtl` is the documented way to improve KV performance for keys that are read rarely and change rarely, such as a site theme. It is the wrong choice for a kill switch, because other locations will not see an update until their cached copy expires. Two interactions are worth knowing: - **Expiration wins.** A key with an `expiration` or `expirationTtl` is deleted at its time even if a reader asked for a longer `cacheTtl`. - **Each read sets its own TTL.** The value applies to the cached result of that read at that location, so a team that reads one key with different `cacheTtl` values in different code paths can get different staleness windows for it. ## Designing around the window None of this is a bug to fix; it is the price of edge-cached reads. The practical rules: - Accept a staleness budget of about a minute or more for anything in KV, and say so in the runbook for flags that act as kill switches. - Use `cacheTtl: 30` for flags that must change fast, and longer values for configuration that changes weekly. - Make readers tolerate both the old and the new value during a change, for example by adding a new flag before removing an old one. - Do not write a key and then read it back from another Worker expecting the new value. - When a read must reflect the latest write everywhere, the data needs a strongly consistent store such as a Durable Object; the Workers KV documentation describes routing all writes for a key through one Durable Object when write ordering matters. ## How the limit has changed The minimum `cacheTtl` was **60 seconds until 30 January 2026**, when Cloudflare lowered it to 30 seconds for both `get()` and `getWithMetadata()`; the default stayed at 60. Older answers that say "`cacheTtl` cannot go below 60" describe the platform before that change. A namespace created in the private-beta Workers KV Instant mode behaves differently, and the behaviour above describes standard namespaces.
- In Workers KV, a location has never read a key; does it also have to wait 60 seconds to see a new write?Usually not. With no cached copy, its first read goes up to the regional and central tiers, which KV revalidates with the new result when a key is written. The long waits come from locations that cached the previous version, including a cached not-found answer, and keep serving it until that copy times out.
- In Workers KV, does lowering cacheTtl to 30 make reads consistent enough for a counter or a lock?No. It only shortens how long one location reuses its cached result; reads are still eventually consistent, concurrent writes still overwrite each other, and a key still accepts one write per second. Counters and locks need a store with atomic updates, such as a Durable Object.
- In Workers KV, why might an admin who just saved a flag see the old value on the next page load?The admin's next request can be served by a location that cached the old value, or by a different location from the one that handled the write. Same-location visibility is usually immediate but not guaranteed, so admin tools should confirm the write from the API response, not from a read through the edge.
A printed timetable at each station: head office edits the master copy at once, but a station keeps showing its old printout until that sheet is due to be replaced, and a longer reprint interval means fewer trips to head office and older timetables.
- A KV write is pushed to every location before put() resolves
- Setting cacheTtl to 0 makes every get() read the latest value
- A key that does not exist yet is never cached, so creating it shows up instantly
- Raising cacheTtl makes updates propagate faster
- The minimum cacheTtl is 60 seconds
- Changes are guaranteed to be visible immediately in the location that wrote them
Assumes Workers KV as of 2026-10-10: the minimum cacheTtl was lowered from 60 to 30 seconds on 30 January 2026, and the default is still 60.