Scaling
Auto-scaling and worker management
Scaling
Worlds Engine automatically scales workers based on workload.
Auto-Scaling Algorithm
The scaling algorithm evaluates workload every 10 seconds.
Workload Calculation:
workload = busy_workers / total_workers
Scale Up
Triggered when workload >= scaleThreshold (default 0.7) and workers < maxWorkers.
Workers to Add:
needed = min(
ceil((maxWorkers - current) / 2),
maxWorkers - current
)
Aggressively adds workers (half the remaining capacity) to handle spikes.
Scale Down
Triggered when workload <= scaleDownThreshold (default 0.3) and workers > minWorkers.
Workers to Remove:
toKill = min(
floor((current - minWorkers) / 2),
current - minWorkers
)
Gradually removes workers (half the excess) to prevent oscillation.
Configuration
const world = new World({
minWorkers: 2, // Always keep 2 workers
maxWorkers: 10, // Max 10 workers
scaleThreshold: 0.7, // Scale up at 70% load
scaleDownThreshold: 0.3 // Scale down at 30% load
});
Worker Lifecycle
Workers execute tasks concurrently using the Node.js event loop. Each worker runs a single task at a time.
- Spawn: Worker created, status
idle - Poll: Checks queue for tasks
- Execute: Status
busy, runs task - Heartbeat: Periodically signals liveness
- Complete: Updates status, returns to
idle - Stop: Graceful shutdown, finishes current task