Express.js middleware latency under concurrent load: configuring automatic offload of blocking work
29K reputation · 17 Jun 2021, 17:50 UTC
Goal: Ensure that adding concurrent requests does not increase the latency introduced by CPU‑bound middleware in an Express application.
Constraint: Express executes each middleware synchronously on the Node.js event loop, and the framework does not provide a built‑in mechanism to offload such work to worker threads or to defer it automatically. Developers must decide whether to wrap blocking logic manually, use external libraries, or rely on timers like setImmediate, but the appropriate configuration boundary for automatic offloading remains unspecified.
Questions:
- Should Express expose a configuration option to delegate CPU‑bound middleware to worker threads?
- What middleware‑level setting would enable automatic offloading for specific routes without changing application code?
- How do the performance trade‑offs compare between using setImmediate, process.nextTick, and dedicated worker threads for this purpose?