Skip to content

fix(api): one D1 round trip for role lookups, configurable perf port, realistic write scenario (#1076) - #1082

Merged
fdemusso merged 2 commits into
devsfrom
fix/issue-1076-concurrent-writes-queue
Nov 30, 2026
Merged

fdemusso merged 2 commits into
devsfrom
fix/issue-1076-concurrent-writes-queue

Conversation

@fdemusso

Copy link
Copy Markdown
Owner

Resolves #1076

Root cause

The reported tail latency was not the Worker. Driving the same writes from plain Node (no k6, no Docker) against the same wrangler dev Worker: 3 content + 1 public writers for 200 s gave 5979 requests with none slower than 3 s and no failures; 20 content + 10 public writers for 120 s (the issue's load) gave 4851 requests, no 5xx, no timeout, max 1.19 s (the only non-201 responses were 409s caused by the probe's own idempotency keys colliding). The same k6 scenario run through the grafana/k6 image on Windows (host.docker.internal) leaves 3-4 requests per minute hanging until k6's 60 s timeout, regardless of route and with the dev health checks in apps/api/src/index.ts disabled. That is the p99 of 60 s and the "timeouts" of the issue: a runner artifact, not queueing in D1.

What is real: an authorized write costs about 10 sequential D1 round trips (registry version, user, assignments, roles, role permissions, slug check, insert, plus activity log, analytics and automations lookup in waitUntil), and D1 serialises them per database. D1RoleRepository.findByIds read roles and role_permissions as two sequential queries on every authorized request.

Fix

  • apps/api/src/shared/db/repositories/d1-role.repository.ts: findByIds reads both tables in one db.batch (one round trip; no cache, no new state, same result). Lives in the shared D1 repository layer, which already owns this query.
  • tests/k6/run.mjs: --port N / BEECH_PERF_PORT (default stays 8798) so perf runs from several worktrees no longer share one fixed port.
  • tests/k6/scenarios/04_write_contention.js: write ceiling capped at 3 + 1 writers (D1 serialises writes, Beech targets read-heavy sites).
  • docs/testing.md: documents --port, the new ceiling, and that on Windows a native k6 should be preferred over the Docker image.

Caching the registry version or effective permissions, a cached "no automations" flag and in-memory analytics batching were considered and rejected: they trade cross-instance consistency or durable counts for a small saving.

Why existing tests missed it

d1-role.repository.test.ts ran against a real SQLite D1 and asserted results, never the number of round trips. The new case pins it.

Blast radius

findByIds callers: findById, resolveEffectivePermissions (every authorized request), rbac/assignments.ts, rbac/invitations.ts, rbac/invitations.public.ts; the returned records are unchanged, covered by 858 related unit and integration tests. No persisted state is written differently: nothing to backfill, no cached copies.

Evidence

  • Regression test: apps/api/src/shared/db/repositories/d1-role.repository.test.ts › "findByIds reads the roles and their permissions in a single D1 round trip" (unit, real SQLite D1 with a spy on batch)
  • RED on origin/devs: the case fails on expect(roundTrips).toEqual(['batch']) (no batch call)
  • Revert check: fails again with the fix reverted, passes with it
  • GREEN: pnpm --filter @beechcms/api exec vitest run --project unit src/shared/db/repositories/d1-role.repository.test.ts (11 passed)
  • The latency itself is not asserted (wall-clock is noisy: two smoke runs of the same scenario gave p95 254 ms before and 187 ms after, with lower throughput the second time); the bound is the round-trip count.

Verification

Gate Where Result
Related tests + changed-file coverage (test-coverage-diff --base origin/devs) local ✅ 858 passed (112 files), 92.3% lines on d1-role.repository.ts
Type-check + lint (--filter=...[origin/devs]) local ✅ 34/34, plus eslint on the changed k6 files
Test placement local ✅
Unit / Integration / Flow tiers (full) CI see checks
pnpm beech test:perf write --profile load local not a pass criterion: the Docker k6 path on Windows reports the phantom timeouts described above

Follow-ups

Real per-write cost is ~10 D1 round trips; reducing it further (permissions in one query, a registry-version read policy) changes consistency guarantees and was left out on purpose.

fdemusso and others added 2 commits November 30, 2026 00:13
… realistic write scenario (#1076)

findByIds read roles and role_permissions in two sequential queries on every authorized request; one batch now. The perf runner takes --port/BEECH_PERF_PORT so runs from several worktrees do not share port 8798, and the write scenario is capped at 3+1 writers (D1 serialises writes). Docs note that k6 in Docker on Windows reports phantom 60 s timeouts.
@fdemusso
fdemusso merged commit 8e866a9 into devs Nov 30, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant