Skip to content

feat: reduce cold start latency for serverless functions#426

Merged
Smartdevs17 merged 3 commits into
Smartdevs17:mainfrom
od-hunter:feat/reduce-cold-start-latency
Jun 1, 2026
Merged

feat: reduce cold start latency for serverless functions#426
Smartdevs17 merged 3 commits into
Smartdevs17:mainfrom
od-hunter:feat/reduce-cold-start-latency

Conversation

@od-hunter
Copy link
Copy Markdown
Contributor

@od-hunter od-hunter commented May 29, 2026

Closes #385


#385

- 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
@vercel
Copy link
Copy Markdown

vercel Bot commented May 29, 2026

@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.

@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented May 29, 2026

@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! 🚀

Learn more about application limits

@gitguardian
Copy link
Copy Markdown

gitguardian Bot commented Jun 1, 2026

⚠️ GitGuardian has uncovered 1 secret following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

Since your pull request originates from a forked repository, GitGuardian is not able to associate the secrets uncovered with secret incidents on your GitGuardian dashboard.
Skipping this check run and merging your pull request will create secret incidents on your GitGuardian dashboard.

🔎 Detected hardcoded secret in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
15742864 Triggered Generic Password aea1ca4 .github/workflows/migrations.yml View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secret safely. Learn here the best practices.
  3. Revoke and rotate this secret.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

@Smartdevs17 Smartdevs17 merged commit 5532c95 into Smartdevs17:main Jun 1, 2026
13 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce cold start latency for serverless functions

2 participants