Dynamic Workers
Overview
Dynamic Workers is an automatic resource allocation algorithm that dynamically adjusts the number of workers for applications based on their Event Loop Utilization (ELU) metrics. It intelligently balances computational resources across multiple applications while respecting system constraints.
How It Works
Health Metrics
The algorithm uses two primary health metrics:
Event Loop Utilization (ELU)
ELU measures how busy the Node.js event loop is:
- 0.0 = Event loop is completely idle
- 1.0 = Event loop is fully saturated
ELU values are collected continuously from all workers and averaged over a configurable time window to smooth out temporary spikes and make stable scaling decisions.
Memory Usage
The algorithm tracks heap memory usage (heapUsed and heapTotal) for each worker. When making scaling decisions, it considers:
- Total memory limit: A configured maximum total memory (
maxTotalMemory), defaulting to 90% of the system's total memory - Available memory: Calculated as
maxTotalMemory - currently used memory - Average heap usage: The average memory consumed by workers of each application
The system memory information is obtained from cgroup files when running in containerized environments (Docker, Kubernetes), or from the operating system otherwise. This ensures that new workers are only started when there's sufficient memory available to accommodate them based on the application's average heap usage.
Time Windows
The algorithm uses different time windows for scale-up and scale-down decisions:
- Scale-up time window (
timeWindowSec): A shorter window (default: 10 seconds) for detecting high utilization and scaling up quickly - Scale-down time window (
scaleDownTimeWindowSec): A longer window (default: 60 seconds) for detecting sustained low utilization before scaling down, preventing premature worker removal
Scaling Logic
The algorithm operates in two modes:
- Reactive Mode: Triggers immediately when any worker's ELU exceeds the scale-up threshold
- Periodic Mode: Runs at regular intervals (default: every 60 seconds) regardless of metrics
Both modes analyze all applications and generate scaling recommendations:
1. Metric Collection
- Collects ELU and heap memory metrics from all active workers every second
- Only collects metrics from workers that have been running for at least the grace period (default: 30 seconds)
- Maintains a rolling time window of metrics for both scale-up (default: 10 seconds) and scale-down (default: 60 seconds) decisions
- Calculates average ELU and heap usage per application across all its workers using the appropriate time window
- Checks available memory by calculating
maxTotalMemory - currently used memory