Skip to content
Back to Deployment

Deploying Fastify Apps

High-performance Node.js framework with schema-based serialization, deploy via PM2, Docker, or serverless.

PlatformsIntermediate

Why

Fastify is 2-3x faster than Express thanks to JSON schema-based serialization and compilation. Its plugin architecture and built-in TypeScript support make it ideal for performance-critical APIs.

Platform-as-a-Service adoption grew 24% in 2024 as teams shift from VM management to git-push deploys. the cloud infrastructure market reached $330B in 2024, growing 22% year-over-year (Synergy Research), and PaaS layers capture the majority of new workloads.

When

Choose Fastify over Express when throughput matters (high-traffic APIs, gateways). Use Express when middleware ecosystem compatibility is more important.

How

1. Build — tsc to compile, fastify has no build step itself 2. Schema — define JSON schema for routes, fastify compiles for fast serialization 3. PM2 — pm2 start dist/server.js -i max for cluster 4. Docker — node:20-alpine, CMD ["node", "dist/server.js"] 5. Serverless — @fastify/aws-lambda adapter for Lambda 6. Plugins — register with fastify.register(), encapsulated scope 7. Hooks — onRequest/preHandler for auth, logging, metrics

Example

// src/server.ts — Fastify with schema import Fastify from "fastify"; const app = Fastify({ logger: true }); app.get("/users/:id", { schema: { params: { type: "object", properties: { id: { type: "string" } } }, response: { 200: { type: "object", properties: { id: { type: "string" }, name: { type: "string" } } } }, }, handler: async (req) => ({ id: req.params.id, name: "Alice" }), }); app.listen({ port: 3000, host: "0.0.0.0" }); // Dockerfile FROM node:20-alpine AS build WORKDIR /app COPY . . RUN npm ci && npm run build FROM node:20-alpine WORKDIR /app COPY --from=build /app/dist ./dist COPY --from=build /app/node_modules ./node_modules EXPOSE 3000 CMD ["node", "dist/server.js"]

Common Problems

  • ⚠Schema validation rejects valid data — too-strict schema, add additionalProperties: false intentionally
  • ⚠Plugin scope issues — decorators not available outside encapsulated plugin scope, use fastify-plugin
  • ⚠Slow serialization without schema — response without schema uses slow JSON.stringify, always define response schema

Best Practices

  • ✓Always define JSON schema for routes — Fastify's speed comes from schema-compiled serialization
  • ✓Use fastify-plugin to break encapsulation when decorators should be globally available
  • ✓Set up health checks and graceful shutdown — handle SIGTERM to drain in-flight requests before exit

FAQ