perf(website-server): Enable Node.js compile cache for faster cold starts#1066
perf(website-server): Enable Node.js compile cache for faster cold starts#1066
Conversation
…arts Add V8 compile cache support to reduce Cloud Run cold start latency. The cache is pre-generated at Docker build time using a warmup script, so subsequent container starts use pre-compiled code. Changes: - Add warmup.mjs script to generate compile cache at build time - Add WARMUP_MODE check to skip server initialization during warmup - Set NODE_COMPILE_CACHE environment variable in Dockerfile - Run warmup script during Docker build to pre-populate cache
The warmup script was hanging during Docker build because some modules (like winston/LoggingWinston) keep the event loop alive. Adding an explicit process.exit(0) ensures the warmup script terminates after generating the compile cache. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. 📝 WalkthroughWalkthroughThis change introduces V8 compile cache optimization to reduce Node.js startup latency. A new warmup script pre-generates compile cache during Docker build by importing dependencies, and server initialization is updated to skip startup during warmup mode. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Pre-merge checks and finishing touches✅ Passed checks (3 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @yamadashy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a performance optimization aimed at reducing the cold start latency of the website server. By leveraging the Node.js V8 compile cache, the server's modules are pre-compiled during the Docker build process. This ensures that when the server starts in a production environment, particularly on platforms like Cloud Run, it can utilize already compiled code, leading to faster startup times and improved responsiveness. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively implements the Node.js compile cache to reduce server cold start latency. The approach of using a warmup.mjs script during the Docker build is sound, and the necessary changes in the server's entry point to accommodate a 'warmup mode' are correctly implemented. The code is clear and follows best practices for this feature. I have one suggestion to optimize the final Docker image by removing the warmup script after it's used, which will help in keeping the image size minimal.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1066 +/- ##
=======================================
Coverage 87.19% 87.19%
=======================================
Files 116 116
Lines 4350 4350
Branches 1011 1011
=======================================
Hits 3793 3793
Misses 557 557 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Deploying repomix with
|
| Latest commit: |
78cf278
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://10509484.repomix.pages.dev |
| Branch Preview URL: | https://perf-website-server-compile.repomix.pages.dev |
PR Review: perf(website-server): Enable Node.js compile cache for faster cold startsOverviewThis PR enables V8 compile cache (Node.js v22.8.0+) to reduce Cloud Run cold start latency by pre-compiling modules during Docker build time. ✅ Strengths
📋 Code Quality AssessmentDetailswarmup.mjs
src/index.ts
Dockerfile
|
| Scenario | Risk | Mitigation |
|---|---|---|
| Node.js version mismatch between build and runtime | Low | Both stages use same base image (node:24-alpine) |
| Compile cache corruption | Low | Cache is regenerated on each Docker build |
| Memory overhead from cache | Low | Compile cache is typically small; worth monitoring |
| API changes in future Node versions | Low | Using stable documented APIs |
| Warmup import failure | Medium | Build would fail immediately - add try/catch with clear error message if you want more graceful handling |
🛡️ Security Review
No security concerns. The changes don't affect:
- Input validation
- Authentication/authorization
- External API interactions
- File system access patterns
📊 Test Coverage
The website server doesn't have unit tests (no test files found in website/server/). The compile cache feature is tested implicitly by the Docker build succeeding. For a performance optimization like this, integration testing via successful deployment is appropriate.
Verdict
LGTM ✅ - This is a well-implemented, low-risk performance optimization with minimal code changes. The implementation follows Node.js best practices and integrates cleanly with the existing codebase.
Reviewed by Claude
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
website/server/warmup.mjs (1)
11-25: Consider adding error handling for robustness.The warmup script lacks error handling. While Docker build failures will surface if the script exits with non-zero status, explicit try-catch blocks would provide clearer error messages during build-time debugging.
🔎 Proposed error handling
+try { // Enable compile cache (uses NODE_COMPILE_CACHE env var for directory) enableCompileCache(); // Set warmup mode to prevent server from actually starting process.env.WARMUP_MODE = 'true'; // Import the server module to trigger compilation of all dependencies await import('./dist-bundled/server.mjs'); // Flush cache to disk immediately (default is on process exit) flushCompileCache(); console.log('Compile cache generated successfully'); +} catch (error) { + console.error('Failed to generate compile cache:', error); + process.exit(1); +}
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
website/server/Dockerfilewebsite/server/src/index.tswebsite/server/warmup.mjs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (13)
- GitHub Check: Test (windows-latest, 24.x)
- GitHub Check: Test (macos-latest, 25.x)
- GitHub Check: Build and run (windows-latest, 24.x)
- GitHub Check: Test (macos-latest, 24.x)
- GitHub Check: Test (ubuntu-latest, 25.x)
- GitHub Check: Build and run (windows-latest, 25.x)
- GitHub Check: Test (windows-latest, 20.x)
- GitHub Check: Test (windows-latest, 22.x)
- GitHub Check: Build and run (windows-latest, 20.x)
- GitHub Check: Build and run (windows-latest, 22.x)
- GitHub Check: Test with Bun (windows-latest, latest)
- GitHub Check: claude-review
- GitHub Check: Cloudflare Pages
🔇 Additional comments (6)
website/server/src/index.ts (2)
25-28: LGTM! Clean warmup mode implementation.The
isWarmupMode()function correctly checks the environment variable to determine if running in warmup mode. This aligns well with the existingisTinypoolWorker()pattern.
30-31: Server initialization guard correctly updated.The condition now properly skips server initialization in both Tinypool worker mode and warmup mode, which is essential for compile cache generation during Docker build.
website/server/Dockerfile (3)
47-48: Warmup script correctly copied from builder stage.The warmup script is properly copied from the builder stage to the runtime image, enabling compile cache generation during Docker build.
50-56: Environment variables properly configured for compile cache.The
NODE_COMPILE_CACHE=/app/.compile-cacheenvironment variable correctly specifies the cache directory. The warmup script will use this location to store pre-compiled bytecode.
58-60: Compile cache generation correctly integrated into build process.Running
node warmup.mjsduring the Docker build ensures the compile cache is pre-generated before the runtime container starts. The implementation is sound:
NODE_COMPILE_CACHEenvironment variable is properly set to/app/.compile-cachebefore the warmup script executeswarmup.mjsuses the official Node.jsenableCompileCache()API from thenode:modulemodule- The server respects the
WARMUP_MODEflag and skips initialization during cache generationflushCompileCache()ensures the cache is persisted to disk before the build completes- Node.js 24 (specified in Dockerfile) fully supports this feature
This will reduce cold start latency by pre-compiling all modules at build time rather than on first run.
website/server/warmup.mjs (1)
11-14: No compatibility issues detected—Node.js 24 in the base image fully supports the compile cache API.The
node:24-alpineimage in website/server/Dockerfile meets the requirement. Node.js 24.x is well above the minimum v22.8.0 where compile cache APIs were introduced.
PR Review: perf(website-server): Enable Node.js compile cache for faster cold startsSummaryThis PR implements Node.js V8 compile cache (available since v22.8.0) to reduce Cloud Run cold start latency by pre-compiling modules during Docker build time. ✅ Overall Assessment: LGTMThis is a well-implemented, low-risk performance optimization with minimal code changes. The implementation follows Node.js best practices and integrates cleanly with the existing codebase. Strengths
Code Quality AssessmentDetailswarmup.mjs
src/index.ts
Dockerfile
🔍 Potential Failure Scenarios (Premortem)Details
🛡️ Security ReviewNo security concerns identified:
📊 Test CoveragePer Codecov: All modified and coverable lines are covered. Project coverage remains at 87.19%. For a performance optimization like this, the Docker build success serves as effective integration testing. 💡 Optional Suggestions (Non-blocking)Details
VerdictApproved ✅ - Clean, well-documented performance optimization with minimal risk. Reviewed by Claude |
Add compile cache directory to server startup metrics log to help verify that the Node.js compile cache is properly enabled in production.
Follow-up PR ReviewI've re-reviewed this PR. Since my previous reviews (visible above), the only additional commit adds compile cache directory logging on startup ( Bot Comment EvaluationsGemini Code Assist suggested removing the warmup script after use (
The warmup script is very small (~600 bytes) and keeping it in the image aids debugging if there are issues with the compile cache in production. The space savings are negligible. StatusNo new issues identified. My previous review assessment stands: LGTM ✅ The implementation is clean, well-documented, and follows Node.js best practices for compile cache usage. Reviewed by Claude |
Enable V8 compile cache to reduce Cloud Run cold start latency.
Summary
This PR implements Node.js compile cache (available since v22.8.0) to speed up server startup on Cloud Run cold starts.
How it works
warmup.mjsto import all modules and generate compile cacheChanges
WARMUP_MODEcheck to skip server initialization during warmupNODE_COMPILE_CACHEenv var and run warmup script during buildReference
Note
Since the server is already bundled with esbuild, the improvement may be less dramatic than unbundled setups. However, the implementation cost is minimal and it still helps with:
server.mjsfile itselftinypool,tiktoken)Checklist
npm run testnpm run lint