Architecture¶
burrowDB is PocketBase with its storage and process model changed so that many processes can serve one app. This page describes the pieces and how they work together. The reasons behind the choices are in docs/DESIGN.md.
flowchart TB
clients["Clients<br/>SDKs, browsers, agents"] -->|HTTP, SSE| lb["Load balancer"]
lb --> api1["api replica"]
lb --> api2["api replica"]
lb --> apiN["api replica"]
api1 & api2 & apiN -->|pooled| pgb["pgbouncer x N<br/>transaction mode"]
worker["worker<br/>crons"] -->|pooled| pgb
pgb --> pg[("PostgreSQL<br/>burrow_data, burrow_aux")]
api1 & api2 & apiN -.->|direct: LISTEN/NOTIFY| pg
migrate["burrow migrate<br/>once per deploy"] -.->|direct: DDL, advisory lock| pg
migrate -.->|RECONNECT| pgb
api1 & api2 & apiN & worker --> s3[("S3-compatible storage<br/>files, backups")]
One binary, three roles¶
The image runs one binary, burrow. burrow serve starts the server: the PocketBase engine in
engine/ with burrowDB's cluster wiring from internal/server. BURROW_ROLE picks what the process
does:
| Role | Serves | Runs crons | Use |
|---|---|---|---|
all (default) |
API, dashboard, realtime | yes | One process, like PocketBase. Also applies migrations and settings from the environment at start |
api |
API, dashboard, realtime | never | Any number, behind a load balancer |
worker |
only /livez, /readyz, /api/health, /metrics |
yes, once per cluster | One or more; crons are claimed through leases, so more workers only add failover |
Any other value of BURROW_ROLE stops the process at start, so a typo never starts a process that both
serves traffic and runs every cron.
api and worker processes do not apply migrations (unless BURROW_RUN_MIGRATIONS=true). They report
not ready on /readyz while a registered migration has not been applied.
PostgreSQL: two databases, two connections¶
Two databases. burrow_data (BURROW_DATA_DB) holds collections, records, settings and the
internal tables. burrow_aux (BURROW_AUX_DB) holds request and app logs, like auxiliary.db in
PocketBase. Both are created on first start when the user may (BURROW_ALLOW_CREATE_DATABASE), with
ENCODING 'UTF8' LC_COLLATE 'C' LC_CTYPE 'C' TEMPLATE template0. The C collation compares bytes, as
SQLite does, so sorting, range filters and unique indexes behave as in PocketBase.
Two connections.
- The pooled URL (
BURROW_POSTGRES_URL) carries ordinary queries. In a cluster it points at pgbouncer in transaction mode, so hundreds of replicas share a small number of server connections. - The direct URL (
BURROW_POSTGRES_DIRECT_URL) bypasses the pooler. It carries what a transaction pooler cannot: the realtime bridge'sLISTENconnection (one per replica), the migrate step's DDL and advisory lock,CREATE DATABASE, and backups and restores.
Without a pooler, set only BURROW_POSTGRES_URL; the direct URL falls back to it.
Statement mode. pgx's default prepares statements by name, which breaks behind a transaction pooler.
The engine sets default_query_exec_mode=cache_describe unless the URL names a mode. After a column is
added, a pooled connection can still hold the old description of a statement; dbx sends such a
statement once more outside a transaction, the engine closes idle connections when a collection
changes, and the migrate step asks pgbouncer to RECONNECT (see below).
Outages. When PostgreSQL or pgbouncer cannot be reached, the API answers 503 rather than a
misleading 400 or 404, and the request log keeps the status the client saw. Record list and view reads
retry for up to BURROW_READ_RETRY_BUDGET (default 3 s). Writes are never retried by the server.
Cron jobs: once per cluster¶
PocketBase runs its own jobs (log cleanup, scheduled backups, OTP and MFA cleanup) and yours
(cronAdd in JS, app.Cron().Add in Go) in every process. burrowDB puts a guard in front of the
engine's scheduler:
- On
apireplicas the guard refuses every job. - On
workerandallprocesses, a due job is claimed in the table_burrowCronLeaseswith oneINSERT ... ON CONFLICT DO UPDATEthat succeeds only when the stored minute slot is older and no other process holds the job. The winner runs it; the others skip that tick. - A running job renews its lease every 40 seconds (a third of the 2-minute TTL), so long jobs keep it. A worker that dies frees its jobs after one TTL.
- A job still running from the previous tick on the same process is not started again.
- Process-local housekeeping (the rate limiter's cleanup) runs in every process.
The lease is a row, not a session lock, so it works through pgbouncer and survives a database failover. Losing a lease mid-run does not cancel the job, so jobs should be idempotent. See Hooks at scale.
/metrics exposes burrow_cron_ticks_total{outcome="ran|skipped|error"} per process.
Realtime across replicas¶
Each replica keeps its own server-sent-event (SSE) clients, as PocketBase does. The realtime bridge connects them:
- Each replica has a channel and announces itself in
_realtimeChannelswith a heartbeat every 5 s; a channel is considered alive for 20 s. A replica that stops cleanly says so at once. - Subscriptions are stored in
_realtimeClients, so a client can connect to one replica and send itsPOST /api/realtimesubscription request to another. - When a record changes, the replica that made the change sends the event with
NOTIFYto the replicas whose clients may want it. Each replica checks API rules for its own clients, exactly as PocketBase does. - Payloads over 7,000 bytes go through the
_realtimeOverflowtable; outgoing messages are batched (5 ms window); each local client has a bounded queue, and a client that does not take a message within 10 s is dropped and reconnects.
Collection and settings changes also go over the bridge: whatever made the change (the API, a hook, a migration, a restore), every replica reloads its collection or settings cache.
What clients must do. A client whose replica dies reconnects to another replica within seconds, and
can miss events sent in between. Clients on surviving replicas miss nothing. Refetch what the page shows
when the SDK fires PB_CONNECT after a reconnect.
The bridge is on by default (BURROW_REALTIME_BRIDGE). Turn it off only for a single process.
The migrate step¶
burrow migrate (or burrow migrate up) runs once per deploy, before the replicas serve:
- It takes a PostgreSQL advisory lock keyed by the data database, on the direct connection. A second migrate step waits for it (up to 10 minutes) instead of running the same migrations twice.
- It applies PocketBase's system migrations and your migrations (
pb_migrations, JS or Go) withdefault_query_exec_mode=describe_execon the direct connection, without the realtime bridge. - It writes settings and the first superuser from
BURROW_*variables (see Configuration). Only this step does that, so replicas never race on settings. - If it applied anything, it sends
RECONNECTto every pgbouncer inBURROW_PGBOUNCER_ADMIN_URLS, so no pooled server connection keeps statements described against the old tables.
In Docker Compose it is the migrate service; the replicas start when it has finished. In the Helm
chart it is an init container in every api and worker pod: the lock makes concurrent runs safe, and a
run with nothing to apply only checks.
Migrations must be additive within one release (add a column now, remove the old one in a later release), because old and new replicas run side by side during a rolling update.
Files and backups¶
PocketBase stores uploaded files under pb_data/storage by default. With more than one replica, every
replica must see the same files, so burrowDB deployments put them on S3-compatible storage:
MinIO in Compose and kind, S3 on AWS, GCS through its S3 interoperability API on GCP, and in-cluster
MinIO on Azure. A cluster role (api, worker) with files on local disk logs a warning at start, and
burrow doctor flags it.
Backups use PocketBase's Backups API and dashboard page. On PostgreSQL a backup is the usual zip, with
pg_dump --format=custom archives of both databases and a manifest inside (plus local files, when files
are not on S3). Only one backup or restore runs at a time across the cluster (an advisory lock). A
restore loads each archive into a new database, then swaps names: the old database is kept as
<name>_pre_restore_<time>, every replica reconnects to the restored one and reloads its caches. See
Operations.
Health and metrics¶
| Path | Answers |
|---|---|
/livez |
200 while the process runs: {"status":"ok","role":"api"} |
/readyz |
200 when the database answers SELECT 1, no registered migration is pending, and the replica is not draining; otherwise 503 with the reason |
/api/health |
PocketBase's health endpoint |
/metrics |
Prometheus text: burrow_info, burrow_ready, burrow_cron_ticks_total, burrow_realtime_clients, burrow_db_connections, burrow_db_wait_total, burrow_db_wait_seconds_total |
These paths answer without authentication.
What is different from PocketBase¶
- Raw SQL is PostgreSQL SQL:
$app.db()queries in hooks, view collection queries,burrow sql. The API's filters and sorts are translated and behave as on SQLite. Backtick-quoted identifiers are rewritten to double quotes, and a few SQLite functions are installed as shims (strftime,hex,randomblob,json_valid, thenocasecollation); anything else SQLite-specific must be ported. - Hooks run on every API replica. In-memory state (
$app.store(), globals) is per replica. See Hooks at scale. - Crons run only on workers, once per tick across the cluster.
- Files need S3-compatible storage with more than one replica.
- Rate limits count per replica: PocketBase's limiter keeps counters in memory.
- Backups contain
pg_dumparchives, not SQLite files, and restore without a process restart. - Settings and the first superuser can come from
BURROW_*variables, applied by the migrate step.