Skip to content

perf: warm lambdas on page load and front-load cold-start setup - #409

Merged
nourshoreibah merged 4 commits into
mainfrom
perf/warm-lambdas
Oct 17, 2026
Merged

nourshoreibah merged 4 commits into
mainfrom
perf/warm-lambdas

Conversation

@nourshoreibah

Copy link
Copy Markdown
Collaborator

Why

Pages are slow when lambdas cold-start. Each cold start paid two network setups one after the other: the Cognito JWKS fetch on the first verify(), then the DB connect on the first query. With 6 separate lambdas, browsing the app hits several cold starts in a row.

What

  • warmAuth(db) (@branch/lambda-auth): verifier.hydrate() and one branch.users query, run in parallel.
  • prewarm(fn) (@branch/lambda-http): starts the warm-up at module scope during Lambda init. It does nothing outside Lambda (tests, dev-server). Failures are logged and not retried.
  • dispatch({ warmup }): GET /<prefix>/health waits for the warm-up before answering, so Lambda doesn't freeze the container part-way through.
  • All 6 lambdas are wired up (auth.ts exports warmup, handler.ts wraps it in prewarm).
  • Frontend warmServices(): Providers calls it once per page load. It pings all 6 /health endpoints with a no-cors GET, which counts as a simple request, so there's no preflight. The lambdas start warming while the user is still on the first screen (including the login page).

No infra changes. Memory, provisioned concurrency and warmers are left for a separate PR.

Testing

  • Shared packages via build-shared-packages/run.py test: rbac 30, lambda-auth 31, lambda-http 44, store 8, all passing. New tests cover: health waits for warmup, ordinary routes skip it, prewarm runs once / is a no-op outside Lambda / swallows failures, and warmAuth hydrates and queries.
  • All 6 lambdas pass tsc --noEmit and npm run package.
  • Smoke test of the bundled projects handler with Lambda env set and an unreachable DB: logged Warm-up failed: ECONNREFUSED, and health still returned 200 after 130 ms.
  • Frontend: 40 suites / 383 tests pass. tsc and eslint are clean.

Verify after deploy

Compare CloudWatch Init Duration and first-request latency per function before and after.

🤖 Generated with Claude Code

nourshoreibah and others added 2 commits October 15, 2026 13:19
Each cold start serially paid a Cognito JWKS fetch on the first verify()
and a DB connect on the first query. warmAuth(db) does both in parallel;
prewarm() kicks it off at module scope during Lambda init, and
GET /<prefix>/health awaits it.

The frontend pings all six /health endpoints once per page load
(no-cors simple GET, no preflight), so containers are warm before the
user navigates to them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nourshoreibah nourshoreibah added the test-environment Creates a temporary (nearly free) test environment. Uses prod DB and cognito label Oct 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🌿 ⏳ Creating preview environment… (logs)

@github-actions

Copy link
Copy Markdown
Contributor

🌿 Preview environment — ready ✅

Open: https://d34ajqx96zo93a.cloudfront.net/pr-409/
API: https://b3ptgjv1l1.execute-api.us-east-2.amazonaws.com/prod

Shared database + Cognito (prod data); DB migrations are not applied here — if this PR adds a migration, endpoints using the new columns will fail until it merges. New commits update this environment in place — a note is posted here on each update. Remove the test-environment label or close the PR to tear it down.

# Conflicts:
#	apps/frontend/src/app/providers.tsx
@github-actions

Copy link
Copy Markdown
Contributor

🌿 Test environment updated in place ✅ — Click here to open. updated for 399ef08 · logs

# Conflicts:
#	apps/backend/lambdas/auth/auth.ts
#	apps/backend/lambdas/auth/handler.ts
#	apps/backend/lambdas/donors/auth.ts
#	apps/backend/lambdas/donors/handler.ts
#	apps/backend/lambdas/expenditures/auth.ts
#	apps/backend/lambdas/expenditures/handler.ts
#	apps/backend/lambdas/projects/auth.ts
#	apps/backend/lambdas/projects/handler.ts
#	apps/backend/lambdas/reports/auth.ts
#	apps/backend/lambdas/reports/handler.ts
#	apps/backend/lambdas/users/auth.ts
#	apps/backend/lambdas/users/handler.ts
#	shared/lambda-auth/src/index.ts
#	shared/lambda-auth/test/authenticate.test.ts
#	shared/lambda-http/README.md
#	shared/lambda-http/src/dispatch.ts
#	shared/lambda-http/src/types.ts
@github-actions

Copy link
Copy Markdown
Contributor

🌿 Test environment updated in place ✅ — Click here to open. updated for 273b965 · logs

@nourshoreibah nourshoreibah added the no-review The PR review bot won't run label Oct 17, 2026
@nourshoreibah
nourshoreibah marked this pull request as ready for review October 17, 2026 11:38
@nourshoreibah
nourshoreibah merged commit dfc6a08 into main Oct 17, 2026
19 checks passed
@nourshoreibah
nourshoreibah deleted the perf/warm-lambdas branch October 17, 2026 11:38
@github-actions

Copy link
Copy Markdown
Contributor

🌿 Preview environment torn down 🧹 — the stack for this PR has been destroyed.

This branch was successfully deployed

1 active deployment
preview — 7c8bddad Deployed Oct 17, 2026 by nourshoreibah via teardown #533
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-review The PR review bot won't run test-environment Creates a temporary (nearly free) test environment. Uses prod DB and cognito

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant