Squeeze More Throughput Out of Nest
Performance Tuning
Pick Fastify, avoid request scope on hot paths, and profile before you optimise — most bottlenecks are downstream of Nest.
What you'll learn
- Switch from Express to Fastify
- Avoid request-scoped providers on hot routes
- Profile real bottlenecks with autocannon and clinic.js
Nest is fast — but every framework has knobs worth turning before you ship. The headline wins are picking the right HTTP platform, keeping providers singleton, and measuring before you optimise.
Use Fastify
For trivial routes, Fastify is roughly 2-3x faster than Express. The abstraction layer means almost no code changes.
npm i @nestjs/platform-fastify fastify import { NestFactory } from '@nestjs/core';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(),
);
await app.listen(3000, '0.0.0.0');
}
bootstrap(); Check that your middlewares are compatible — some Express middlewares need the Fastify-flavoured equivalents.
Mind Provider Scope
Scope.REQUEST creates a fresh instance of the provider — and the whole
chain that depends on it — on every request. That is expensive at scale.
// Avoid:
@Injectable({ scope: Scope.REQUEST })
export class UsersService {} Most apps only need request-scoped providers for things like a per-request
user context. Even then, prefer AsyncLocalStorage over request scope on
hot paths.
Cut Reflection On Hot Paths
Each interceptor, guard, and pipe runs on every request. A handful is fine. A dozen global ones — especially heavy ones that walk the metadata tree — adds up. Keep the pipeline lean and put expensive work behind a specific decorator instead.
Profile, Do Not Guess
npx autocannon -c 100 -d 30 http://localhost:3000/users
npx clinic doctor -- node dist/main.js autocannon measures throughput and latency. clinic doctor produces a
flame-graphy report that almost always shows the bottleneck is in a
downstream call — database, third-party API, or your own JSON
serialisation — not in Nest itself.