perf: share db, docker and auth singletons across duplicated bundles#4777
Merged
Conversation
The @dokploy/server modules get bundled multiple times per process (esbuild inline copy, compiled node_modules copy, and several Next.js chunks via transpilePackages), so every module-level side effect ran once per copy: up to 6 postgres pools, 6 dockerode clients and 6 better-auth instances in the server process, plus the repeated 'Using Docker socket' logs at boot. - db/index.ts: use the globalThis cache in production too (one pool per process) - constants/index.ts: cache the dockerode client on globalThis - lib/auth.ts: wrap betterAuth() in a factory and cache the instance - Dockerfile: exec node directly instead of leaving pnpm resident (~100MB RSS)
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.
Problem
@dokploy/servergets bundled several times into the same server process: an inline copy indist/server.mjs(esbuild + tsconfig paths for subpath imports), the compilednode_modulescopy (bare imports left external bypackages: "external"), and 4 copies duplicated across.next/server/chunksbytranspilePackages. Every module-level side effect runs once per copy:db/index.tsexplicitly skipped theglobalThiscache in production (measured: 21 idle connections on a fresh instance after ~20 min of traffic)Using Docker socketlog at boot (7 times)On top of that, the image keeps
pnpmresident as a process wrapper (~100MB RSS doing nothing). Measured container usage: ~930MB after 27 minutes up (801MB node + 99MB pnpm).Changes
globalThiscache in production too → one pool per processglobalThis→ also fixes the repeated socket logsbetterAuth()in acreateBetterAuth()factory and cache the instance onglobalThisexec nodedirectly in CMD instead of going throughpnpm startCode copies are still duplicated across bundles (drizzle schema instantiated per copy); deduplicating the bundling itself (
serverExternalPackagesin prod + wildcard exports todist/) is a follow-up.