Hooks at scale¶
burrowDB runs PocketBase's JavaScript hooks (pb_hooks/*.pb.js) and migrations (pb_migrations/*.js)
unchanged. What changes is that several processes load them at once. A hook that is correct on one
PocketBase process can send an email twice, or keep state that other replicas never see, once you run
three.
What runs where¶
| Hook | Runs on |
|---|---|
onRecord*Request, onRecordCreate/Update/Delete*, other model events |
The replica that handled the write, once per write |
routerAdd, routerUse |
API replicas. Workers answer only the health paths and return 404 for the rest |
cronAdd |
Workers, and processes with role all. Each tick runs once in the whole cluster |
onBootstrap, onServe |
Every process, at start. Keep them idempotent |
onTerminate |
Every process, on shutdown |
API replicas (BURROW_ROLE=api) never run cron jobs. A cron tick is claimed through a lease row in
_burrowCronLeases: the first process to claim a minute slot runs it; the others skip it and count a
skipped tick in burrow_cron_ticks_total. A job still running from the previous tick on the same
process is skipped too. The lease lasts 2 minutes and is renewed while the job runs, so a worker that
dies mid-job frees the job for the next tick on another process.
Rules¶
No shared state in memory. A JS variable or $app.store() lives in one process. Keep anything other
replicas must see in a collection.
Make side effects idempotent. A client or load balancer can retry a request after a replica dies, and a cron job may be cut off and run again on the next tick. Send webhooks and emails with an idempotency key, or record "sent" in a collection in the same transaction as the change.
Finish crons within their interval and tolerate a missed tick.
Use cronAdd, not background loops. A loop started in onServe runs on every replica.
Raw SQL is PostgreSQL. $app.db() talks to PostgreSQL: now(), ||, ILIKE, jsonb operators
such as meta->>'key'. Bind values with {:param} placeholders as before; never build SQL from user
input. Backtick-quoted identifiers are still accepted.
Ship hooks with every process. Hooks load at start. Put them in the image, in the Compose stack's
pb_hooks/ folder, or in the Helm hooks: values, then roll the pods. Every API replica and worker must
run the same hooks.
Examples¶
// once per minute for the whole cluster
cronAdd("expire-sessions", "* * * * *", () => {
$app.db().newQuery("DELETE FROM sessions WHERE expires < now()").execute()
})
// an outbound webhook the receiver can deduplicate
onRecordAfterCreateSuccess((e) => {
$http.send({
url: $os.getenv("WEBHOOK_URL"),
method: "POST",
headers: { "Idempotency-Key": "post-created-" + e.record.id },
body: JSON.stringify({ id: e.record.id }),
timeout: 10,
})
e.next()
}, "posts")
Not built yet¶
The design proposed cluster helpers for hooks ($burrow.lock, $burrow.kv, $burrow.afterCommit).
They do not exist. Use a collection with a unique index as a lock or a key-value store, and
onRecordAfter*Success hooks for work that must only happen after a commit.
The API rate limiter is per replica, not shared: with N replicas the effective limit is up to N times the configured one.
Checking¶
burrow logs --filter 'level >= 4' --per-page 20 # hook errors
curl -s http://WORKER:8090/metrics | grep burrow_cron_ticks_total # ran / skipped / error
Test with at least two API replicas and one worker (burrow up local --replicas 2) before you ship a
hook that sends anything outside.