<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tom Nachman on Medium]]></title>
        <description><![CDATA[Stories by Tom Nachman on Medium]]></description>
        <link>https://medium.com/@tnachman?source=rss-c6febabc6343------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*QRl6X23K3tI91WsrFTopSg.jpeg</url>
            <title>Stories by Tom Nachman on Medium</title>
            <link>https://medium.com/@tnachman?source=rss-c6febabc6343------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 09:25:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tnachman/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[on Scenario 1: Basic Indexing, show the output of the 'explain analyze' so it will be clearer that…]]></title>
            <link>https://medium.com/@tnachman/on-scenario-1-basic-indexing-show-the-output-of-the-explain-analyze-so-it-will-be-clearer-that-63b3743a9228?source=rss-c6febabc6343------2</link>
            <guid isPermaLink="false">https://medium.com/p/63b3743a9228</guid>
            <dc:creator><![CDATA[Tom Nachman]]></dc:creator>
            <pubDate>Wed, 06 Sep 2023 14:58:03 GMT</pubDate>
            <atom:updated>2023-09-06T14:58:03.634Z</atom:updated>
            <content:encoded><![CDATA[<p>on Scenario 1: Basic Indexing, show the output of the &#39;explain analyze&#39; so it will be clearer that index on the &#39;email&#39; column helped</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=63b3743a9228" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[since you mention that there will be second part.]]></title>
            <link>https://medium.com/@tnachman/since-you-mention-that-there-will-be-second-part-f58f3e728f47?source=rss-c6febabc6343------2</link>
            <guid isPermaLink="false">https://medium.com/p/f58f3e728f47</guid>
            <dc:creator><![CDATA[Tom Nachman]]></dc:creator>
            <pubDate>Wed, 06 Sep 2023 14:55:47 GMT</pubDate>
            <atom:updated>2023-09-06T14:55:47.958Z</atom:updated>
            <content:encoded><![CDATA[<p>since you mention that there will be second part. in the start of the article point that this is part 1</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f58f3e728f47" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[on the Hash indexing section i would olaso mention that since it uses equality checks(=) If we…]]></title>
            <link>https://medium.com/@tnachman/on-the-hash-indexing-section-i-would-olaso-mention-that-since-it-uses-equality-checks-if-we-c073c1130eaf?source=rss-c6febabc6343------2</link>
            <guid isPermaLink="false">https://medium.com/p/c073c1130eaf</guid>
            <dc:creator><![CDATA[Tom Nachman]]></dc:creator>
            <pubDate>Wed, 06 Sep 2023 14:52:31 GMT</pubDate>
            <atom:updated>2023-09-06T14:52:31.576Z</atom:updated>
            <content:encoded><![CDATA[<p>on the Hash indexing section i would olaso mention that since it uses equality checks(=) If we specify a separate operator such as (pattern matching), it does not use the hash index.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c073c1130eaf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[i would change the Frequent Searches bullet to:]]></title>
            <link>https://medium.com/@tnachman/i-would-change-the-frequent-searches-bullet-to-804392fa490c?source=rss-c6febabc6343------2</link>
            <guid isPermaLink="false">https://medium.com/p/804392fa490c</guid>
            <dc:creator><![CDATA[Tom Nachman]]></dc:creator>
            <pubDate>Wed, 06 Sep 2023 14:34:23 GMT</pubDate>
            <atom:updated>2023-09-06T14:34:23.144Z</atom:updated>
            <content:encoded><![CDATA[<p>i would change the <strong>Frequent Searches</strong> bullet to:</p><p><strong>Frequent</strong> <strong>Searches</strong>: When your application heavily relies on regular searches within specific columns, indexing is the optimal choice. It is essential to always create indexes for the most frequently executed and resource-intensive queries.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=804392fa490c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Utilizing the power of AsyncLocalStorage to build tenant-aware NestJS service]]></title>
            <link>https://medium.com/bigidev-bigid-engineering/utilizing-the-power-of-asynclocalstorage-to-build-tenant-aware-nestjs-service-27b7d01e2464?source=rss-c6febabc6343------2</link>
            <guid isPermaLink="false">https://medium.com/p/27b7d01e2464</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[nestjs]]></category>
            <category><![CDATA[multitenancy]]></category>
            <dc:creator><![CDATA[Tom Nachman]]></dc:creator>
            <pubDate>Wed, 25 May 2022 17:12:44 GMT</pubDate>
            <atom:updated>2022-05-30T09:03:18.817Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cpSi1gRRRPtJpcYA.jpg" /></figure><p>There’s a lot of information about multi-tenant architecture online, but not much about the implementation in NodeJS and the ease of execution that AsyncLocalStorage can provide. In this post, we’ll create a <strong>simple tenant-aware NestJS service which will utilize the power of </strong><a href="https://nodejs.org/docs/latest-v16.x/api/async_context.html#class-asynclocalstorage"><strong>AsyncLocalStorage</strong></a><strong> to pass tenant IDs</strong>.</p><h4>Introduction</h4><p>Many software vendors turn to software as a service — SaaS — to enable rapid scale. SaaS provides a new form of quick distribution and achieves economies of scale because customers share the same code.</p><p>When it comes to software, customers often have their own requirements. Implementing all of these requests and delivering the software according to each customer’s needs separately causes development time to increase. It’s also very difficult and expensive to maintain multiple implementations of the product.</p><p>Multi-tenancy can provide the solution by helping the provider to satisfy customers with their specific needs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tJxgZksnrQ496hF2.jpg" /></figure><h4><strong>What is Multi-tenancy?</strong></h4><p>Multi-tenancy is a reference to the mode of software operation where many independent instances of one or multiple applications operate in a shared environment. The instances (tenants) are logically isolated, but physically integrated.</p><h4><strong>How does a multi-tenant architecture work?</strong></h4><p>The best way to describe this architecture is with a simple example.<br>Let’s take Jira, the most popular online tool for managing project tasks, tracking errors and issues, and for operational project management. In Jira, each organization has its own dashboard, accessed via a custom subdomain where A and B have access to the same features, receive the same updates, but the issues, tickets, comments, users, etc. of A can’t be accessed by B, and vice-versa.</p><p>Slack is another example of multi-tenancy and works in the same way as Jira — but, of course, in this case, the components that need to be kept separate are users, channels, PMs, notifications, and so on.</p><p>We’ve now covered the definition of multi-tenant architecture and how it works, but if you still have questions, here’s a blog that can help: <a href="https://digitalguardian.com/blog/saas-single-tenant-vs-multi-tenant-whats-difference">Single Tenant vs Multi Tenant</a>.</p><h4>NodeJS and Multi-tenancy?</h4><p>One of the benefits of the multi-tenant NodeJS application is the maintainability of the code base. The code will always be the same for all clients (customers). If a client reports a problem, the solution will be applied to all their other clients. The downside is that if you introduce an an error, it will apply to all clients<em>.</em> And what happens with the administration of the database? Maybe it’s a little more complicated, but following the appropriate patterns and conventions, it’s easily managed. There are different approaches to managing databases which we won’t cover in this article, but you can read more about them <a href="https://docs.microsoft.com/en-us/azure/azure-sql/database/saas-tenancy-app-design-patterns?view=azuresql">here</a>.</p><p>Some of you who managed to look ahead to the practice and implementation of the multi-tenancy architecture in your code base might have noticed a small problem. Let’s say we passed the stage of making our DB tenant-aware and we attached the tenant ID to each API request. Almost every function in our code that eventually accesses the DB (Redis, MongoDB, etc.) needs to get another parameter. This parameter is the tenant&#39;s ID which we’ll need when we call our DB functions.</p><p>To solve this, we’ll use a new feature of NodeJS called ‘AsyncLocalStorage’.</p><h4>What is NodeJS AsyncLocalStorage?</h4><p>According to the <a href="https://nodejs.org/docs/latest-v16.x/api/async_context.html#class-asynclocalstorage">NodeJS official documentation </a>: “AsyncLocalStorage is used to create asynchronous state within callbacks and promise chains. It allows storing data throughout the lifetime of a web request or any other asynchronous duration. It is similar to thread-local storage in other languages.”</p><p>To simplify the explanation further, AsyncLocalStorage allows you to store a state when executing an async function, and then make it available to all the code paths within that function.</p><h4>Why do we need that functionality?</h4><p>Unlike other languages (like PHP) NodeJS isn’t a threaded language.<br>In PHP, every HTTP request creates a new thread, and each thread has its memory. This allows you to store the state into the global memory and access it anywhere inside your codebase.</p><p>In NodeJS, you can’t save data to a global object and keep it isolated between HTTP requests. This is impossible because NodeJS runs in a single thread and shares the memory across all the HTTP requests.</p><p>This is where NodeJS gains a lot of performance — it doesn’t have to boot the application for every single HTTP request.</p><p>Let’s assume we get the tenantId from the headers of the request and we want to do some work for that specific tenant.</p><p>The first solution that comes to mind is to construct a counter called tenantId:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eddc9fe7e9b0f02bf2d69de08e673a04/href">https://medium.com/media/eddc9fe7e9b0f02bf2d69de08e673a04/href</a></iframe><p>We can see that all our function arguments are polluted with this additional parameter and it can become really tedious when working with large projects.</p><p>The second approach, which is no better than the first, is to use a global variable and assign our data to it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ff07ae3d04b5f92ece77534a2378e4d/href">https://medium.com/media/0ff07ae3d04b5f92ece77534a2378e4d/href</a></iframe><p>But it would fail spectacularly when we have more than one request on hand. This is because Node wouldn’t execute asynchronous code serially (of course, that’s the definition of asynchronous!) which will cause wrong data to be logged, or it might even create race conditions.</p><p>Now, what if we want to persist specific data with that specific user whenever the asynchronous code specific to them is called? Here’s when you use <strong>AsyncLocalStorage — storage for asynchronous flows in NodeJS.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b70415324f2e8f82d2fb277b5eb5a750/href">https://medium.com/media/b70415324f2e8f82d2fb277b5eb5a750/href</a></iframe><p>This example shows the “stickiness” of the tenantId with the respective request which allows isolated states between multiple async operations. You can imagine howexpress populates the req.session object with the correct user every time. In a similar fashion, this is a low level API using a lower level construct in Node called async_hooks.</p><h4>Implementing Async Tenant Context interceptor with NestJS</h4><p>After dealing with a basic example of using AsyncLocalStorage the functions are in the same file , let’s start building our own AsyncTenantContext interceptor!</p><p>First, we need to build our service: async-tenant-context.service.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fc1eb56f43e00bab685cf7dc88724a2/href">https://medium.com/media/9fc1eb56f43e00bab685cf7dc88724a2/href</a></iframe><p>After building the service, we can start building our Async Tenant Context. For convenience, we will build this module as a dynamic module to provide options to import it with existing AsyncLocalStorage instance or, if we want to, register this module as global.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8d4b2f1b8fd3510992e3ff4f8065551/href">https://medium.com/media/d8d4b2f1b8fd3510992e3ff4f8065551/href</a></iframe><p>Now its up to you if you want the new AsyncTenantService in a NestJS interceptor or middleware.<br>Note that interceptors have access to request/response before and after the route handler is called. Middleware is only called before the route handler, which means they have access to the response object, but you don’t have the result of the route handler.</p><p>In our example we will create a NestJS interceptor:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51c3dd0746602d78d6a0a9f01024fbf4/href">https://medium.com/media/51c3dd0746602d78d6a0a9f01024fbf4/href</a></iframe><p>Our last step is to register our interceptor as a global interceptor in app.module.ts .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e45f27d970ce15e2190c96f09007a4c/href">https://medium.com/media/4e45f27d970ce15e2190c96f09007a4c/href</a></iframe><p>Now all you have to do to extract the tenantId from the context is use the AsyncTenantContext service as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0bbf44598151862e21f92bfd9abbbe38/href">https://medium.com/media/0bbf44598151862e21f92bfd9abbbe38/href</a></iframe><h4>Conclusion</h4><p>This example just uses a simple nesting level , so the benefit of using AsyncLocalStorage may not seem obvious. However, in a larger project where the function stack is several layers deep and is spread across different files, you can imagine the usefulness of having access to the synchronous state within callbacks and promise chains.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27b7d01e2464" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bigidev-bigid-engineering/utilizing-the-power-of-asynclocalstorage-to-build-tenant-aware-nestjs-service-27b7d01e2464">Utilizing the power of AsyncLocalStorage to build tenant-aware NestJS service</a> was originally published in <a href="https://medium.com/bigidev-bigid-engineering">BigIDEV — BigID Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>