Have you ever ended passing request, tenant or session session information around in your function call chain and asked yourself how could you make this look cleaner? Well there is a solution in Node.js! Welcome to AsyncLocalStorage (ALS)! Many languages and runtimes provide thread-local storage (TLS). In thread-per-request servers, TLS can be used to store request-scoped context and access it anywhere in the call stack. In Node.js we have something similar, although we don't use threads to process the requests, we use async contexts. Think of ALS as “thread-local storage” for async code: it lets you attach a small context object to the current async execution chain (Promises, async/await, timers, etc.) and read it anywhere downstream without having to pass that context data around on every function call, effectively making the function/method signature leaner. What it’s great for 🔎 Log correlation (requestId in every log line) 📈 Tracing/observability (span ids, metadata) 🧩 Request-scoped context (tenant/user, feature flags) 🧪 Diagnostics (debugging async flows) But with great power comes great responsibility, (sorry for the joke). A misused ALS can cause context leak to other requests and if not carefully designed you can start losing control of where things are set and where things are read. To solve this I like to treat ALS similar to a "Redux Store Slice", so each piece of related data I need to store in the ALS is a Slice. So I have slices for: auth, DB connections, soft delete behaviors, request logging, etc. And those slices are only set at the middleware level (or in Guards/Interceptors/Pipes if you use NestJS). Have you used ALS in production? What was your main win (or gotcha)? #nodejs #javascript #backend #nestjs #distributedtracing #cleanarchitecture
Nice post! 👍 Thanks for sharing!
Good post 👏 Thanks for sharing