# Scaling और Clustering — Fastify

Source: https://www.geekswithgeeks.com/hi/fastify/ff-scaling

> Multiple processes और machines के across Fastify applications को scale करें।

## Horizontal scaling

More traffic handle करने के लिए load balancer (nginx, HAProxy, या cloud provider LB) के पीछे multiple Fastify instances चलाएँ।

## Node के cluster module के साथ clustering

Single machine पर सभी CPU cores को utilize करने के लिए multiple processes spawn करें।

```javascript
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

Fastify app को stateless रखें—shared state के लिए external stores (Redis, databases) उपयोग करें ताकि कोई भी instance किसी भी request को handle कर सके।
