feat: reduce cold start latency for serverless functions#426
Open
od-hunter wants to merge 2 commits into
Open
Conversation
- Lazy Sentry profiling: defer @sentry/profiling-node native addon load until after server.listen() via setImmediate, saving ~30-50ms on cold start - Deferred background services: jobs, BullMQ scheduler, queue processors, webhook worker, batch processor, and Redis connection all start after the server is already accepting requests (inside listen() callback) - Lazy sandbox initialization: SandboxManager, MockPaymentProcessor, and TestDataSeeder constructed on first use via getter functions, not at module scope - Fixed import ordering: all imports moved to top of file (ESM requirement), removed mid-file import that was between route registrations - Added missing gdprRouter import that caused a ReferenceError at runtime - Removed dead imports: SecurityMiddleware, SecurityMonitor, sanitizeInput, contentSecurityPolicy, rateLimitAnalyticsRouter, sessionsRouter, stripeRouter, signaturesRouter were imported but never used workers/src/index.ts: - Code splitting via classifyRoute() + getHandler() with handler cache for execution context reuse across requests in the same isolate - Cold start detection: isFirstRequest flag captures init duration on first request and persists to KV for the monitoring dashboard - Warm-up scheduling: scheduled() export handles Cloudflare cron trigger, proxies to backend /api/v1/cold-start/warmup every 5 minutes - Warmup proxies to backend so backend lastRequestAt is updated correctly workers/wrangler.toml: - Added cron trigger every 5 minutes for warm-up scheduling backend/src/lib/lazy-loader.ts (new): - lazyLoad(), lazySyncLoad(), trackedLazyLoad() utilities for deferred module initialization with a registry for the monitoring dashboard backend/src/middleware/cold-start.ts (new): - Per-request cold start tracking middleware with p50/p95/p99 latency - Detects first-request and idle-gap cold starts, records init duration - Fixed idle-gap initDurationMs to report actual gap duration backend/src/routes/cold-start-monitor.ts (new): - GET /api/v1/cold-start/metrics - full snapshot, p95 target compliance - GET /api/v1/cold-start/summary - lightweight dashboard summary - POST /api/v1/cold-start/reset - reset counters - POST /api/v1/cold-start/warmup - warm-up ping endpoint for schedulers
|
@od-hunter is attempting to deploy a commit to the smartdevs17's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@od-hunter Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Closes #385
#385