Continuation Local Storage for easy context passing in Node.js

Roman Coedo
Oct 14, 2019 · 2 min read
Image for post
Image for post
Photo by Suad Kamardeen on Unsplash

Recently I’ve been working on a Node.js project where we needed to keep track of which requests were generating calls to some other pieces of code. Passing a request identifier around was not viable since it would require to change too many APIs.

We solved the problem using cls-hooked, a small package that uses the async_hooks experimental Node.js API to implement Continuation Local Storage.

What is Continuation Local Storage?

CLS is a mechanism that allows the attachment of data to the current asynchronous execution context. It uses async_hooks to keep track of asynchronous context changes and to load and unload the data associated with it.

Building an Express middleware

Our express middleware is going to be quite simple. We’re going to generate an uuid and store it in the namespace.

As you can see, the set method is called within a run. The requestId value is going to be available from any code called inside that run block, and that’s why we are calling next() inside the run as well. Calling next() inside the run will make the requestId available for any other middleware and controller in our express application.

We can now build our complete express application and use the requestId anywhere:

A word on performance

The async_hooks API is still experimental so it could possibly change in the future, but what is worse is that enabling it has a big impact on performance. This is currently being discussed in this github issue.

Benedikt Meurer wrote this great benchmark comparing performances with and without async_hooks enabled.

If you are writing a critical piece of code you probably want to stay away from async_hooks. You’d also want to stay away from async_hooks if you’re writing a library, since you could impact performance for your users just for your convenience. If the problem you’re dealing with does not match neither of these scenarios, CLS may be a great way of making things simpler.

Enjoy!

Trabe

We are a development studio.

Thanks to David Barral and Clara Dopico

Roman Coedo

Written by

Software developer @Trabe. Coffee driven development expert.

Trabe

Trabe

We are a development studio. We use Java, Rails, and JavaScript. This is where we write about the technologies we use at Trabe.

Roman Coedo

Written by

Software developer @Trabe. Coffee driven development expert.

Trabe

Trabe

We are a development studio. We use Java, Rails, and JavaScript. This is where we write about the technologies we use at Trabe.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store