<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lộc Nguyễn</title>
    <description>The latest articles on DEV Community by Lộc Nguyễn (@tanloc).</description>
    <link>https://dev.to/tanloc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2147535%2F1bdcdcb7-78dc-481b-8c40-34635369eec3.png</url>
      <title>DEV Community: Lộc Nguyễn</title>
      <link>https://dev.to/tanloc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tanloc"/>
    <language>en</language>
    <item>
      <title>How a Wrong Singleton Implementation Crushed Our Redis Connections in NestJS</title>
      <dc:creator>Lộc Nguyễn</dc:creator>
      <pubDate>Mon, 02 Feb 2026 17:05:35 +0000</pubDate>
      <link>https://dev.to/tanloc/how-a-wrong-singleton-implementation-crushed-our-redis-connections-in-nestjs-56b7</link>
      <guid>https://dev.to/tanloc/how-a-wrong-singleton-implementation-crushed-our-redis-connections-in-nestjs-56b7</guid>
      <description>&lt;h1&gt;
  
  
  Singleton Pattern in NestJS: Don't Let Legacy Code Choke Your System
&lt;/h1&gt;

&lt;p&gt;Hello everyone! Recently, my team noticed a significant performance drop after deploying new features. Despite scaling, the system became sluggish. After a deep dive, I discovered that the culprit was a "legacy" implementation of the &lt;strong&gt;Singleton pattern&lt;/strong&gt; for our &lt;code&gt;ClientProxy&lt;/code&gt; service (the service responsible for microservice communication via Redis).&lt;/p&gt;

&lt;p&gt;Here is a breakdown of what went wrong and how we fixed it.&lt;/p&gt;




&lt;h2&gt;
  
  
  I. The Singleton Pattern
&lt;/h2&gt;

&lt;p&gt;For those who might need a refresher: The &lt;strong&gt;Singleton pattern&lt;/strong&gt; ensures that a class has only one instance throughout the application's lifecycle. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This instance is created once when the app starts.&lt;/li&gt;
&lt;li&gt;All other services share this single instance instead of creating new ones, which is crucial for resource-heavy tasks like database or message broker connections.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  II. Dependency Injection (DI) in NestJS
&lt;/h2&gt;

&lt;p&gt;Singleton behavior is tightly coupled with Dependency Injection. In NestJS, &lt;strong&gt;Singleton is the default scope&lt;/strong&gt; for DI.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;During the bootstrap phase, NestJS registers these dependencies in the &lt;strong&gt;IoC (Inversion of Control) Container&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;To use a service, you simply "inject" it via the class constructor, and NestJS handles the rest.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  III. The "Legacy" Mistake: When DI Goes Wrong
&lt;/h2&gt;

&lt;p&gt;In NestJS, the &lt;code&gt;@Injectable()&lt;/code&gt; decorator defaults to a Singleton scope. If you need a service to be available everywhere (like Config, Logger, or a Shared Client), you can use the &lt;code&gt;@Global()&lt;/code&gt; decorator.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Issue We Faced:
&lt;/h3&gt;

&lt;p&gt;Despite the service being marked as a Singleton, the legacy code was manually re-declaring the service in the &lt;code&gt;providers&lt;/code&gt; array of multiple modules and overusing the &lt;code&gt;@Inject()&lt;/code&gt; annotation unnecessarily. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Result:&lt;/strong&gt; Developers followed the "copy-paste" pattern. Every time a new service needed to talk to another microservice, it inadvertently triggered a &lt;strong&gt;new instance&lt;/strong&gt; of &lt;code&gt;ClientProxy&lt;/code&gt;. This led to an explosion of active connections to Redis, creating a massive &lt;strong&gt;bottleneck&lt;/strong&gt; and slowing down the entire infrastructure.&lt;/p&gt;




&lt;h2&gt;
  
  
  IV. The Fix
&lt;/h2&gt;

&lt;p&gt;The solution was straightforward but required a thorough cleanup:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Centralized Module:&lt;/strong&gt; Created a dedicated &lt;code&gt;ClientProxyModule&lt;/code&gt; to house the &lt;code&gt;ClientProxyService&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Global Decorator:&lt;/strong&gt; Added the &lt;code&gt;@Global()&lt;/code&gt; decorator to this module so it’s initialized once at the root level.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refactoring:&lt;/strong&gt; Removed redundant provider declarations and unnecessary &lt;code&gt;@Inject()&lt;/code&gt; annotations from other services. We switched back to standard constructor injection.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  V. Pro-Tips for Debugging
&lt;/h2&gt;

&lt;p&gt;How do you know if you've handled Singletons correctly? Here are two quick tricks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Constructor Log:&lt;/strong&gt; Add a &lt;code&gt;console.log&lt;/code&gt; inside the &lt;code&gt;constructor&lt;/code&gt; of your service. Restart the app. If you see that log &lt;strong&gt;more than once&lt;/strong&gt;, you have an instantiation leak. It should only fire once during startup.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Getter Log:&lt;/strong&gt; If you use a getter method, add a log there to monitor exactly when and where the service is being accessed.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  VI. Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Whether you are using NestJS, Spring Boot, or .NET, the core principle of the &lt;strong&gt;Singleton Pattern&lt;/strong&gt; remains vital. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Understand the Scope:&lt;/strong&gt; Don't just copy-paste. Understand how your framework manages instances.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Audit your Connections:&lt;/strong&gt; Whether it's a Monolith or Microservices, "blind injection" can lead to resource exhaustion.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check your current projects—are you injecting services recklessly? Let's keep our connections clean and our systems fast.&lt;/p&gt;

&lt;p&gt;Happy coding! 🚀&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>singleton</category>
      <category>designpatterns</category>
      <category>microservices</category>
    </item>
  </channel>
</rss>
