Versioned KV
KV now supports atomic conditional writes: create a key only if it doesn't exist, or update and delete a key only if it hasn't changed since you read it.
It works through versions. Every read returns the entry's current version.
Pass it back as ifVersion and the write only happens if the key is still at
that version. Pass ifVersion: null and the write only happens if the key
doesn't exist yet:
const jobs = kv("jobs")
// Exactly one worker can create the key
const claimed = await jobs.setJson(jobId, { worker: workerId }, {
ifVersion: null, // only write if the key doesn't exist
ttl: 300,
})
if (!claimed) return // another worker already claimed itWhen the condition fails, the write returns false instead of throwing. That
is all you need for locks, counters, and idempotency keys that stay correct
when two workers race.
Under the hood each cloud's native primitive does the work: DynamoDB condition expressions, Firestore preconditions, Azure Table Storage ETags, and an atomic upsert in local dev. Identical semantics everywhere, in TypeScript and Rust.