Fastify Adapter (platform-fastify): Routing Performance vs Express
- Last updated
- Prerequisites:
- NestJS HTTP Adapters (`HttpAdapterHost`)
- Express vs Fastify architectures
- nestjs
- fastify
- express
- performance
- platform-fastify
- throughput
Read at your depth
The practical view
NestJS is platform-agnostic: it uses Express by default (`@nestjs/platform-express`), but you can switch to Fastify by installing `@nestjs/platform-fastify` and initializing `NestFactory.create(AppModule, new FastifyAdapter())`. Fastify provides up to **2x higher throughput (RPS)** and lower latency than Express due to its radix-tree router and fast JSON serializer (`fast-json-stringify`).
The same idea in other frameworks
`NestFactory.create(AppModule, new FastifyAdapter())`.Uvicorn running Starlette on `uvloop` (high performance C-based event loop).Fastify (`fastify({ logger: true })`) vs Express (`express()`).`Bun.serve()` high-throughput native server.Legacy vs modern
Default Express Platform vs High-Throughput Fastify Adapter
Switching from Express to Fastify doubles request throughput in high-QPS NestJS microservices.
const app = await NestFactory.create(AppModule); // ❌ Defaults to slower Express platformconst app = await NestFactory.create(AppModule, new FastifyAdapter()); // ✅ NEW: Fastify engineInterview gotchas
Context
This is the classic production deployment gotcha with Fastify.
The mechanical answer
Express binds to `0.0.0.0` (all network interfaces) by default. Fastify binds to `127.0.0.1` (localhost only) by default. If you deploy a Fastify app inside a Docker container and call `await app.listen(3000)`, the container port will **not be reachable from outside the container**. You **must pass `'0.0.0.0'` explicitly**: `await app.listen(3000, '0.0.0.0')`.
Trap
Forgetting `'0.0.0.0'` in `app.listen(3000)` causes container health checks and ingress routing to fail with connection refused.
Context
Checks knowledge of middleware compatibility layers.
The mechanical answer
No. Standard Express middleware relies on the `(req, res, next)` signature and Express-specific methods (like `res.send()`). With `FastifyAdapter`, you must use native Fastify plugins (e.g. `@fastify/helmet`, `@fastify/cors`) or register them using `app.register()`.
Trap
Attempting to use `@Res() res: express.Response` in a Fastify controller will result in undefined method crashes (`res.status is not a function`).