Repository navigation
fix(api): one D1 round trip for role lookups, configurable perf port, realistic write scenario (#1076) - #1082
Merged
Merged
Conversation
… 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.
9 of 11 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 devWorker: 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 were409s caused by the probe's own idempotency keys colliding). The same k6 scenario run through thegrafana/k6image 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 inapps/api/src/index.tsdisabled. 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.findByIdsreadrolesandrole_permissionsas two sequential queries on every authorized request.Fix
apps/api/src/shared/db/repositories/d1-role.repository.ts:findByIdsreads both tables in onedb.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 nativek6should 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.tsran against a real SQLite D1 and asserted results, never the number of round trips. The new case pins it.Blast radius
findByIdscallers: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
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 onbatch)expect(roundTrips).toEqual(['batch'])(no batch call)pnpm --filter @beechcms/api exec vitest run --project unit src/shared/db/repositories/d1-role.repository.test.ts(11 passed)Verification
test-coverage-diff --base origin/devs)d1-role.repository.ts--filter=...[origin/devs])pnpm beech test:perf write --profile loadFollow-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.