← Back to Node.js performance map
🧵
Node.js Performance

Request Context

Preserve request IDs across async calls.

context

Detailed Description

Request context preserves metadata across async boundaries. AsyncLocalStorage lets deep functions read the current request ID or tenant without passing parameters everywhere.

This is powerful for logging and tracing, but keep the stored context small. Do not store full request bodies, user objects, or large mutable data in async context.

Mental Model

incoming request
-> AsyncLocalStorage
-> logs/spans use context

When To Use

  • ->Logs need request IDs in deep helper functions
  • ->You need per-request tenant/user context
  • ->Async code makes explicit parameter passing noisy
Tools: AsyncLocalStorage, correlation IDs, traceparent
async-local-storage.js

Live Editor

Loading...

Output

Run code to see output...