Cluster cron: one leader, one tick

Redis-backed leader election visualised. createLeader elects exactly one worker across the cluster; live.configureCron({ leader }) gates every cron tick on the lease so a 1Hz live.cron fires once per second cluster-wide instead of once per worker.

This instance
...
Lease key
...
Initial leader status
follower
Current cron leader
...

Recent ticks (newest first)

cap 30

Waiting for the first cron tick...

Instances seen

Waiting...

See the takeover

Single-instance dev shows one steady leader (this worker is always elected). To watch a real cluster transition, start a second prod server on a different port pointing at the same Redis:

npm run build
DATABASE_URL=postgres://postgres:postgres@localhost:5434/stickynotes \
  REDIS_URL=redis://localhost:6380 \
  PORT=3021 HOST=127.0.0.1 \
  node build/index.js

Open both http://127.0.0.1:3020/demos/cluster-cron and http://127.0.0.1:3021/demos/cluster-cron. The recent-tick log on both pages should show the SAME instanceId (the elected leader). Stop one server with Ctrl-C; within ten seconds (renewMs) the surviving sibling acquires the lease and the log on the live page flips to the new instanceId.

The /metrics endpoint exposes leader_acquired_total, leader_renewals_total, leader_lost_total, and svelte_realtime_cron_total{status}. Scrape both servers during the takeover window and the counters tell the same story the page shows.