Lesson 26 / 30

Scaling & Clustering

Scale Fastify applications across multiple processes and machines.

Horizontal scaling

Run multiple Fastify instances behind a load balancer (nginx, HAProxy, or cloud provider LB) to handle more traffic.

Clustering with Node's cluster module

Spawn multiple processes to utilize all CPU cores on a single machine.

import cluster from 'cluster';
import os from 'os';
import Fastify from 'fastify';

if (cluster.isPrimary) {
  const numCPUs = os.cpus().length;
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
  console.log(`Master spawned ${numCPUs} workers`);
} else {
  const fastify = Fastify();
  fastify.get('/', async () => ({ pid: process.pid }));
  await fastify.listen({ port: 3000, host: '0.0.0.0' });
}

Stateless design

Keep your Fastify app stateless—use external stores (Redis, databases) for shared state so any instance can handle any request.