Deno KV Leader Election
Distributed mutual exclusion and leader election across processes, isolates, and Deploy regions on Deno KV alone. withLock(kv, name, fn) is a distributed compare-and-swap through kv.atomic().check(versionstamp).set() with an expireIn lease so a dead holder cannot wedge the lock, and kv.watch() turns poll-until-free into notify-on-release for instant handoff. onLeader(kv, name, cb) runs an election loop that renews at half the TTL and aborts an AbortSignal the moment leadership is lost. Because KV expiry marks the earliest deletion, not the exact moment, each lease also stores its own expiresAt in the value and waiters validate it, so failover latency stays bounded by the TTL even when the expiry sweep lags. Zero dependencies.
npx shadcn@latest add https://ui.aryank.space/r/deno-kv-leader-election.jsonInstalls from ui.aryank.space. To add it by hand, copy the files in Files below, or register the @compronents namespace via the docs.
withLock is a distributed compare-and-swap with an expireIn lease; candidates contend, one wins the lock, and the losers are held off rather than the lock wedging if the holder dies.