@upstash/lock

The button takes a lock and holds it for 10 seconds. While it is held, nobody else can take it. Open this page in a second tab and try.

Unlocked

Nobody holds the lock.

lock id: · shared by your tabs

redis commands

# press the button

what this demo runs
import { Lock } from "@upstash/lock";
import { Redis } from "@upstash/redis";

const lock = new Lock({
  id: "",
  redis: Redis.fromEnv(),
});

if (await lock.acquire()) {
  await doWork(); // you hold the lock
  await lock.release();
} else {
  // someone else holds it
}