XYLEX Group

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.

  1. Spawn: Worker created, status idle
  2. Poll: Checks queue for tasks
  3. Execute: Status busy, runs task
  4. Heartbeat: Periodically signals liveness
  5. Complete: Updates status, returns to idle
  6. Stop: Graceful shutdown, finishes current task