From fe48d08c6955f7696a37f1d5970d6406e396ea8c Mon Sep 17 00:00:00 2001 From: MickLesk Date: Sun, 22 Feb 2026 14:48:11 +0100 Subject: [PATCH] perf: optimize Twenty CRM resource usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Reduce defaults: 4 CPU/8GB → 2 CPU/4GB (official min is 2GB) - Node.js heap: 512MB server, 384MB worker (runtime) - Build-time heap: 3072MB (only during compilation) - PG pool: reduce to 5 connections - PostgreSQL: tune shared_buffers/work_mem/effective_cache_size - Redis: cap at 64MB with noeviction policy - Unset NODE_OPTIONS after build to not leak into runtime --- ct/twenty.sh | 7 ++++--- frontend/public/json/twenty.json | 4 ++-- install/twenty-install.sh | 23 ++++++++++++++++++++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/ct/twenty.sh b/ct/twenty.sh index f47d5a79a..46bb80115 100644 --- a/ct/twenty.sh +++ b/ct/twenty.sh @@ -8,8 +8,8 @@ source <(curl -fsSL "$COMMUNITY_SCRIPTS_URL/misc/build.func") APP="Twenty" var_tags="${var_tags:-crm;business;contacts}" -var_cpu="${var_cpu:-4}" -var_ram="${var_ram:-8192}" +var_cpu="${var_cpu:-2}" +var_ram="${var_ram:-4096}" var_disk="${var_disk:-20}" var_os="${var_os:-debian}" var_version="${var_version:-13}" @@ -51,11 +51,12 @@ function update_script() { export COREPACK_ENABLE_DOWNLOAD_PROMPT=0 $STD corepack enable $STD corepack prepare yarn@4.9.2 --activate - export NODE_OPTIONS="--max-old-space-size=4096" + export NODE_OPTIONS="--max-old-space-size=3072" $STD yarn install --immutable || $STD yarn install $STD npx nx run twenty-server:build $STD npx nx build twenty-front cp -r /opt/twenty/packages/twenty-front/build /opt/twenty/packages/twenty-server/dist/front + unset NODE_OPTIONS msg_ok "Built Application" msg_info "Running Database Migrations" diff --git a/frontend/public/json/twenty.json b/frontend/public/json/twenty.json index d05c190e6..286eda783 100644 --- a/frontend/public/json/twenty.json +++ b/frontend/public/json/twenty.json @@ -19,8 +19,8 @@ "type": "default", "script": "ct/twenty.sh", "resources": { - "cpu": 4, - "ram": 8192, + "cpu": 2, + "ram": 4096, "hdd": 20, "os": "Debian", "version": "13" diff --git a/install/twenty-install.sh b/install/twenty-install.sh index f0c057b32..d97c161ae 100644 --- a/install/twenty-install.sh +++ b/install/twenty-install.sh @@ -30,11 +30,12 @@ cd /opt/twenty export COREPACK_ENABLE_DOWNLOAD_PROMPT=0 $STD corepack enable $STD corepack prepare yarn@4.9.2 --activate -export NODE_OPTIONS="--max-old-space-size=4096" +export NODE_OPTIONS="--max-old-space-size=3072" $STD yarn install --immutable || $STD yarn install $STD npx nx run twenty-server:build $STD npx nx build twenty-front cp -r /opt/twenty/packages/twenty-front/build /opt/twenty/packages/twenty-server/dist/front +unset NODE_OPTIONS msg_ok "Built Application" msg_info "Configuring Application" @@ -43,6 +44,7 @@ mkdir -p /opt/twenty/packages/twenty-server/.local-storage cat </opt/twenty/.env NODE_PORT=3000 PG_DATABASE_URL=postgresql://${PG_DB_USER}:${PG_DB_PASS}@localhost:5432/${PG_DB_NAME} +PG_POOL_MAX_CONNECTIONS=5 REDIS_URL=redis://localhost:6379 SERVER_URL=http://${LOCAL_IP}:3000 APP_SECRET=${APP_SECRET} @@ -51,6 +53,23 @@ NODE_ENV=production EOF msg_ok "Configured Application" +msg_info "Tuning Services" +# Redis: cap memory at 64MB with LRU eviction +cat <>/etc/redis/redis.conf +maxmemory 64mb +maxmemory-policy noeviction +EOF +systemctl restart redis-server + +# PostgreSQL: optimize for low-memory LXC +PG_CONF="/etc/postgresql/16/main/postgresql.conf" +sed -i "s/^shared_buffers.*/shared_buffers = 128MB/" "$PG_CONF" +sed -i "s/^#work_mem.*/work_mem = 4MB/" "$PG_CONF" +sed -i "s/^#effective_cache_size.*/effective_cache_size = 256MB/" "$PG_CONF" +sed -i "s/^#maintenance_work_mem.*/maintenance_work_mem = 64MB/" "$PG_CONF" +systemctl restart postgresql +msg_ok "Tuned Services" + msg_info "Running Database Migrations" cd /opt/twenty/packages/twenty-server set -a && source /opt/twenty/.env && set +a @@ -71,6 +90,7 @@ User=root WorkingDirectory=/opt/twenty/packages/twenty-server EnvironmentFile=/opt/twenty/.env ExecStart=/usr/bin/node dist/main +Environment=NODE_OPTIONS=--max-old-space-size=512 Restart=on-failure RestartSec=5 @@ -92,6 +112,7 @@ EnvironmentFile=/opt/twenty/.env Environment=DISABLE_DB_MIGRATIONS=true Environment=DISABLE_CRON_JOBS_REGISTRATION=true ExecStart=/usr/bin/node dist/queue-worker/queue-worker +Environment=NODE_OPTIONS=--max-old-space-size=384 Restart=on-failure RestartSec=5