Skip to content

Supported multi-process serving on one host: auth SQLite pragmas, database rate limit, declaration fence, setup refusal, multi-process harness (#902 item 4) #927

Closed

Description

@jimhoyd

Follow-up to #902 item 4, which says one serving process per store database is supported but not enforced. This is an evaluation on 036e86d; items marked (unverified) were not reproduced.

What happens today with N urlcode serve processes on one host sharing the SQLite files

  • Store invariants hold. Idempotency-Key, intervals, transfers, quotas and the audit backlog are each checked in one BEGIN IMMEDIATE transaction.

  • Store writes do not scale, and waiting blocks the event loop. Measured on macOS, Node 26.10, WAL and FULL sync:

    Processes Commits/s
    1 4.4k
    2 6.3k
    4 6.2k
    8 6.7k

    Single stalls reached 1.4 s, and the 2 s busy timeout answers 503. With a real Linux fsync the total ceiling stays about 250 commits/s whatever N is (unverified). Reads scale.

  • Auth database has no WAL and busy_timeout 0 (packages/auth/src/auth.ts:63).

    • A second writer gets "database is locked" at once, and a read during another process's commit fails. authorize() turns that into a false 401.
    • This already affects urlcode-auth create-user beside a running server.
  • Better Auth's rate limit is per process (an in-memory Map), so sign-in 10/min becomes 10·N. rateLimit.storage: 'database' is atomic, but urlcode-auth migrate never creates its table, so it can't be enabled today.

  • Processes on different declarations or releases break invariants.

    • An older process keeps writing rows the newer declaration forbids (intervals, transfer min, keys), and old SQL runs against a migrated schema.
    • The documented blue/green deploy (docs/OPERATIONS.md) already runs two servers on one database, which contradicts STORE.md.
  • Throttle and origin cache are per process.

  • Audit drain is correct (INSERT OR IGNORE on the id, ack by id, polled every 1 s), so STORE.md's "woken only in the writing process" overstates it. Edge case: a stalled drainer can re-insert an event already pruned past retention (unverified).

  • The race tests use threads, not processes, so cross-process locking is never exercised.

Plan

Supported topology: N processes or containers on distinct ports behind a proxy, all on one release, on local disk. node:cluster stays unsupported.

  • S auth: open auth.sqlite with timeout: 2000, WAL and synchronous=FULL; decide whether Better Auth transactions need BEGIN IMMEDIATE or a retry.
  • S auth: rateLimit.storage: 'database' by default; urlcode-auth migrate creates the table.
  • M store: a declaration and schema fence.
    • Activation records each collection's declaration fingerprint and the schema version.
    • Every write transaction checks both inside BEGIN IMMEDIATE, and answers 503 storage_unavailable on a mismatch.
  • S-M refuse unsupported setups at activation:
    • a network filesystem (fs.statfs);
    • a peer on another host or boot id, detected through a lease/heartbeat table;
    • warn that throttle and caches are per process.
  • S audit: a single drainer, through a lease.
  • M Linux CI harness: 3 child server processes on one database set. It covers:
    • the race suites run across processes;
    • each audit event exactly once and in order;
    • a sign-in burst with no false 401s and a global limit;
    • a stale declaration gets 503;
    • kill -9 during writes, then an integrity check.
  • S docs: STORE, OPERATIONS (blue/green relies on the fence), CAPACITY, the throttle and cache pages, OBSERVABILITY.

Not planned: SQLite-backed throttle counters. They would put a write transaction on every throttled request, reads included.

Stays unsupported: multiple hosts

  • WAL needs the -shm file on one kernel, and SQLite says WAL does not work over network filesystems.
  • fcntl locking over NFS, SMB or EFS is unreliable.
  • Outbox ordering assumes one clock.

Multiple hosts would need a different storage backend and a shared limiter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions