perf: gateway + DB + frontend optimizations (P8-003) #211

Merged
jason.woltje merged 2 commits from feat/p8-003-performance into main 2026-03-21 12:28:31 +00:00
Owner

P8-003 — Performance Optimization

Summary

Measured and fixed concrete bottlenecks targeting <200ms TTFB and <2s page loads.

Changes

  • DB pool config: max=20, idle_timeout=30s, connect_timeout=5s — eliminates pool exhaustion and indefinite hangs
  • Preferences upsert: Replace 2-query SELECT+INSERT/UPDATE with single INSERT … ON CONFLICT DO UPDATE — 50% fewer DB round-trips, no TOCTOU race
  • DB indexes: Add missing indexes for sessions(user_id), accounts(provider_id, account_id), compound conversations(user_id, archived), compound agent_logs(session_id, tier) — auth hot-path now uses index scans
  • Conversation queries: Add ORDER BY + LIMIT to findAll (200) and findMessages (500) — prevents unbounded full scans
  • Cold-start GC: Make onModuleInit fire-and-forget — removes 100–500ms from startup TTFB
  • Next.js: Enable compress, productionBrowserSourceMaps: false, image avif/webp formats

Docs

Full profiling report in docs/PERFORMANCE.md.

Quality gates

  • pnpm typecheck — 32/32 tasks pass
  • pnpm lint — 18/18 tasks pass
  • pnpm test — 125/125 tests pass

Closes #P8-003

## P8-003 — Performance Optimization ### Summary Measured and fixed concrete bottlenecks targeting <200ms TTFB and <2s page loads. ### Changes - **DB pool config**: `max=20`, `idle_timeout=30s`, `connect_timeout=5s` — eliminates pool exhaustion and indefinite hangs - **Preferences upsert**: Replace 2-query SELECT+INSERT/UPDATE with single `INSERT … ON CONFLICT DO UPDATE` — 50% fewer DB round-trips, no TOCTOU race - **DB indexes**: Add missing indexes for `sessions(user_id)`, `accounts(provider_id, account_id)`, compound `conversations(user_id, archived)`, compound `agent_logs(session_id, tier)` — auth hot-path now uses index scans - **Conversation queries**: Add `ORDER BY + LIMIT` to `findAll` (200) and `findMessages` (500) — prevents unbounded full scans - **Cold-start GC**: Make `onModuleInit` fire-and-forget — removes 100–500ms from startup TTFB - **Next.js**: Enable `compress`, `productionBrowserSourceMaps: false`, `image avif/webp` formats ### Docs Full profiling report in `docs/PERFORMANCE.md`. ### Quality gates - ✅ `pnpm typecheck` — 32/32 tasks pass - ✅ `pnpm lint` — 18/18 tasks pass - ✅ `pnpm test` — 125/125 tests pass Closes #P8-003
jason.woltje added 2 commits 2026-03-19 02:27:39 +00:00
- DB client: configure connection pool (max=20, idle_timeout=30s, connect_timeout=5s)
- DB schema: add missing indexes for auth sessions, accounts, conversations, agent_logs
- DB schema: promote preferences(user_id,key) to UNIQUE index for ON CONFLICT upsert
- Drizzle migration: 0003_p8003_perf_indexes.sql
- preferences.service: replace 2-query SELECT+INSERT/UPDATE with single-round-trip upsert
- conversations repo: add ORDER BY + LIMIT to findAll (200) and findMessages (500)
- session-gc.service: make onModuleInit fire-and-forget (removes cold-start TTFB block)
- next.config.ts: enable compress, productionBrowserSourceMaps:false, image avif/webp
- docs/PERFORMANCE.md: full profiling report and change impact notes
chore: format BUG-CLI-scratchpad.md (prettier)
All checks were successful
ci/woodpecker/push/ci Pipeline was successful
ci/woodpecker/pr/ci Pipeline was successful
133668f5b2
jason.woltje merged commit cbb729f377 into main 2026-03-21 12:28:31 +00:00
jason.woltje deleted branch feat/p8-003-performance 2026-03-21 12:28:31 +00:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: mosaicstack/stack#211