AsyncLocalStorage

Creator
Creator
Seonglae ChoSeonglae Cho
Created
Created
2020 Nov 2 6:33
Editor
Edited
Edited
2026 Jan 7 18:46
Refs
Refs
Libuv

Provides functions to track asynchronous resources

Called when a promise starts or ends, receiving an ID

AsyncLocalStorage is fine and stable

(1) It overlays a current execution context pointer on top of the async resource graph to automatically propagate values. Rather than storing "in one place" like a global variable, it stores in a context grouped by async chain (request chain).
How does AsyncLocalStorage have a separate store for each request? Node internally creates an async resource (Timeout, Promise, FSReq, TCPWrap, etc.) whenever an asynchronous operation is created. Each resource has a unique asyncId, and when a new resource is created, it has a triggerAsyncId indicating "who created it". In other words, asynchronous operations form a parent-child relationship graph. So when you call als.run(store, fn) at the request starting point, storage can be safely propagated to all async resources created during fn execution; there is a store that is commonly referenced by async resources created in the request chain
However, note that ALS can break in certain cases: when the request starting point is not properly wrapped with run(): store becomes undefined, libraries/patterns that break context propagation: (certain native
N-API
addons, worker thread boundaries are thread-local so they're separate)

Raw async_hooks are Experimental

 
 
 
Async hooks | Node.js v24.10.0 Documentation
We strongly discourage the use of the async_hooks API. Other APIs that can cover most of its use cases include:
NodeJS에서 async_hooks을 이용해 요청의 고유한 context 사용하기
이 글은 Guillaume Bession의 개인 블로그에 올라와있는 Getting per-request context in NodeJS with async_hooks 글의 번역글입니다.저는 최근 NodeJS로 HTTP 서버를 개발하던 도중 문제를 맞닥트리게 되었습니다. 저는 제 코드베이스의 많은 부분에서 로깅을 하고 있었고, 각 요청별로 유니크ID를 가졌습니다. 저는 이 유니크한 ID를 각 요청 안에서 무슨 일이 일어나고 있는지
NodeJS에서 async_hooks을 이용해 요청의 고유한 context 사용하기
nest
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
Documentation | NestJS - A progressive Node.js framework
aws lambda
Node.js runtime for Lambda Managed Instances - AWS Lambda
For Node.js runtimes, Lambda Managed Instances uses worker threads with async/await-based execution to handle concurrent requests.
Node.js runtime for Lambda Managed Instances - AWS Lambda
 
 

Recommendations