Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 43 ยท Quality and Operations

Performance and Caching

Speed up Express: compression, ETag and Cache-Control, response caching, keeping the event loop free, clustering and keep-alive, with runnable demos.

A fast Express app is usually not the result of clever tricks. It comes from doing less work per request: sending fewer bytes, answering repeated questions from a cache, and never letting one slow task block everybody else. Node runs your JavaScript on a single thread, so a few well-chosen habits matter more than micro-optimizations.

The real packages (compression, apicache, Redis clients) cannot be installed on this site, so their code is read-only. The runnable examples are small stand-ins built on our mini Express that show the mechanisms: a cache middleware, conditional requests with ETag, and a blocked event loop. Measure before you optimize; guessing which part is slow is how effort gets wasted.

Where the time goes

Fewer bytes Compress and cache in the browser.
Fewer computations Cache repeated answers on the server.
Free event loop Never block the one JS thread.
More processes Use every CPU core.

Measure first, then fix the biggest cost.

1 / 2

Compression

Text such as JSON, HTML and CSS shrinks a lot when compressed. The compression middleware negotiates gzip or Brotli with the client:

JavaScript
import compression from 'compression';

app.use(compression()); // put it above the routes

It skips tiny responses and types that do not compress. In production many teams compress at the reverse proxy or CDN instead (nginx gzip on;, Cloudflare), which frees Node for application work. Do one or the other, not both. The idea is shown in Common Third-Party Middleware.

Cache-Control and ETag: let the browser reuse responses

The cheapest request is the one that never reaches your server. Two headers control that:

  • Cache-Control tells browsers and CDNs how long a response stays fresh: Cache-Control: public, max-age=3600. Use private for per-user data and no-store for secrets.
  • ETag is a fingerprint of the response. The browser sends it back in If-None-Match, and if the content has not changed, the server answers 304 Not Modified with no body.

Express adds a weak ETag to res.send and res.json responses automatically, and express.static supports both ETag and Last-Modified. Add freshness yourself:

JavaScript
app.get('/api/countries', (req, res) => {
  res.set('Cache-Control', 'public, max-age=86400'); // one day
  res.json(countries);
});

// hashed asset file names can be cached "forever"
app.use('/assets', express.static('public/assets', { maxAge: '1y', immutable: true }));

Here is what the conditional dance looks like at the byte level. The stand-in computes a hash of the body and answers 304 when the client already has it:

Node.js ETag and 304 Not Modified

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The second request still travels to the server, but costs almost nothing: no body is generated or sent, and the browser reuses its stored copy. Real Express does the comparison for you through req.fresh.

Caching on the server

Some work is expensive to repeat: a heavy database query, a call to a slow third-party API, rendering a big page. If the answer is the same for everyone for a while, remember it. A tiny response cache stores the body by URL for a few seconds:

JavaScript
const cache = new Map();

function cacheFor(ms) {
  return (req, res, next) => {
    const hit = cache.get(req.originalUrl);
    if (hit && hit.expires > Date.now()) {
      res.set('X-Cache', 'HIT');
      return res.json(hit.body);
    }
    const send = res.json.bind(res);
    res.json = (body) => {
      cache.set(req.originalUrl, { body, expires: Date.now() + ms });
      res.set('X-Cache', 'MISS');
      return send(body);
    };
    next();
  };
}

app.get('/api/top-products', cacheFor(30_000), handler);

It works by wrapping res.json so the middleware can save the body on the way out. Try the same idea; a counter shows how often the slow work really runs:

Node.js A response cache middleware

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Three requests, but the slow query ran only twice. In real code remember three things. Never cache personal data under a shared key, or one user sees another's page. Give entries a time limit, or an explicit delete when the data changes (invalidation is the hard part). And a Map grows without bound and exists per process, so use a size limit (an LRU cache) or a shared store such as Redis when you run several processes. Where possible, let a CDN do this work with Cache-Control: public, s-maxage=....

Do not block the event loop

Node handles thousands of connections on one thread because waiting for the network or disk does not block it. But CPU work does: while a handler loops for 300 ms, no other request is served. Typical culprits are huge JSON.parse or JSON.stringify calls, synchronous file or crypto functions (readFileSync, pbkdf2Sync), complex regular expressions on user input, and big sorting or number crunching.

Node.js One slow handler delays everyone

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The timer wanted to run at 20 ms but had to wait for the loop. In a server, every waiting request is that timer. The fixes: use asynchronous APIs (fs.promises, async crypto.pbkdf2), cut work into smaller pieces, or move heavy work to a worker thread or a job queue (Worker Threads, The Event Loop). Never use *Sync functions inside request handlers; they are fine at startup.

Clustering and scaling

One Node process uses one CPU core. To use a multi-core machine, run several copies of the app: the built-in cluster module forks workers that share a port, and process managers such as PM2 do the same with pm2 start app.js -i max.

JavaScript
import cluster from 'node:cluster';
import os from 'node:os';

if (cluster.isPrimary) {
  for (let i = 0; i < os.availableParallelism(); i++) cluster.fork();
  cluster.on('exit', () => cluster.fork()); // replace a crashed worker
} else {
  const { app } = await import('./app.js');
  app.listen(3000);
}

Workers do not share memory, so keep state (sessions, caches, rate-limit counters) in a shared store like Redis, and keep the app stateless. Many deployments simply run one process per container and let the platform start more containers (Deployment and Production, Clustering and Scaling).

Keep-alive and other settings

  • Keep-alive: reusing TCP connections avoids repeated handshakes. Node enables it by default for servers; make sure the proxy in front also keeps connections to your app alive, and that Node's server.keepAliveTimeout is longer than the proxy's idle timeout (a common cause of random 502 errors).
  • Static files: let nginx or a CDN serve them, not Express.
  • Database: add indexes, use a pool, avoid N+1 queries (Databases with Express).
  • Logging: keep it asynchronous and structured; avoid logging huge bodies.
  • Production mode: run with NODE_ENV=production, which makes Express cache view templates and send shorter error messages.
  • Measure: use load tools such as autocannon, the --cpu-prof flag and your logs to find real bottlenecks.

Do not set long cache times on anything that changes, such as an HTML page or /api/me. A year-long max-age is only safe for files whose name changes when the content changes.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the server send when the browser's If-None-Match header matches the current ETag?

    304 tells the browser its stored copy is still valid, saving the cost of sending the body again.
  2. 2. Why is it dangerous to cache a per-user response under just the URL?

    A shared cache key must only be used for responses that are identical for everybody, otherwise data leaks between users.
  3. 3. What blocks the Node.js event loop?

    Waiting is non-blocking, but a busy loop or a *Sync call holds the only JS thread, so all other requests wait.
  4. 4. Why should state such as sessions or rate-limit counters live in Redis when you use clustering?

    Each worker has its own memory, so shared state needs a shared store.

Summary

  • Do less work per request: compress, cache and reuse.
  • Cache-Control and ETag let browsers and CDNs skip requests or receive a tiny 304.
  • Cache expensive results on the server with a time limit, and never share personal data under a common key.
  • Never block the event loop with CPU-heavy or *Sync work; offload it to workers or queues.
  • Run one process per core (cluster, PM2 or containers) and keep the app stateless.
  • Measure with real tools before optimizing.

Next: Deployment and Production.