Skip to content

Fastify vs Express in production

Compare Fastify vs Express for production Node.js HTTP services using measurements from a real workload instead of a hello-world benchmark. You will see when each framework is the better pick, based on your validation strategy, your ecosystem and the cost of migrating.

For a new Node.js HTTP service that mostly takes and returns JSON, Fastify is usually the better choice. It checks requests and builds responses from a JSON Schema you declare per route, which is a description of the fields and types you expect. It also has excellent TypeScript support. Express remains the sensible pick when you depend on Express-specific middleware (functions that run on every request) such as passport. It also wins when your team already runs many Express services and does not want a second framework. Raw speed matters less than benchmarks suggest. On a bare endpoint, Fastify handles two to three times the requests per second (RPS) of Express. Once a request parses JSON and queries a database, that gap often shrinks to 15 to 30 percent. The sections below show how to measure both on your own workload, compare them criterion by criterion, and migrate from Express to Fastify one route at a time.

How to verify

Pick the right one by measuring against your actual workload, not a hello-world benchmark:

# Both servers handling 1 KB JSON POST with validation
autocannon -c 100 -d 30 -p 10 http://127.0.0.1:3000/api/things
wrk -t8 -c200 -d30s -s post.lua http://127.0.0.1:3000/api/things
# CPU profile under load
curl -s http://127.0.0.1:3000/_debug/pprof/profile?seconds=30 > prof.pb.gz
# Memory under steady load
ps -o pid,rss,cmd -p $(pgrep -f node)

A no-validation hello endpoint gives Fastify a 2-3x throughput advantage. A real endpoint that parses 5 KB JSON, validates, hits Postgres, and emits 8 KB JSON often shrinks that gap to 15-30% because the database is the bottleneck, not the framework.

What’s happening

Express dispatches via a linear middleware chain. Each request walks the registered middleware in registration order, with next() chaining and a small per-call overhead. It is intentionally unopinionated: validation, schema, serialization, error handling are all libraries you bolt on. The ecosystem is enormous and almost any auth/CORS/parsing problem has a express-* package.

Fastify is built around schema-driven routing. Routes declare a JSON Schema for body, query, params, and response; Fastify compiles those schemas to native JS validators at boot via ajv and uses them at request time. Response serialization with a schema is also compiled — Fastify’s secret throughput advantage isn’t a faster router, it’s not running JSON.stringify against an unknown shape. The plugin system enforces encapsulation: a plugin gets its own DI scope and cannot leak globals into another plugin without fastify-plugin.

The decision

CriterionExpressFastify
Raw throughput, plain JSON~25 K rps~55 K rps
Real DB-backed endpoint~3 K rps~3.5-4 K rps
Schema validation built-inNo (ajv as library)Yes
Plugin/DI encapsulationNoYes
Middleware ecosystemVast, matureSmaller, growing
Async/await first-classMixed (errors in async need wrapper)Yes
TypeScript ergonomicsDecentExcellent (typed routes via schemas)
Migration cost from one to the otherHigh if heavy middleware useMedium
Years-old battle-tested libraries that target itAllMost, growing
Team familiarityUniversalCommon but lower

Pick Fastify when:

  • The workload is JSON in/out and you want schema-driven validation + serialization
  • You are building a new service and TypeScript ergonomics matter
  • The team is comfortable with the plugin/encapsulation model

Pick Express when:

  • You have heavy reliance on Express-specific middleware (passport, connect-*, ancient cookie handlers)
  • The team already runs 50 Express services and you don’t want a second framework
  • You need the largest possible ecosystem of compatible packages

Use both in parallel when you are mid-migration: a strangler-fig where new routes go to Fastify behind the same reverse proxy.

The procedure (migrating Express to Fastify)

  1. Add @fastify/express to your Fastify app so existing Express middleware keeps working during migration:

    import Fastify from 'fastify';
    import expressPlugin from '@fastify/express';
    const app = Fastify({ logger: { level: 'info' } });
    await app.register(expressPlugin);
    app.use(legacyExpressMiddleware);  // works while you migrate
  2. Declare schemas for routes you migrate. The win is the schema, not the routing:

    app.post('/api/things', {
      schema: {
        body: { type: 'object', properties: { name: { type: 'string', minLength: 1 } }, required: ['name'] },
        response: { 200: { type: 'object', properties: { id: { type: 'integer' } } } },
      },
    }, async (req) => createThing(req.body));
  3. Strip the Express middlewares one by one as you port their behavior to Fastify hooks (onRequest, preHandler, onSend). When @fastify/express is no longer needed, remove it.

  4. For request logging, prefer Fastify’s built-in logger (pino) over morgan. For body parsing, Fastify’s is built in. For cookies, @fastify/cookie. For CORS, @fastify/cors. The Fastify ecosystem covers the 95% of Express middleware use cases.

  5. Cut over by switching the reverse proxy upstream once the Fastify app passes a soak test under production-realistic traffic.

Common pitfalls

  • Benchmarks lie. A 60 K rps hello world is meaningless; the moment you add JSON parsing, validation, and a database call, the framework is rarely the bottleneck.
  • Fastify’s plugin encapsulation surprises Express converts: registering a route inside a plugin’s register callback does not make it visible outside the plugin unless you wrap with fastify-plugin.
  • Express error-handling middleware ((err, req, res, next) => ...) does not exist in Fastify. Use the setErrorHandler hook instead.
  • app.use() in Fastify with @fastify/express runs Express middleware in serial — losing Fastify’s per-route schema compilation advantage. Use Fastify-native plugins where possible.
  • Fastify’s logger is JSON-only by default. Pretty-print is a separate pino-pretty step for development; do not enable pretty-print in production.

For deploying a Fastify or Express app once you choose, see Node.js cluster mode with sticky load balancing. In the engagements Stack Harbor runs, new services default to Fastify with schema-driven routes; existing Express services are migrated only when there is a real reason (validation drift, throughput ceiling, ecosystem gap) — and we plan the migration as part of managed operations rather than treating it as a developer’s side quest.