Repository navigation
perf: warm lambdas on page load and front-load cold-start setup - #409
Merged
Merged
Conversation
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>
Contributor
|
🌿 ⏳ Creating preview environment… (logs) |
Contributor
🌿 Preview environment — ready ✅Open: https://d34ajqx96zo93a.cloudfront.net/pr-409/ 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 |
# Conflicts: # apps/frontend/src/app/providers.tsx
Contributor
# 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
Contributor
Contributor
|
🌿 Preview environment torn down 🧹 — the stack for this PR has been destroyed. |
This branch was successfully deployed
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.
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 onebranch.usersquery, 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>/healthwaits for the warm-up before answering, so Lambda doesn't freeze the container part-way through.auth.tsexportswarmup,handler.tswraps it inprewarm).warmServices():Providerscalls it once per page load. It pings all 6/healthendpoints with ano-corsGET, 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
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,prewarmruns once / is a no-op outside Lambda / swallows failures, andwarmAuthhydrates and queries.tsc --noEmitandnpm run package.projectshandler with Lambda env set and an unreachable DB: loggedWarm-up failed: ECONNREFUSED, and health still returned 200 after 130 ms.tscand eslint are clean.Verify after deploy
Compare CloudWatch
Init Durationand first-request latency per function before and after.🤖 Generated with Claude Code