Fastify Best Practices & Pitfalls
When to use
- •When creating new Fastify routes or plugins.
- •When debugging performance issues or common bugs in Fastify.
- •When reviewing code for security and stability.
Things to AVOID (Pitfalls)
1. Blocking the Event Loop
- •Avoid: Synchronous heavy computation (CPU-bound tasks) inside route handlers.
- •Why: Node.js is single-threaded. Blocking logic stops the server from handling other requests.
- •Fix: Offload to worker threads or optimize algorithms.
2. console.log for Logging
- •Avoid: Using
console.log,console.errorin production. - •Why: It is synchronous and slow; it blocks the event loop.
- •Fix: Use Fastify's built-in logger (
request.logorfastify.log) which uses Pino (async, fast).
3. Missing Schemas
- •Avoid: Defining routes without JSON schemas for request/response.
- •Why: Schemas provide faster serialization (Fastify optimizes JSON parsing) and automatic validation/documentation (Swagger).
- •Fix: Always define
schemain route options.
4. Global State Mutation
- •Avoid: Modifying global variables or
fastifyinstance properties directly inside request handlers. - •Why: Causes race conditions and cross-request pollution.
- •Fix: Use
decorateRequestor request-scoped context.
5. Incorrect Plugin Encapsulation
- •Avoid: Registering plugins that accidentally affect parent or sibling contexts.
- •Fix: Use
fastify-plugin(fp) to break encapsulation when you want to share decorators, but understand Fastify's encapsulation model (DAG) to keep plugins isolated when needed.
6. Using Arrow Functions for Handlers (Context Binding)
- •Warning: Be careful when using arrow functions if you rely on
thiscontext in Fastify (though standard usage often relies onrequest/replyargs). - •Best Practice: Prefer explicit arguments
(request, reply) => ...over relying onthis.
Best Practices
1. Dependency Injection
- •Register services, db connections, and configs as plugins or decorators early in the boot sequence.
2. Error Handling
- •Use
fastify.setErrorHandlerto centralize error formatting. - •Do not let promises hang; always
awaitor return the promise.
3. Graceful Shutdown
- •Handle
SIGINT/SIGTERM. - •Close database connections and
fastify.close().