Full Stack & Backend Engineer: Scaling Event-Driven Node.js 22, TypeScript & NestJS Microservices
Node.js 22 and TypeScript (with NestJS or Fastify) power high-concurrency API gateways, Microsoft Teams / Slack bots, and real-time collaborative backends—provided developers respect the single-threaded V8 event loop. Performing synchronous JSON schema validation on multi-megabyte payloads, leaking unresolved Promises, or losing OpenTelemetry trace context across async message queues quickly degrades Node.js p99 latency. As a Full Stack Engineer and Backend Engineer, I architect non-blocking, strictly typed Node.js and NestJS microservices.
1. Propagating Correlation & Tenant Context Zero-Allocation via `AsyncLocalStorage`
Passing `tenantId` and `requestId` manually through 15 layers of TypeScript service and repository methods pollutes function signatures. Using Node.js native `node:async_hooks` (`AsyncLocalStorage`), we bind request-scoped tenant and trace metadata once in an HTTP/gRPC middleware and read it transparently inside Prisma/Drizzle database interceptors and structured Pino loggers:
import { AsyncLocalStorage } from "node:async_hooks";
import { Injectable, NestMiddleware } from "@nestjs/common";
import { Request, Response, NextFunction } from "express";
export interface RequestContextStore {
requestId: string;
tenantId: string;
startedAtMs: number;
}
export const requestContext = new AsyncLocalStorage<RequestContextStore>();
@Injectable()
export class TenantContextMiddleware implements NestMiddleware {
use(req: Request, _res: Response, next: NextFunction): void {
const store: RequestContextStore = {
requestId: String(req.headers["x-request-id"] ?? crypto.randomUUID()),
tenantId: String(req.headers["x-verified-tenant-id"] ?? "anonymous"),
startedAtMs: performance.now(),
};
requestContext.run(store, () => next());
}
}
2. Event-Loop Lag Monitoring & Worker Thread Offloading
We instrument every Node.js 22 container with `perf_hooks.monitorEventLoopDelay({ resolution: 10 })` and automatically offload CPU-heavy tasks (such as PDF rendering, cryptographic verification, or large CSV parsing) to a bounded `node:worker_threads` pool (`piscina`). This guarantees the main V8 event loop stays responsive under `< 15 ms` p99 lag across our Full Stack TypeScript & Next.js 15 architectures and HL7 FHIR R4 / Medplum TypeScript integrations.
Need a Full Stack Engineer or Backend Engineer to scale your Node.js, TypeScript, or NestJS backend? Book a 1-Day Engineering Sprint.