Lesson 17 / 32

Edge Key-Value Stores and Consistency

Pick between eventually consistent edge KV and strongly consistent coordination primitives for a given piece of data.

By · 15 years in web development

Fast reads, slow-to-spread writes

Edge platforms offer data stores replicated close to users. A globally distributed key-value store, such as Cloudflare Workers KV or Vercel Edge Config, is optimised for data that is read very often and written rarely: feature flags, configuration, redirect tables, cached API results. Reads from a nearby location are fast, but writes propagate gradually, so the store is eventually consistent: after a write, some locations may keep serving the old value for a while (Cloudflare documents this as up to about a minute, sometimes longer). That makes such stores wrong for counters, seat inventory or anything needing read-your-writes. For coordination, platforms offer stronger primitives. Cloudflare Durable Objects provide one uniquely addressed instance with strongly consistent storage, so all requests for a given chat room or shopping cart are processed in one place, in order. Edge SQL databases with read replicas also exist, but writes still go to a primary located somewhere specific.

Cache-aside with a KV binding

The first call misses and goes to the origin; the second is served from KV. The expirationTtl option (in seconds) makes stale data age out on its own.

// Cache-aside with an edge KV store (env.CACHE mocked here with a Map).
const store = new Map();
const env = { CACHE: {
  async get(key) { return store.get(key) ?? null; },
  async put(key, value, opts) { store.set(key, value); },
} };

async function fetchRatesFromOrigin() {
  console.log('  (origin call)');
  return JSON.stringify({ USD_INR: 83.1 }); // sample value
}

async function getRates(env) {
  let rates = await env.CACHE.get('fx-rates');
  if (rates === null) {
    rates = await fetchRatesFromOrigin();
    await env.CACHE.put('fx-rates', rates, { expirationTtl: 300 }); // seconds
  }
  return rates;
}

console.log('first :', await getRates(env));
console.log('second:', await getRates(env));

Output:

  (origin call)
first : {"USD_INR":83.1}
second: {"USD_INR":83.1}

Notice boards versus the warden's register

A circular pinned on every hostel block's notice board is quick to read anywhere, but a correction takes time to reach all boards. Room allotment is kept in the warden's single register, so two students can never get the same bed. KV is the notice boards; a Durable Object is the register.

Quick check: Which data is the best fit for an eventually consistent edge KV store?

  • A feature-flag configuration updated a few times a day
  • The remaining seat count for a flash-sale train booking
  • A bank account balance
  • A counter that must give every user a unique ticket number
Answer

A feature-flag configuration updated a few times a day — Rarely written, often read data tolerates a short propagation delay. Seat counts, balances and unique counters need strong consistency.