<?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: Nsikan Patrick Adaowo</title>
    <description>The latest articles on DEV Community by Nsikan Patrick Adaowo (@nsikanadaowo).</description>
    <link>https://dev.to/nsikanadaowo</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3061717%2F705a3df2-2eb9-4d99-8071-08fcc00c9b4a.png</url>
      <title>DEV Community: Nsikan Patrick Adaowo</title>
      <link>https://dev.to/nsikanadaowo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nsikanadaowo"/>
    <language>en</language>
    <item>
      <title>Coupling vs. Cohesion: The Two Forces That Shape Good Software</title>
      <dc:creator>Nsikan Patrick Adaowo</dc:creator>
      <pubDate>Thu, 17 Sep 2026 04:31:45 +0000</pubDate>
      <link>https://dev.to/nsikanadaowo/coupling-vs-cohesion-the-two-forces-that-shape-good-software-36gg</link>
      <guid>https://dev.to/nsikanadaowo/coupling-vs-cohesion-the-two-forces-that-shape-good-software-36gg</guid>
      <description>&lt;p&gt;Good software is not only about making each component work.&lt;/p&gt;

&lt;p&gt;It is also about deciding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What belongs together?&lt;/li&gt;
&lt;li&gt;What should stay separate?&lt;/li&gt;
&lt;li&gt;Which modules should know about each other?&lt;/li&gt;
&lt;li&gt;How much knowledge should one component have about another?&lt;/li&gt;
&lt;li&gt;What should happen when one part of the system changes?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two concepts help answer these questions:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cohesion: how closely related the responsibilities inside a module are.&lt;/p&gt;

&lt;p&gt;Coupling: how dependent one module is on other modules.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The goal is usually simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;High cohesion within modules and low coupling between modules.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This sounds like a theoretical software engineering principle, but it directly affects how easy your system is to understand, test, scale, and change.&lt;/p&gt;

&lt;h2&gt;
  
  
  It works, but is that enough?
&lt;/h2&gt;

&lt;p&gt;When developers build a feature under pressure, the first goal is usually to make it work.&lt;/p&gt;

&lt;p&gt;That is understandable.&lt;/p&gt;

&lt;p&gt;A client needs the feature. A product manager wants the release. A deadline is approaching. You write the endpoint, connect the database, call the external provider, return the response, and move on.&lt;/p&gt;

&lt;p&gt;At that moment, the code may look perfectly fine.&lt;/p&gt;

&lt;p&gt;The trouble starts when the system grows.&lt;/p&gt;

&lt;p&gt;A simple order service begins to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Validate customer information.&lt;/li&gt;
&lt;li&gt;Calculate prices.&lt;/li&gt;
&lt;li&gt;Apply discounts.&lt;/li&gt;
&lt;li&gt;Check inventory.&lt;/li&gt;
&lt;li&gt;Process payment.&lt;/li&gt;
&lt;li&gt;Send emails.&lt;/li&gt;
&lt;li&gt;Publish events.&lt;/li&gt;
&lt;li&gt;Write audit logs.&lt;/li&gt;
&lt;li&gt;Generate invoices.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A small change to the payment provider now requires opening the order service. A change to email behaviour affects order creation. A new discount rule breaks an unrelated checkout flow.&lt;br&gt;
The code still works—but it has become expensive to change.&lt;/p&gt;

&lt;p&gt;This is often a design problem involving coupling and cohesion.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is cohesion?
&lt;/h2&gt;

&lt;p&gt;Cohesion describes how closely related the responsibilities inside a module are.&lt;/p&gt;

&lt;p&gt;A highly cohesive module focuses on one clear purpose.&lt;/p&gt;

&lt;p&gt;For example, an authentication module may contain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Login.&lt;/li&gt;
&lt;li&gt;Logout.&lt;/li&gt;
&lt;li&gt;Token refresh.&lt;/li&gt;
&lt;li&gt;Password reset.&lt;/li&gt;
&lt;li&gt;Session validation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These responsibilities are related because they all belong to identity and access management.&lt;/p&gt;

&lt;p&gt;A module that contains authentication, invoice generation, image resizing, and product recommendations has low cohesion.&lt;/p&gt;

&lt;p&gt;The code may still compile, but the module does not have a clear identity.&lt;/p&gt;

&lt;p&gt;A simple analogy&lt;br&gt;
Imagine a toolbox.&lt;/p&gt;

&lt;p&gt;A toolbox containing screwdrivers, pliers, measuring tape, and a wrench has a clear purpose: helping you perform repairs.&lt;/p&gt;

&lt;p&gt;Now imagine opening the same toolbox and finding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cooking utensils.&lt;/li&gt;
&lt;li&gt;Medical supplies.&lt;/li&gt;
&lt;li&gt;Office stationery.&lt;/li&gt;
&lt;li&gt;Car keys.&lt;/li&gt;
&lt;li&gt;Gardening tools.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The box may contain useful things, but it is no longer organized around one meaningful purpose.&lt;/p&gt;

&lt;p&gt;Low-cohesion software feels the same way.&lt;/p&gt;

&lt;p&gt;You find unrelated functionality in the same class or module, and you are never sure where the next responsibility should go.&lt;/p&gt;

&lt;p&gt;High cohesion in code&lt;br&gt;
Consider this service:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjtekyvmd48c6xbj2ooym.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjtekyvmd48c6xbj2ooym.png" alt=" " width="672" height="708"&gt;&lt;/a&gt;&lt;br&gt;
The name UserService does not explain the actual boundaries.&lt;/p&gt;

&lt;p&gt;Some methods deal with users. Others deal with communication, billing, and reporting.&lt;/p&gt;

&lt;p&gt;A more cohesive design might separate them:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2jwq7vthmpld88boa8lp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2jwq7vthmpld88boa8lp.png" alt=" " width="772" height="1154"&gt;&lt;/a&gt;&lt;br&gt;
Now each module has a clearer purpose.&lt;/p&gt;

&lt;p&gt;When a developer needs to change password-reset behavior, they know where to look.&lt;/p&gt;

&lt;p&gt;When invoice requirements change, authentication code is less likely to be affected.&lt;/p&gt;

&lt;p&gt;That is the practical benefit of cohesion: related things stay together, and unrelated things stay apart.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is coupling?
&lt;/h2&gt;

&lt;p&gt;Coupling describes the degree of dependency between different modules.&lt;/p&gt;

&lt;p&gt;Two modules are tightly coupled when one knows too much about the other or depends heavily on its internal details.&lt;/p&gt;

&lt;p&gt;A change in one module may force changes in several others.&lt;/p&gt;

&lt;p&gt;Consider:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fly6gtuyujygs0216ohit.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fly6gtuyujygs0216ohit.png" alt=" " width="800" height="615"&gt;&lt;/a&gt;&lt;br&gt;
This service is coupled to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Paystack.&lt;/li&gt;
&lt;li&gt;Prisma.&lt;/li&gt;
&lt;li&gt;A specific payment response format.&lt;/li&gt;
&lt;li&gt;A specific database implementation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you replace Paystack, change the database library, or alter the payment workflow, this service must change.&lt;/p&gt;

&lt;p&gt;That is tight coupling.&lt;/p&gt;

&lt;p&gt;A loosely coupled design depends on stable contracts instead:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd1payf0pkwtw95g84g9p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd1payf0pkwtw95g84g9p.png" alt=" " width="800" height="430"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The order service depends on capabilities, not concrete infrastructure:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F41l8vq4ey3plunhsgv6f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F41l8vq4ey3plunhsgv6f.png" alt=" " width="800" height="751"&gt;&lt;/a&gt;&lt;br&gt;
Now the payment provider and database implementation can change behind the abstractions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cohesion and coupling work together
&lt;/h2&gt;

&lt;p&gt;Cohesion and coupling are not competing goals.&lt;/p&gt;

&lt;p&gt;They describe two sides of software structure:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cohesion looks inside a module.&lt;/li&gt;
&lt;li&gt;Coupling looks between modules.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;A healthy design aims for:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High cohesion inside&lt;/li&gt;
&lt;li&gt;Low coupling between&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of a well-organized company.&lt;/p&gt;

&lt;p&gt;Each department focuses on a specific responsibility:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Finance handles finances.&lt;/li&gt;
&lt;li&gt;Human resources handles people operations.&lt;/li&gt;
&lt;li&gt;Sales handles customers and revenue.&lt;/li&gt;
&lt;li&gt;Engineering handles product development.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The departments still communicate, but they do so through clear responsibilities and defined processes.&lt;/p&gt;

&lt;p&gt;If every department performs every task, the organization becomes chaotic.&lt;/p&gt;

&lt;p&gt;If departments cannot communicate at all, work also stops.&lt;/p&gt;

&lt;p&gt;Software modules behave similarly.&lt;/p&gt;

&lt;p&gt;The goal is not zero coupling.&lt;/p&gt;

&lt;p&gt;The goal is controlled coupling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why zero coupling is impossible
&lt;/h2&gt;

&lt;p&gt;Every useful system contains dependencies.&lt;/p&gt;

&lt;p&gt;An order system needs payment information. A dashboard needs reporting data. A notification service needs information about events. A controller needs an application service.&lt;/p&gt;

&lt;p&gt;The goal is not to remove every dependency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The goal is to make dependencies:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Necessary.&lt;/li&gt;
&lt;li&gt;Explicit.&lt;/li&gt;
&lt;li&gt;Stable.&lt;/li&gt;
&lt;li&gt;Small.&lt;/li&gt;
&lt;li&gt;Easy to replace.&lt;/li&gt;
&lt;li&gt;Easy to test.&lt;/li&gt;
&lt;li&gt;Easy to understand.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A controller depending on an application service is normal.&lt;/p&gt;

&lt;p&gt;An application service knowing the private implementation details of three databases, two payment providers, and an email server is a warning sign.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of coupling
&lt;/h2&gt;

&lt;p&gt;Coupling can appear in different forms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Content coupling&lt;/strong&gt;&lt;br&gt;
One module directly accesses or modifies the internal data of another module.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftqkgrxszxlu1gl9zj80u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftqkgrxszxlu1gl9zj80u.png" alt=" " width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is highly dangerous because the consuming module depends on implementation details.&lt;/p&gt;

&lt;p&gt;If internalState changes, the order service breaks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common coupling&lt;/strong&gt;&lt;br&gt;
Multiple modules depend on shared global state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvy06p1mx4tezkuximec8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvy06p1mx4tezkuximec8.png" alt=" " width="800" height="443"&gt;&lt;/a&gt;&lt;br&gt;
Global state can make behavior difficult to predict and test.&lt;/p&gt;

&lt;p&gt;One module may modify the state while another module assumes it has not changed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Control coupling&lt;/strong&gt;&lt;br&gt;
One module tells another how to behave by passing flags.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F64nov1l2dilblmwkx3us.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F64nov1l2dilblmwkx3us.png" alt=" " width="800" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This can become problematic when the receiving service accumulates many branches:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1rgyo7s27ryv6on1koue.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1rgyo7s27ryv6on1koue.png" alt=" " width="799" height="458"&gt;&lt;/a&gt;&lt;br&gt;
Sometimes a strategy or channel abstraction is clearer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data coupling&lt;/strong&gt;&lt;br&gt;
One module passes only the data another module needs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvlwww5dg7w8vkbcj28m8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvlwww5dg7w8vkbcj28m8.png" alt=" " width="688" height="522"&gt;&lt;/a&gt;&lt;br&gt;
This is generally healthier because the dependency is explicit and limited.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Message coupling&lt;/strong&gt;&lt;br&gt;
Modules communicate through messages or events without depending heavily on each other’s internal structure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0pvoggj8bcbs4zxpq2a3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0pvoggj8bcbs4zxpq2a3.png" alt=" " width="672" height="522"&gt;&lt;/a&gt;&lt;br&gt;
Message-based communication can reduce direct coupling, though it introduces other concerns such as event versioning, delivery guarantees, retries, and eventual consistency.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of cohesion
&lt;/h2&gt;

&lt;p&gt;Cohesion can also vary in strength.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functional cohesion&lt;/strong&gt;&lt;br&gt;
A module contains elements that work together to perform one well-defined task.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8j4f82k74ecvhd8vv82t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8j4f82k74ecvhd8vv82t.png" alt=" " width="800" height="401"&gt;&lt;/a&gt;&lt;br&gt;
Both methods belong to password hashing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sequential cohesion&lt;/strong&gt;&lt;br&gt;
One operation produces data consumed by another operation within the same module.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fafbrzj5fygfjesz697fv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fafbrzj5fygfjesz697fv.png" alt=" " width="688" height="558"&gt;&lt;/a&gt;&lt;br&gt;
These operations form a clear sequence in a file-import workflow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Communicational cohesion&lt;/strong&gt;&lt;br&gt;
Several operations work on the same data.&lt;/p&gt;

&lt;p&gt;For example, a profile module may validate, update, and format user-profile data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Coincidental cohesion&lt;/strong&gt;&lt;br&gt;
A module contains unrelated utilities simply because they were created around the same time.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjx4n9iror1hhviark3ei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjx4n9iror1hhviark3ei.png" alt=" " width="704" height="596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This kind of module often grows into a dumping ground.&lt;/p&gt;

&lt;p&gt;The problem with utility folders is not that utilities are always bad. The problem is that unrelated behaviour becomes difficult to own, test, and discover.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A practical example: order processing&lt;/strong&gt;&lt;br&gt;
Let us compare two designs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Low cohesion and high coupling&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzsnsj1tusete2j20akip.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzsnsj1tusete2j20akip.png" alt=" " width="800" height="657"&gt;&lt;/a&gt;&lt;br&gt;
This service has many responsibilities.&lt;/p&gt;

&lt;p&gt;It is also coupled to several infrastructure systems.&lt;/p&gt;

&lt;p&gt;Possible consequences:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Large and slow tests.&lt;/li&gt;
&lt;li&gt;Difficult mocking.&lt;/li&gt;
&lt;li&gt;High risk of regression.&lt;/li&gt;
&lt;li&gt;Unclear error handling.&lt;/li&gt;
&lt;li&gt;Difficult reuse.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every change affects the same class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;High cohesion and controlled coupling&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnqg9lb8u8mmht0nulfc7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnqg9lb8u8mmht0nulfc7.png" alt=" " width="551" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each component owns a focused responsibility.&lt;/p&gt;

&lt;p&gt;The use case still coordinates the workflow, but the details are distributed across meaningful boundaries.&lt;/p&gt;

&lt;p&gt;This design is not automatically perfect.&lt;/p&gt;

&lt;p&gt;It may introduce more files and abstractions.&lt;/p&gt;

&lt;p&gt;But if the application is complex and these concerns change independently, the separation creates value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coupling in a NestJS application
&lt;/h2&gt;

&lt;p&gt;NestJS encourages dependency injection, which can help reduce coupling.&lt;/p&gt;

&lt;p&gt;A service can receive dependencies through its constructor:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F69wnsza3zn19kbdll89y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F69wnsza3zn19kbdll89y.png" alt=" " width="799" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is better than constructing concrete dependencies inside the service:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F39keyq73xd683pjkzd10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F39keyq73xd683pjkzd10.png" alt=" " width="800" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Constructor injection makes dependencies visible.&lt;/p&gt;

&lt;p&gt;It also makes testing easier:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fj5mt5paynp8n8bko4902.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fj5mt5paynp8n8bko4902.png" alt=" " width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The service can be tested without connecting to a real database or sending real emails.&lt;/p&gt;

&lt;p&gt;However, dependency injection alone does not guarantee low coupling.&lt;/p&gt;

&lt;p&gt;You can still inject a massive concrete service that exposes too many responsibilities.&lt;/p&gt;

&lt;p&gt;The quality of the boundary matters more than the presence of the injection mechanism.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coupling in modular monoliths
&lt;/h2&gt;

&lt;p&gt;A modular monolith may run as one deployable application while maintaining strong internal boundaries.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzcrsetr0ahl7numlxiie.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzcrsetr0ahl7numlxiie.png" alt=" " width="533" height="135"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The modules can communicate through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Public application interfaces.&lt;/li&gt;
&lt;li&gt;Domain events.&lt;/li&gt;
&lt;li&gt;Commands.&lt;/li&gt;
&lt;li&gt;Queries.&lt;/li&gt;
&lt;li&gt;Shared contracts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They should avoid reaching directly into one another’s internal repositories or private tables without a clear reason.&lt;/p&gt;

&lt;p&gt;A modular monolith can provide many benefits of good boundaries without immediately introducing the operational complexity of microservices.&lt;/p&gt;

&lt;p&gt;This is especially useful for early-stage SaaS products.&lt;/p&gt;

&lt;p&gt;You can keep deployment simple while keeping responsibilities organized.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coupling in microservices
&lt;/h2&gt;

&lt;p&gt;Microservices do not automatically create low coupling.&lt;/p&gt;

&lt;p&gt;A system can have separate deployable services and still be tightly coupled if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every request requires five synchronous service calls.&lt;/li&gt;
&lt;li&gt;Services share the same database tables.&lt;/li&gt;
&lt;li&gt;One service depends on another’s internal schema.&lt;/li&gt;
&lt;li&gt;All services must deploy together.&lt;/li&gt;
&lt;li&gt;A small contract change breaks many consumers.&lt;/li&gt;
&lt;li&gt;A single service failure brings down the entire workflow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is distributed coupling.&lt;/p&gt;

&lt;p&gt;The services are separate in code and deployment,but tightly connected in behaviour.&lt;/p&gt;

&lt;p&gt;A well-designed microservice should own a meaningful capability and communicate through stable contracts.&lt;/p&gt;

&lt;p&gt;But even then, network communication introduces costs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Latency.&lt;/li&gt;
&lt;li&gt;Timeouts.&lt;/li&gt;
&lt;li&gt;Retries.&lt;/li&gt;
&lt;li&gt;Partial failures.&lt;/li&gt;
&lt;li&gt;Versioning.&lt;/li&gt;
&lt;li&gt;Observability.&lt;/li&gt;
&lt;li&gt;Eventual consistency.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sometimes a well-modularized monolith has less harmful coupling than a poorly designed microservice architecture.&lt;/p&gt;

&lt;p&gt;The number of services is not a measure of design quality.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cohesion and database design
&lt;/h2&gt;

&lt;p&gt;Coupling and cohesion also apply to data.&lt;/p&gt;

&lt;p&gt;A module should ideally own the data required for its responsibility.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Billing module → subscriptions, invoices, billing transactions&lt;/li&gt;
&lt;li&gt;Inventory module → stock levels, reservations, warehouses&lt;/li&gt;
&lt;li&gt;Identity module → users, credentials, sessions, permissions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If every module directly reads and writes every table, the database becomes a hidden integration layer.&lt;/p&gt;

&lt;p&gt;Changes become risky because nobody knows which services depend on which columns, indexes, or status values.&lt;/p&gt;

&lt;p&gt;This does not mean every module must have a separate database.&lt;/p&gt;

&lt;p&gt;A modular monolith can use one database while still enforcing ownership boundaries.&lt;/p&gt;

&lt;p&gt;The important question is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Who owns this data, and how should other modules access it?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Clear ownership improves cohesion.&lt;br&gt;
Controlled access reduces coupling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coupling and event-driven design
&lt;/h2&gt;

&lt;p&gt;Events can reduce direct dependency between modules.&lt;/p&gt;

&lt;p&gt;Instead of the order module directly calling the notification module:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv2vqsw19hq4463gnyu7n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv2vqsw19hq4463gnyu7n.png" alt=" " width="800" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;it may publish an event:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqe42obf9zsaqdj75ib4t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqe42obf9zsaqdj75ib4t.png" alt=" " width="800" height="518"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The notification module subscribes to that event.&lt;/p&gt;

&lt;p&gt;This creates looser direct coupling because the order module does not need to know how notifications work.&lt;/p&gt;

&lt;p&gt;However, event-driven systems introduce new trade-offs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Events may be delayed.&lt;/li&gt;
&lt;li&gt;Events may be delivered more than once.&lt;/li&gt;
&lt;li&gt;Consumers may process events out of order.&lt;/li&gt;
&lt;li&gt;Event schemas must evolve carefully.&lt;/li&gt;
&lt;li&gt;Debugging requires tracing across asynchronous flows.&lt;/li&gt;
&lt;li&gt;Data may become eventually consistent.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Events reduce some forms of coupling, but they do not eliminate system complexity.&lt;/p&gt;

&lt;p&gt;They move the complexity from direct calls to contracts, delivery, and observability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Signs of high coupling
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Your codebase may be tightly coupled when:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A small change requires edits across many modules.&lt;/li&gt;
&lt;li&gt;Tests need the entire application to run.&lt;/li&gt;
&lt;li&gt;Classes instantiate their own dependencies.&lt;/li&gt;
&lt;li&gt;Modules access one another’s private data.&lt;/li&gt;
&lt;li&gt;A shared utility module contains business logic for everything.&lt;/li&gt;
&lt;li&gt;Multiple services depend on the same database schema.&lt;/li&gt;
&lt;li&gt;Replacing a provider requires changing business logic.&lt;/li&gt;
&lt;li&gt;One failed dependency causes unrelated features to fail.&lt;/li&gt;
&lt;li&gt;Developers avoid refactoring certain areas because the impact is unpredictable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;High coupling creates a change ripple.&lt;/p&gt;

&lt;p&gt;One adjustment travels through the system, touching code that should not have been affected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Signs of low cohesion
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;A module may have low cohesion when:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Its name is vague, such as CommonService or Utils.&lt;/li&gt;
&lt;li&gt;Its methods serve unrelated business functions.&lt;/li&gt;
&lt;li&gt;It changes frequently for different reasons.&lt;/li&gt;
&lt;li&gt;Developers are unsure where to add new functionality.&lt;/li&gt;
&lt;li&gt;The module has too many dependencies.&lt;/li&gt;
&lt;li&gt;Its tests cover unrelated behaviour.&lt;/li&gt;
&lt;li&gt;Removing one method leaves the remaining methods unrelated.&lt;/li&gt;
&lt;li&gt;The module requires a long explanation before anyone understands its purpose.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A useful question is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If I had to describe this module in one sentence, would the description be clear?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the answer is no, the module may need a better boundary.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to improve cohesion
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Group behavior by business capability&lt;/strong&gt;&lt;br&gt;
Instead of organizing everything by technical type:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;controllers/&lt;/li&gt;
&lt;li&gt;services/&lt;/li&gt;
&lt;li&gt;repositories/&lt;/li&gt;
&lt;li&gt;utils/&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;consider also thinking in terms of capabilities:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyrpmrsgqwki6w13p0x3j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyrpmrsgqwki6w13p0x3j.png" alt=" " width="577" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The best structure depends on the application, but grouping related behavior makes ownership clearer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keep related data and behavior close&lt;/strong&gt;&lt;br&gt;
If a business rule always operates on a particular concept, consider keeping the rule near that concept.&lt;/p&gt;

&lt;p&gt;For example, order status transitions may belong to an order domain model or order service rather than a general utility file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Separate unrelated reasons to change&lt;/strong&gt;&lt;br&gt;
If one module changes because of payment requirements, email requirements, and reporting requirements, it probably contains multiple responsibilities.&lt;/p&gt;

&lt;p&gt;Split it around those change patterns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Avoid generic dumping grounds&lt;/strong&gt;&lt;br&gt;
Be cautious with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;utils.&lt;/li&gt;
&lt;li&gt;helpers.&lt;/li&gt;
&lt;li&gt;common.&lt;/li&gt;
&lt;li&gt;misc.&lt;/li&gt;
&lt;li&gt;shared-service.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These folders can be useful, but they often become places where code is stored without a clear owner.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to reduce coupling
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Depend on abstractions&lt;/strong&gt;&lt;br&gt;
Define contracts around capabilities:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk6cy2mufuvfc7rogkjoy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk6cy2mufuvfc7rogkjoy.png" alt=" " width="800" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The application service should not need to know the database library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use dependency injection&lt;/strong&gt;&lt;br&gt;
Pass dependencies into a component instead of constructing them internally.&lt;/p&gt;

&lt;p&gt;This makes dependencies explicit and replaceable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Encapsulate internal details&lt;/strong&gt;&lt;br&gt;
Expose what another module needs, not everything the module knows.&lt;/p&gt;

&lt;p&gt;A module should not expose its internal database models, private state, and implementation-specific helper methods unnecessarily.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prefer messages for some workflows&lt;/strong&gt;&lt;br&gt;
Events and commands can reduce direct knowledge between modules.&lt;/p&gt;

&lt;p&gt;Use them where asynchronous behavior and eventual consistency are acceptable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Define stable contracts&lt;/strong&gt;&lt;br&gt;
Use clear request and response models.&lt;/p&gt;

&lt;p&gt;Avoid passing internal database entities everywhere. Internal data structures change more frequently than business contracts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Limit shared mutable state&lt;/strong&gt;&lt;br&gt;
Shared mutable state is one of the easiest ways to create unpredictable coupling.&lt;/p&gt;

&lt;p&gt;Prefer explicit inputs and outputs.&lt;/p&gt;

&lt;h2&gt;
  
  
  The balance: cohesion versus coupling
&lt;/h2&gt;

&lt;p&gt;It is possible to overcorrect.&lt;/p&gt;

&lt;p&gt;Suppose you split one simple function into ten classes.&lt;/p&gt;

&lt;p&gt;You may reduce local responsibilities, but create excessive coordination and indirection.&lt;/p&gt;

&lt;p&gt;This is sometimes called accidental complexity.&lt;/p&gt;

&lt;p&gt;A good architecture balances:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cohesion.&lt;/li&gt;
&lt;li&gt;Coupling.&lt;/li&gt;
&lt;li&gt;Simplicity.&lt;/li&gt;
&lt;li&gt;Performance.&lt;/li&gt;
&lt;li&gt;Team understanding.&lt;/li&gt;
&lt;li&gt;Operational cost.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You do not want modules so large that everything is mixed together.&lt;/p&gt;

&lt;p&gt;You also do not want modules so fragmented that understanding one workflow requires jumping through twenty files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The right boundary is usually where:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Responsibilities change together.&lt;/li&gt;
&lt;li&gt;Data is accessed together.&lt;/li&gt;
&lt;li&gt;Business rules belong together.&lt;/li&gt;
&lt;li&gt;The module can be explained clearly.&lt;/li&gt;
&lt;li&gt;The dependency is meaningful.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A useful code review framework
&lt;/h2&gt;

&lt;p&gt;When reviewing a module, ask:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;About cohesion&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do these methods belong to the same business capability?&lt;/li&gt;
&lt;li&gt;Do they use the same data?&lt;/li&gt;
&lt;li&gt;Do they change for similar reasons?&lt;/li&gt;
&lt;li&gt;Does the module have one clear purpose?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;About coupling&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What does this module know about other modules?&lt;/li&gt;
&lt;li&gt;Does it depend on implementation details?&lt;/li&gt;
&lt;li&gt;Can a dependency be replaced easily?&lt;/li&gt;
&lt;li&gt;Does a small change here affect many consumers?&lt;/li&gt;
&lt;li&gt;Are dependencies explicit?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;About boundaries&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Who owns this business rule?&lt;/li&gt;
&lt;li&gt;Who owns this data?&lt;/li&gt;
&lt;li&gt;Is this communication synchronous or asynchronous?&lt;/li&gt;
&lt;li&gt;Is the contract stable?&lt;/li&gt;
&lt;li&gt;Is the abstraction solving a real problem?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These questions help you evaluate design beyond whether the code is syntactically clean.&lt;/p&gt;

&lt;h2&gt;
  
  
  The connection to SOLID
&lt;/h2&gt;

&lt;p&gt;Coupling and cohesion are closely connected to SOLID principles.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single Responsibility improves cohesion.&lt;/li&gt;
&lt;li&gt;Open/Closed reduces unnecessary changes to stable modules.&lt;/li&gt;
&lt;li&gt;Liskov Substitution creates reliable contracts.&lt;/li&gt;
&lt;li&gt;Interface Segregation reduces unnecessary dependencies.&lt;/li&gt;
&lt;li&gt;Dependency Inversion reduces coupling to infrastructure.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They are also related to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Encapsulation.&lt;/li&gt;
&lt;li&gt;Separation of concerns.&lt;/li&gt;
&lt;li&gt;Domain-driven design.&lt;/li&gt;
&lt;li&gt;Modular architecture.&lt;/li&gt;
&lt;li&gt;Clean architecture.&lt;/li&gt;
&lt;li&gt;Hexagonal architecture.&lt;/li&gt;
&lt;li&gt;Event-driven design.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But the underlying idea remains simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Put related responsibilities together, and prevent unrelated components from knowing too much about one another.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Good software is not made of isolated components that never communicate.&lt;/p&gt;

&lt;p&gt;It is made of focused components that communicate intentionally.&lt;/p&gt;

&lt;p&gt;High cohesion gives each module a clear identity.&lt;/p&gt;

&lt;p&gt;Low coupling prevents changes from spreading unnecessarily.&lt;/p&gt;

&lt;p&gt;When cohesion is low, modules become confusing.&lt;/p&gt;

&lt;p&gt;When coupling is high, the entire system becomes fragile.&lt;/p&gt;

&lt;p&gt;When both are poor, every feature becomes a negotiation with the existing codebase.&lt;/p&gt;

&lt;p&gt;The goal is not to eliminate dependencies.&lt;/p&gt;

&lt;p&gt;The goal is to make dependencies deliberate, visible, and manageable.&lt;/p&gt;

&lt;p&gt;So, the next time you create a service, module, class, or microservice, ask two questions:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Does everything inside this component belong together?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Does this component know more about the outside world than it needs to know?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Those two questions can prevent a lot of future pain.&lt;/p&gt;

&lt;p&gt;Because maintainable software is not software with no complexity.&lt;/p&gt;

&lt;p&gt;It is software where complexity has been given a clear place to live.&lt;/p&gt;

</description>
      <category>softwaredevelopment</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>When Abstraction Becomes a Bottleneck: The Real Cost of Overengineering</title>
      <dc:creator>Nsikan Patrick Adaowo</dc:creator>
      <pubDate>Thu, 10 Sep 2026 14:04:08 +0000</pubDate>
      <link>https://dev.to/nsikanadaowo/when-abstraction-becomes-a-bottleneck-the-real-cost-of-overengineering-c57</link>
      <guid>https://dev.to/nsikanadaowo/when-abstraction-becomes-a-bottleneck-the-real-cost-of-overengineering-c57</guid>
      <description>&lt;p&gt;Abstraction is one of the most powerful tools in software engineering.&lt;/p&gt;

&lt;p&gt;It hides complexity. It reveals intent. It reduces duplication. It makes systems easier to understand, test, and change.&lt;br&gt;
But abstraction has a shadow side.&lt;/p&gt;

&lt;p&gt;When applied too early, too generically, or without a clear problem, abstraction can become a bottleneck.&lt;/p&gt;

&lt;p&gt;It can make code harder to read, harder to debug, and harder to evolve.&lt;/p&gt;

&lt;p&gt;It can turn simple workflows into mazes of indirection.&lt;/p&gt;

&lt;p&gt;It can create the illusion of flexibility while introducing hidden coupling.&lt;/p&gt;

&lt;p&gt;And perhaps most dangerously, it can make engineers feel productive while actually reducing the team’s ability to ship.&lt;/p&gt;

&lt;p&gt;This article explores when abstraction becomes harmful, why overengineering is more expensive than duplication, and how to recognize the signs that your design is working against you.&lt;/p&gt;

&lt;h2&gt;
  
  
  The promise of abstraction
&lt;/h2&gt;

&lt;p&gt;Abstraction is how we manage complexity.&lt;/p&gt;

&lt;p&gt;Instead of thinking in terms of bits and bytes, we work with concepts like users, orders, payments, and notifications.&lt;/p&gt;

&lt;p&gt;Instead of repeating the same logic in ten places, we extract a function.&lt;/p&gt;

&lt;p&gt;Instead of coupling business rules to a specific database, we define a repository interface.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Good abstractions:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce cognitive load.&lt;/li&gt;
&lt;li&gt;Clarify intent.&lt;/li&gt;
&lt;li&gt;Make change safer.&lt;/li&gt;
&lt;li&gt;Enable reuse.&lt;/li&gt;
&lt;li&gt;Protect core logic from infrastructure details.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They are essential for building maintainable systems.&lt;/p&gt;

&lt;p&gt;But not every abstraction is good.&lt;/p&gt;

&lt;p&gt;And not every problem needs an abstraction.&lt;/p&gt;

&lt;h2&gt;
  
  
  The hidden cost of premature abstraction
&lt;/h2&gt;

&lt;p&gt;Premature abstraction happens when you generalize a solution before you fully understand the problem.&lt;/p&gt;

&lt;p&gt;You see a pattern that appears twice. You assume it will appear a third time. You create an interface, a factory, a strategy, and a configuration object to support every possible future variation.&lt;/p&gt;

&lt;p&gt;The code looks sophisticated.&lt;/p&gt;

&lt;p&gt;But the abstraction is built on assumptions, not evidence.&lt;/p&gt;

&lt;h2&gt;
  
  
  The asymmetry of technical debt
&lt;/h2&gt;

&lt;p&gt;There is an important asymmetry between premature optimization and premature abstraction.&lt;/p&gt;

&lt;p&gt;Premature optimization usually affects a localized part of the code.&lt;/p&gt;

&lt;p&gt;For example, you implement a complex caching layer for a function that is called a few times per day. The optimization is unnecessary, but its impact is limited.&lt;/p&gt;

&lt;p&gt;Premature abstraction, on the other hand, can affect the entire architecture.&lt;/p&gt;

&lt;p&gt;When you introduce semantic boundaries and logical coupling before the problem domain is understood, you create a mesh of dependencies that resists evolution.&lt;/p&gt;

&lt;p&gt;The cost of unwinding a wrong abstraction is not linear.&lt;/p&gt;

&lt;p&gt;It is often geometric.&lt;/p&gt;

&lt;p&gt;You may need to dismantle entire subsystems, update multiple layers, and reconcile conflicting mental models.&lt;/p&gt;

&lt;p&gt;The cost of removing a premature optimization is typically isolated to a specific function or block.&lt;/p&gt;

&lt;p&gt;The cost of removing a premature abstraction can ripple through the entire codebase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Signs that abstraction has become a bottleneck
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Too many layers of indirection&lt;/strong&gt;&lt;br&gt;
You need to jump through five files to understand a simple operation.&lt;/p&gt;

&lt;p&gt;Controller → Service → UseCase → Handler → Processor → Adapter&lt;/p&gt;

&lt;p&gt;Each layer adds indirection without adding clarity.&lt;/p&gt;

&lt;p&gt;The call chain is deep, but the business intent is unclear.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Overly complex class hierarchies&lt;/strong&gt;&lt;br&gt;
Your class hierarchy has multiple levels of abstract classes and interfaces.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;BaseService&lt;/em&gt; → &lt;em&gt;AbstractCrudService&lt;/em&gt; → &lt;em&gt;EntityService&lt;/em&gt; → &lt;em&gt;UserService&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Each layer introduces hooks, configuration options, and extension points that are rarely used.&lt;/p&gt;

&lt;p&gt;New developers cannot tell which methods are important and which are legacy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Methods with too many parameters&lt;/strong&gt;&lt;br&gt;
A method signature looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Folcmgc263memq1uy4ocj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Folcmgc263memq1uy4ocj.png" alt=" " width="790" height="856"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The method tries to support every possible scenario.&lt;/p&gt;

&lt;p&gt;As a result, it is difficult to call, difficult to test, and difficult to understand.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Generic components that handle everything&lt;/strong&gt;&lt;br&gt;
A single component or service tries to support every use case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4gc92h09wxtneafroaot.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4gc92h09wxtneafroaot.png" alt=" " width="672" height="744"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The class becomes a dumping ground for unrelated functionality.&lt;/p&gt;

&lt;p&gt;Its name no longer communicates intent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Configuration objects that hold everything&lt;/strong&gt;&lt;br&gt;
A configuration object accumulates settings for multiple concerns:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0s4nee85nk4u4tn6tmfr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0s4nee85nk4u4tn6tmfr.png" alt=" " width="790" height="930"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The configuration becomes unreadable and impossible to evolve safely.&lt;/p&gt;

&lt;p&gt;Changing one setting may affect unrelated parts of the system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Names that hide intent&lt;/strong&gt;&lt;br&gt;
Classes and functions are named with generic terms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manager&lt;/li&gt;
&lt;li&gt;Processor&lt;/li&gt;
&lt;li&gt;Handler&lt;/li&gt;
&lt;li&gt;Service&lt;/li&gt;
&lt;li&gt;Controller&lt;/li&gt;
&lt;li&gt;Provider&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The names do not convey what the component actually does.&lt;/p&gt;

&lt;p&gt;A developer must read the implementation to understand its purpose.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Conditional branches that proliferate&lt;/strong&gt;&lt;br&gt;
A single function contains many conditional branches to support different scenarios:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff2r5c7j1px98uag79x7x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff2r5c7j1px98uag79x7x.png" alt=" " width="738" height="708"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The function tries to be versatile, but it becomes complex and fragile.&lt;/p&gt;

&lt;p&gt;Adding a new type requires modifying the central function and understanding all existing branches.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Onboarding becomes extremely difficult&lt;/strong&gt;&lt;br&gt;
New developers take weeks to understand the codebase.&lt;/p&gt;

&lt;p&gt;They must decipher multiple layers of abstraction before they can make a simple change.&lt;/p&gt;

&lt;p&gt;The joy of building yields to the chore of deciphering.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. Breaking one abstraction breaks the whole&lt;/strong&gt;&lt;br&gt;
A single abstraction is used by many parts of the system.&lt;/p&gt;

&lt;p&gt;When you change it, multiple features break.&lt;/p&gt;

&lt;p&gt;The abstraction has become a concentration risk.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10. Domain intent disappears&lt;/strong&gt;&lt;br&gt;
The code no longer reflects the business domain.&lt;/p&gt;

&lt;p&gt;Instead of Order, Payment, and Invoice, you have Entity, Resource, and Model.&lt;/p&gt;

&lt;p&gt;Instead of &lt;em&gt;chargePayment&lt;/em&gt;, you have &lt;em&gt;executeAction&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The connection between the code and the problem it solves is lost.&lt;/p&gt;

&lt;h2&gt;
  
  
  The real cost of overengineering
&lt;/h2&gt;

&lt;p&gt;Overengineering is not just an aesthetic problem.&lt;/p&gt;

&lt;p&gt;It has real costs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Slower development&lt;/strong&gt;&lt;br&gt;
Features take longer to implement because engineers must navigate multiple layers of abstraction.&lt;/p&gt;

&lt;p&gt;Simple changes require understanding complex call chains.&lt;/p&gt;

&lt;p&gt;Estimates slip because the cognitive surface is larger than it appears.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- More bugs&lt;/strong&gt;&lt;br&gt;
Abstractions that try to support too many scenarios introduce edge cases.&lt;/p&gt;

&lt;p&gt;Conditional branches multiply.&lt;/p&gt;

&lt;p&gt;Configuration options interact in unexpected ways.&lt;/p&gt;

&lt;p&gt;The system becomes harder to reason about, and bugs become harder to locate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Higher maintenance cost&lt;/strong&gt;&lt;br&gt;
Maintaining an overengineered system requires more effort.&lt;/p&gt;

&lt;p&gt;Developers must understand not only what the code does, but why it was designed this way.&lt;/p&gt;

&lt;p&gt;Refactoring becomes risky because the impact of changes is difficult to predict.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Reduced morale&lt;/strong&gt;&lt;br&gt;
Engineers join teams to build things.&lt;/p&gt;

&lt;p&gt;When the codebase feels like a maze, motivation declines.&lt;/p&gt;

&lt;p&gt;Talent departs when the joy of building yields to the chore of deciphering.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Lost opportunities&lt;/strong&gt;&lt;br&gt;
Overengineered systems resist change.&lt;/p&gt;

&lt;p&gt;New ideas are avoided because they do not fit the existing abstraction.&lt;/p&gt;

&lt;p&gt;The team spends more time reconciling mental models than shipping value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Duplication is cheaper than the wrong abstraction
&lt;/h2&gt;

&lt;p&gt;One of the most important lessons in software design is this:&lt;/p&gt;

&lt;p&gt;Duplication is far cheaper than the wrong abstraction.&lt;/p&gt;

&lt;p&gt;When you duplicate code, you pay a localized cost.&lt;/p&gt;

&lt;p&gt;When you change one copy, you may need to change the other.&lt;/p&gt;

&lt;p&gt;But the impact is limited.&lt;/p&gt;

&lt;p&gt;When you create the wrong abstraction, you pay a systemic cost.&lt;/p&gt;

&lt;p&gt;The abstraction couples multiple parts of the system.&lt;/p&gt;

&lt;p&gt;Changing it requires understanding all consumers.&lt;/p&gt;

&lt;p&gt;The cost of unwinding it can be geometric.&lt;/p&gt;

&lt;p&gt;This does not mean duplication is always good.&lt;/p&gt;

&lt;p&gt;It means that premature generalization is often more expensive than waiting until the pattern is clear.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A useful rule is the “rule of three”:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Implement the logic the first time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Implement it the second time, even if it looks similar.&lt;br&gt;
When you see it a third time, consider an abstraction.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This rule is not absolute, but it encourages evidence-based design.&lt;/p&gt;

&lt;p&gt;You abstract when you have seen the pattern, not when you predict it.&lt;/p&gt;

&lt;h2&gt;
  
  
  When abstraction is appropriate
&lt;/h2&gt;

&lt;p&gt;Abstraction is not the enemy.&lt;/p&gt;

&lt;p&gt;Bad abstraction is.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Good abstraction is appropriate when:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The pattern has appeared multiple times.&lt;/li&gt;
&lt;li&gt;The variation is real, not speculative.&lt;/li&gt;
&lt;li&gt;The abstraction clarifies intent.&lt;/li&gt;
&lt;li&gt;The boundary is meaningful.&lt;/li&gt;
&lt;li&gt;The cost of change is reduced.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The team can understand and maintain it.&lt;br&gt;
For example, a payment gateway abstraction is useful when:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You have multiple payment providers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The business may change providers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The core logic should not depend on a specific provider.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Testing is easier with a fake implementation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A repository abstraction is useful when:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The data access strategy may change.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The domain logic should not depend on a specific database.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Testing is easier with an in-memory implementation.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But if you have one payment provider and no plan to change, a direct implementation may be simpler.&lt;/p&gt;

&lt;p&gt;If you have one database and no need for multiple implementations, a direct repository may be clearer.&lt;/p&gt;

&lt;p&gt;The goal is not to avoid abstraction.&lt;/p&gt;

&lt;p&gt;The goal is to avoid abstraction that does not yet earn its place.&lt;/p&gt;

&lt;h2&gt;
  
  
  The YAGNI principle
&lt;/h2&gt;

&lt;p&gt;YAGNI stands for “You Aren’t Going to Need It.”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The principle says:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Do not implement functionality until it is necessary.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This does not mean you should ignore the future.&lt;/p&gt;

&lt;p&gt;It means you should not implement speculative features or abstractions based on assumptions.&lt;/p&gt;

&lt;p&gt;There is no reason to make code more complicated just so that down the road you may be able to make it more flexible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;YAGNI complements other principles such as:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DRY (Don’t Repeat Yourself).&lt;/li&gt;
&lt;li&gt;KISS (Keep It Simple, Stupid).&lt;/li&gt;
&lt;li&gt;SOLID.&lt;/li&gt;
&lt;li&gt;Separation of concerns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The balance is important.&lt;/p&gt;

&lt;p&gt;DRY encourages removing duplication.&lt;/p&gt;

&lt;p&gt;YAGNI encourages waiting until the duplication is real.&lt;/p&gt;

&lt;p&gt;Together, they suggest:&lt;/p&gt;

&lt;p&gt;Do not repeat yourself unnecessarily, but do not abstract prematurely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WET before DRY&lt;/strong&gt;&lt;br&gt;
Another useful perspective is “WET before DRY.”&lt;/p&gt;

&lt;p&gt;WET stands for “Write Everything Twice.”&lt;/p&gt;

&lt;p&gt;The idea is that duplicating code once or twice is not costly.&lt;/p&gt;

&lt;p&gt;It allows you to understand the pattern before abstracting it.&lt;/p&gt;

&lt;p&gt;Starting with DRY from the beginning increases the chances of bad generalizations.&lt;/p&gt;

&lt;p&gt;When you attempt to DRY up the code too early, you may:&lt;/p&gt;

&lt;p&gt;Optimize prematurely.&lt;/p&gt;

&lt;p&gt;Bundle operations with different contexts.&lt;/p&gt;

&lt;p&gt;Create abstractions that do not fit the problem.&lt;/p&gt;

&lt;p&gt;An alternative is to see WET and DRY as complementary.&lt;/p&gt;

&lt;p&gt;Start with a basic design.&lt;/p&gt;

&lt;p&gt;When you find duplicated code two or three times, consider an abstraction.&lt;/p&gt;

&lt;p&gt;After working on the problem for some time, reflect on how to abstract based on the challenges you faced.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical guidelines for avoiding over-abstraction
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Wait for evidence&lt;/strong&gt;&lt;br&gt;
Do not abstract based on predictions.&lt;/p&gt;

&lt;p&gt;Wait until you have seen the pattern multiple times.&lt;/p&gt;

&lt;p&gt;Ask:&lt;/p&gt;

&lt;p&gt;Has this variation appeared in production?&lt;/p&gt;

&lt;p&gt;Is this a real requirement or a hypothetical scenario?&lt;/p&gt;

&lt;p&gt;Will this abstraction simplify the next feature?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Favor explicit code&lt;/strong&gt;&lt;br&gt;
Explicit code is easier to understand than clever abstractions.&lt;/p&gt;

&lt;p&gt;If a function has three clear steps, write them explicitly.&lt;/p&gt;

&lt;p&gt;If a workflow has four distinct operations, name them clearly.&lt;/p&gt;

&lt;p&gt;Do not hide simple logic behind layers of indirection.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Keep abstractions close to the domain&lt;/strong&gt;&lt;br&gt;
Abstractions should reflect the business domain, not technical convenience.&lt;/p&gt;

&lt;p&gt;Use names like Order, Payment, and Invoice instead of Entity, Resource, and Model.&lt;/p&gt;

&lt;p&gt;Use verbs like chargePayment and createOrder instead of executeAction and processResource.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Limit configuration&lt;/strong&gt;&lt;br&gt;
Avoid configuration objects that hold everything.&lt;/p&gt;

&lt;p&gt;Split configuration by concern:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Database configuration.&lt;/li&gt;
&lt;li&gt;Feature flags.&lt;/li&gt;
&lt;li&gt;External API keys.&lt;/li&gt;
&lt;li&gt;Tenant settings.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This makes the configuration easier to understand and evolve.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Reduce indirection&lt;/strong&gt;&lt;br&gt;
Ask whether each layer adds value.&lt;/p&gt;

&lt;p&gt;If a layer only delegates to another layer without adding behaviour, consider removing it.&lt;/p&gt;

&lt;p&gt;Deep call chains increase cognitive load without improving design.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Test the abstraction&lt;/strong&gt;&lt;br&gt;
Before adopting an abstraction, ask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can a new developer understand it in ten minutes?&lt;/li&gt;
&lt;li&gt;Does it make the next feature easier to implement?&lt;/li&gt;
&lt;li&gt;Does it reduce the number of concepts a developer must hold in their head?&lt;/li&gt;
&lt;li&gt;Does it make testing easier or harder?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the answer is no, the abstraction may not be worth the cost.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Prefer composition over inheritance&lt;/strong&gt;&lt;br&gt;
Inheritance can create deep hierarchies that are difficult to understand.&lt;/p&gt;

&lt;p&gt;Composition allows you to assemble behaviour from smaller components.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ovy0i51bnsy092ntb4j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ovy0i51bnsy092ntb4j.png" alt=" " width="799" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is often clearer than a deep inheritance tree.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Document the intent&lt;/strong&gt;&lt;br&gt;
If an abstraction is necessary, document its intent.&lt;/p&gt;

&lt;p&gt;Explain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why the abstraction exists.&lt;/li&gt;
&lt;li&gt;What problem it solves.&lt;/li&gt;
&lt;li&gt;What scenarios it supports.&lt;/li&gt;
&lt;li&gt;What scenarios it does not support.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This helps future developers understand the design decisions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A practical example: notification service&lt;/strong&gt;&lt;br&gt;
Consider a notification system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overengineered design&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuy4ucaxiv9a1p3ssdhz8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuy4ucaxiv9a1p3ssdhz8.png" alt=" " width="800" height="632"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The abstraction tries to support every possible scenario.&lt;/p&gt;

&lt;p&gt;The configuration object holds settings for multiple concerns.&lt;/p&gt;

&lt;p&gt;The method contains many conditional branches.&lt;/p&gt;

&lt;p&gt;Adding a new channel requires modifying the central method and understanding all existing logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simpler design&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F36nfxc0ublzdyxhmgi3h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F36nfxc0ublzdyxhmgi3h.png" alt=" " width="800" height="799"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each channel is a separate implementation.&lt;/p&gt;

&lt;p&gt;The service delegates to the appropriate channel.&lt;/p&gt;

&lt;p&gt;Adding a new channel requires creating a new implementation, not modifying existing logic.&lt;/p&gt;

&lt;p&gt;The design is simpler, clearer, and easier to extend.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to refactor
&lt;/h2&gt;

&lt;p&gt;Refactoring is appropriate when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The pattern has appeared multiple times.&lt;/li&gt;
&lt;li&gt;The duplication is causing maintenance problems.&lt;/li&gt;
&lt;li&gt;The abstraction will clarify intent.&lt;/li&gt;
&lt;li&gt;The team can understand and maintain it.&lt;/li&gt;
&lt;li&gt;The cost of change is reduced.&lt;/li&gt;
&lt;li&gt;Refactoring is premature when:&lt;/li&gt;
&lt;li&gt;The pattern has appeared once.&lt;/li&gt;
&lt;li&gt;The variation is speculative.&lt;/li&gt;
&lt;li&gt;The abstraction adds indirection without clarity.&lt;/li&gt;
&lt;li&gt;The team cannot explain the design in simple terms.&lt;/li&gt;
&lt;li&gt;The cost of change increases.
The goal is not to avoid refactoring.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal is to refactor based on evidence, not prediction.&lt;/p&gt;

&lt;h2&gt;
  
  
  The balance: Goldilocks abstraction
&lt;/h2&gt;

&lt;p&gt;Abstraction should be “just right.”&lt;/p&gt;

&lt;p&gt;Not too little, not too much.&lt;/p&gt;

&lt;p&gt;A codebase with too little abstraction can be repetitive and difficult to maintain.&lt;/p&gt;

&lt;p&gt;A codebase with too much abstraction can be difficult to understand and evolve.&lt;/p&gt;

&lt;p&gt;The sweet spot is when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The code reflects the domain.&lt;/li&gt;
&lt;li&gt;The abstractions are meaningful.&lt;/li&gt;
&lt;li&gt;The call chains are shallow.&lt;/li&gt;
&lt;li&gt;The intent is clear.&lt;/li&gt;
&lt;li&gt;The team can onboard quickly.&lt;/li&gt;
&lt;li&gt;Change is easier, not harder.&lt;/li&gt;
&lt;li&gt;This balance is not static.&lt;/li&gt;
&lt;li&gt;It evolves as the system grows and the team learns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The important skill is recognizing when an abstraction has crossed the line from helpful to harmful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Abstraction is a tool, not a goal.&lt;/p&gt;

&lt;p&gt;Good abstraction reduces complexity.&lt;/p&gt;

&lt;p&gt;Bad abstraction creates it.&lt;/p&gt;

&lt;p&gt;The real cost of overengineering is not measured in lines of code.&lt;/p&gt;

&lt;p&gt;It is measured in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slower development.&lt;/li&gt;
&lt;li&gt;More bugs.&lt;/li&gt;
&lt;li&gt;Higher maintenance cost.&lt;/li&gt;
&lt;li&gt;Reduced morale.&lt;/li&gt;
&lt;li&gt;Lost opportunities.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Duplication is often cheaper than the wrong abstraction.&lt;/p&gt;

&lt;p&gt;Waiting for evidence is often wiser than predicting the future.&lt;/p&gt;

&lt;p&gt;Explicit code is often clearer than clever design.&lt;/p&gt;

&lt;p&gt;The next time you create an abstraction, ask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does this simplify the next feature?&lt;/li&gt;
&lt;li&gt;Can a new developer understand this in ten minutes?&lt;/li&gt;
&lt;li&gt;Is this pattern real or speculative?&lt;/li&gt;
&lt;li&gt;Does this reduce or increase the cognitive load?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because good software is not software with the most abstractions.&lt;/p&gt;

&lt;p&gt;It is software that can be understood, changed, and extended without unnecessary friction.&lt;/p&gt;

&lt;p&gt;And sometimes, the best design is the one that resists the urge to generalize before it is ready.&lt;/p&gt;

</description>
      <category>softwareengineering</category>
      <category>architecture</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Dependency Injection Explained: Why Your Classes Shouldn't Know How Everything Is Built</title>
      <dc:creator>Nsikan Patrick Adaowo</dc:creator>
      <pubDate>Sun, 30 Aug 2026 12:31:31 +0000</pubDate>
      <link>https://dev.to/nsikanadaowo/dependency-injection-explained-why-your-classes-shouldnt-know-how-everything-is-built-77o</link>
      <guid>https://dev.to/nsikanadaowo/dependency-injection-explained-why-your-classes-shouldnt-know-how-everything-is-built-77o</guid>
      <description>&lt;p&gt;Your codebase may not be failing because it lacks features. It may be failing because everything knows too much about everything else.&lt;/p&gt;

&lt;p&gt;Most backend developers learn dependency injection through a framework.&lt;/p&gt;

&lt;p&gt;In NestJS, you add decorators. In Spring, you annotate classes. In Angular, you declare providers. The framework then constructs your objects and passes their dependencies.&lt;/p&gt;

&lt;p&gt;Everything works fine.&lt;/p&gt;

&lt;p&gt;But many developers stop at the mechanics.&lt;/p&gt;

&lt;p&gt;They learn how to use dependency injection, but not why it matters.&lt;/p&gt;

&lt;p&gt;They learn the syntax, but not the design principle.&lt;/p&gt;

&lt;p&gt;They learn the pattern, but not the problem it solves.&lt;/p&gt;

&lt;p&gt;The result is code that uses dependency injection but still feels tightly coupled, difficult to test, and fragile when requirements change.&lt;/p&gt;

&lt;p&gt;This article explains what dependency injection really is, why your classes should not know how their dependencies are built, and how to use it in a way that improves maintainability, testability, and flexibility.&lt;/p&gt;




&lt;h2&gt;
  
  
  The problem: classes that build their own&amp;nbsp;world
&lt;/h2&gt;

&lt;p&gt;Consider this service:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fatb884hd0gu57ks7t7yr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fatb884hd0gu57ks7t7yr.png" alt=" " width="800" height="716"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This code may work.&lt;/p&gt;

&lt;p&gt;But it has a hidden problem.&lt;/p&gt;

&lt;p&gt;The OrderService knows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which database library to use.&lt;/li&gt;
&lt;li&gt;Which payment provider to call.&lt;/li&gt;
&lt;li&gt;Which email client to instantiate.&lt;/li&gt;
&lt;li&gt;How to construct each dependency.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This creates tight coupling.&lt;/p&gt;

&lt;p&gt;If you want to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Replace Paystack with Flutterwave.&lt;/li&gt;
&lt;li&gt;Switch from Prisma to TypeORM.&lt;/li&gt;
&lt;li&gt;Use a different email provider.&lt;/li&gt;
&lt;li&gt;Test the service without calling real APIs.&lt;/li&gt;
&lt;li&gt;Use a different configuration in staging versus production.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;you must modify the service itself.&lt;br&gt;
The class is responsible not only for its business logic, but also for constructing its entire world.&lt;br&gt;
This is the opposite of what dependency injection tries to achieve.&lt;/p&gt;




&lt;h2&gt;
  
  
  What dependency injection actually&amp;nbsp;is
&lt;/h2&gt;

&lt;p&gt;Dependency injection is a design pattern where a class receives its dependencies from the outside instead of creating them internally.&lt;br&gt;
Instead of:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3crvog4mwvny8uo512t4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3crvog4mwvny8uo512t4.png" alt=" " width="799" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You write:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4o2ot6r5p89ecg50hppe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4o2ot6r5p89ecg50hppe.png" alt=" " width="799" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The dependency is provided by an external assembler, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A framework container.&lt;/li&gt;
&lt;li&gt;A factory function.&lt;/li&gt;
&lt;li&gt;A manual composition root.&lt;/li&gt;
&lt;li&gt;A dependency injection library.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The class no longer decides how its dependencies are built.&lt;br&gt;
It only declares what it needs.&lt;/p&gt;




&lt;h2&gt;
  
  
  Inversion of Control: the bigger&amp;nbsp;idea
&lt;/h2&gt;

&lt;p&gt;Dependency injection is a specific way to implement a broader principle called Inversion of Control.&lt;br&gt;
In traditional code, a high-level component directly creates or locates its low-level dependencies.&lt;br&gt;
For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6tmplou4cxne6o1geyqa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6tmplou4cxne6o1geyqa.png" alt=" " width="800" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The ReportGenerator controls how the database is obtained.&lt;br&gt;
Under Inversion of Control, this responsibility is inverted.&lt;br&gt;
An external mechanism provides the dependency:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8xrc9socuhq118cysuxx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8xrc9socuhq118cysuxx.png" alt=" " width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the framework or container controls object creation and dependency resolution.&lt;br&gt;
The high-level component declares what it needs but does not control how it is constructed.&lt;br&gt;
This inversion of control leads to looser coupling and greater flexibility.&lt;/p&gt;




&lt;h2&gt;
  
  
  Constructor injection: the most common&amp;nbsp;form
&lt;/h2&gt;

&lt;p&gt;The most widely used form of dependency injection is constructor injection.&lt;br&gt;
Dependencies are passed through the constructor:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5m13r2rqd2is94okfz6v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5m13r2rqd2is94okfz6v.png" alt=" " width="800" height="485"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This approach has several advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The object is fully initialized when created.&lt;/li&gt;
&lt;li&gt;Dependencies are clearly visible in the constructor signature.&lt;/li&gt;
&lt;li&gt;Fields can be immutable.&lt;/li&gt;
&lt;li&gt;It fails fast if a dependency is missing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A class that requires certain dependencies should not be constructible without them.&lt;br&gt;
Constructor injection enforces this requirement.&lt;/p&gt;




&lt;h2&gt;
  
  
  Setter and interface injection
&lt;/h2&gt;

&lt;p&gt;Other forms of dependency injection exist, though they are less common in modern backend code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setter injection
&lt;/h3&gt;

&lt;p&gt;Dependencies are provided through setter methods after construction:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxd1b62agsyyxgqlc4zvu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxd1b62agsyyxgqlc4zvu.png" alt=" " width="790" height="632"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This can be useful when dependencies are optional or change over time, but it also allows objects to exist in a partially initialized state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interface injection
&lt;/h3&gt;

&lt;p&gt;A class implements an interface that defines how dependencies are injected.&lt;br&gt;
This is less common in TypeScript and JavaScript ecosystems.&lt;br&gt;
For most backend applications, constructor injection is the simplest and most reliable approach.&lt;/p&gt;




&lt;h2&gt;
  
  
  A practical NestJS&amp;nbsp;example
&lt;/h2&gt;

&lt;p&gt;NestJS provides a built-in dependency injection container.&lt;br&gt;
A service can declare its dependencies in the constructor:&lt;br&gt;
NestJS resolves the dependencies when the service is instantiated.&lt;br&gt;
The service does not need to know:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fopfti2rockn6yc03423y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fopfti2rockn6yc03423y.png" alt=" " width="800" height="792"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Whether OrderRepository uses Prisma, TypeORM, or a raw SQL client.&lt;br&gt;
Whether PaymentGateway is implemented with Paystack, Stripe, or a mock.&lt;br&gt;
How NotificationService sends messages.&lt;/p&gt;

&lt;p&gt;It only knows the capabilities it requires.&lt;br&gt;
This separation makes the service easier to test and easier to evolve.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why dependency injection matters
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Reduced&amp;nbsp;coupling&lt;/strong&gt;&lt;br&gt;
When a class constructs its own dependencies, it is tightly coupled to those implementations.&lt;br&gt;
Dependency injection separates the use of a dependency from its construction.&lt;br&gt;
The class depends on an abstraction or interface, not on a specific concrete class.&lt;br&gt;
This makes it easier to switch implementations without modifying the consumer.&lt;br&gt;
For example, you can replace a payment gateway implementation without changing the order service.&lt;br&gt;
&lt;strong&gt;2. Improved testability&lt;/strong&gt;&lt;br&gt;
Testing becomes easier when dependencies can be replaced with fakes or mocks.&lt;br&gt;
Consider this test:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbqaeeq9n2ify4wxuko62.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbqaeeq9n2ify4wxuko62.png" alt=" " width="800" height="993"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The test can verify:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The correct amount is charged.&lt;/li&gt;
&lt;li&gt;The order is created with the right data.&lt;/li&gt;
&lt;li&gt;The notification is sent.&lt;/li&gt;
&lt;li&gt;Error handling behaves correctly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No real database, payment provider, or email server is required.&lt;br&gt;
This makes tests faster, more reliable, and easier to run in isolation.&lt;br&gt;
&lt;strong&gt;3. Easier configuration management&lt;/strong&gt;&lt;br&gt;
Different environments often require different configurations.&lt;br&gt;
For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Development may use local services.&lt;/li&gt;
&lt;li&gt;Staging may use test providers.&lt;/li&gt;
&lt;li&gt;Production may use real providers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With dependency injection, you can compose different implementations for different environments without changing the core business logic.&lt;br&gt;
&lt;strong&gt;4. Clearer responsibilities&lt;/strong&gt;&lt;br&gt;
When a class receives its dependencies, its constructor signature communicates what it needs.&lt;br&gt;
This makes the code easier to understand.&lt;br&gt;
A developer can look at the constructor and immediately see the external capabilities the class depends on.&lt;br&gt;
This is more explicit than hidden instantiations scattered throughout methods.&lt;br&gt;
&lt;strong&gt;5. Better separation of&amp;nbsp;concerns&lt;/strong&gt;&lt;br&gt;
Dependency injection helps separate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Business logic.&lt;/li&gt;
&lt;li&gt;Infrastructure details.&lt;/li&gt;
&lt;li&gt;Object construction.&lt;/li&gt;
&lt;li&gt;Configuration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The business logic focuses on what needs to happen.&lt;br&gt;
The infrastructure layer focuses on how to interact with external systems.&lt;br&gt;
The composition root or container focuses on how to assemble everything.&lt;br&gt;
This separation makes the system easier to reason about and easier to change.&lt;/p&gt;




&lt;h2&gt;
  
  
  Dependency injection versus service&amp;nbsp;locator
&lt;/h2&gt;

&lt;p&gt;A service locator is another pattern for obtaining dependencies.&lt;br&gt;
Instead of injecting dependencies, a class asks a central registry for them:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fo2tdpugy5qhk82yuqbme.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fo2tdpugy5qhk82yuqbme.png" alt=" " width="800" height="337"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This may look convenient, but it has important drawbacks:&lt;br&gt;
Dependencies are hidden.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The class can request anything from the locator.&lt;/li&gt;
&lt;li&gt;It is harder to see what a class truly needs.&lt;/li&gt;
&lt;li&gt;Tests must configure the global locator.&lt;/li&gt;
&lt;li&gt;It can encourage tight coupling to the locator itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Dependency injection makes dependencies explicit and visible in the constructor.&lt;br&gt;
This clarity is one of its main advantages.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common mistakes with dependency injection
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Injecting concrete implementations everywhere
&lt;/h3&gt;

&lt;p&gt;Dependency injection is most powerful when classes depend on abstractions.&lt;br&gt;
If every service depends directly on concrete classes, you still have tight coupling, even if the container constructs them.&lt;br&gt;
For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffq8eq0k9s04s3zntitpo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffq8eq0k9s04s3zntitpo.png" alt=" " width="799" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is better than creating new PrismaClient() inside the service, but the service is still coupled to Prisma.&lt;br&gt;
A repository abstraction can help:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fodna0g1p3sbdtw4fbu4m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fodna0g1p3sbdtw4fbu4m.png" alt=" " width="800" height="582"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the service depends on a capability, not a specific library.&lt;/p&gt;

&lt;h3&gt;
  
  
  Overusing dependency injection
&lt;/h3&gt;

&lt;p&gt;Not every function needs to be a class with injected dependencies.&lt;br&gt;
Simple utilities, pure functions, and small helpers may not benefit from dependency injection.&lt;br&gt;
For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwbv98dxmreh7yqa2wchi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwbv98dxmreh7yqa2wchi.png" alt=" " width="800" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This function has no external dependencies.&lt;br&gt;
Wrapping it in a class and injecting it through a container adds unnecessary complexity.&lt;br&gt;
Use dependency injection where it solves a real problem: managing dependencies, improving testability, and reducing coupling.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating too many tiny&amp;nbsp;services
&lt;/h3&gt;

&lt;p&gt;Dependency injection can encourage splitting behavior into many small services.&lt;br&gt;
This can be useful, but it can also create fragmentation.&lt;br&gt;
If understanding one workflow requires jumping through twenty classes, the design may be over-engineered.&lt;br&gt;
The goal is not maximum fragmentation.&lt;br&gt;
The goal is clear responsibilities and manageable boundaries.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ignoring the composition root
&lt;/h3&gt;

&lt;p&gt;In a NestJS application, the framework handles much of the composition.&lt;br&gt;
But in plain TypeScript or other environments, you need a place where the application is assembled.&lt;br&gt;
This is often called the composition root.&lt;/p&gt;

&lt;p&gt;It is where you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create concrete implementations.&lt;/li&gt;
&lt;li&gt;Configure providers.&lt;/li&gt;
&lt;li&gt;Wire dependencies together.&lt;/li&gt;
&lt;li&gt;Start the application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keeping this logic centralized makes the system easier to understand and easier to configure for different environments.&lt;/p&gt;




&lt;h2&gt;
  
  
  Dependency injection and interfaces
&lt;/h2&gt;

&lt;p&gt;Dependency injection works especially well with interfaces or abstract classes.&lt;br&gt;
An interface defines a contract:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fch24oxwpslhgmvsmx7b1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fch24oxwpslhgmvsmx7b1.png" alt=" " width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Multiple implementations can satisfy the contract:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2nm66g5y3vm2j0223bl4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2nm66g5y3vm2j0223bl4.png" alt=" " width="800" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The business logic depends on the interface:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fujrsymwmydmp8iwptpr4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fujrsymwmydmp8iwptpr4.png" alt=" " width="799" height="299"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This makes it easy to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Switch providers.&lt;/li&gt;
&lt;li&gt;Test with a fake implementation.&lt;/li&gt;
&lt;li&gt;Introduce a new provider without modifying the checkout logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The class does not know how the payment is implemented.&lt;br&gt;
It only knows what it can ask the payment gateway to do.&lt;/p&gt;




&lt;h2&gt;
  
  
  Dependency injection in a multi-tenant SaaS
&lt;/h2&gt;

&lt;p&gt;In a multi-tenant SaaS platform, different tenants may require different configurations.&lt;br&gt;
For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Different payment providers.&lt;/li&gt;
&lt;li&gt;Different email providers.&lt;/li&gt;
&lt;li&gt;Different storage backends.&lt;/li&gt;
&lt;li&gt;Different feature flags.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Dependency injection can help you compose tenant-specific implementations.&lt;br&gt;
A tenant context can determine which implementation to use:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4843d0hplu4caustrh3l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4843d0hplu4caustrh3l.png" alt=" " width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The rest of the application depends on PaymentGateway, not on the specific provider.&lt;br&gt;
This keeps tenant-specific logic contained and makes the core application more stable.&lt;/p&gt;




&lt;h2&gt;
  
  
  Dependency injection and testing strategies
&lt;/h2&gt;

&lt;p&gt;Dependency injection enables several testing strategies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unit testing with&amp;nbsp;fakes
&lt;/h3&gt;

&lt;p&gt;You can provide simple in-memory or hardcoded implementations:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn3zpal2a1eljx5wqcwoj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn3zpal2a1eljx5wqcwoj.png" alt=" " width="799" height="527"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The service can be tested without a real database.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unit testing with&amp;nbsp;mocks
&lt;/h3&gt;

&lt;p&gt;Mocking libraries can create objects that record how they were called:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Foc95ij933636k74i63yv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Foc95ij933636k74i63yv.png" alt=" " width="800" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This verifies that the service interacts correctly with its dependencies.&lt;br&gt;
Integration testing with real dependencies&lt;br&gt;
For integration tests, you can use real or containerized dependencies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A test database.&lt;/li&gt;
&lt;li&gt;A test message broker.&lt;/li&gt;
&lt;li&gt;A test email service or stub.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Dependency injection makes it easier to swap between test and production configurations.&lt;/p&gt;




&lt;h2&gt;
  
  
  Dependency injection and clean architecture
&lt;/h2&gt;

&lt;p&gt;Clean architecture and hexagonal architecture emphasize separating:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Domain logic.&lt;/li&gt;
&lt;li&gt;Application logic.&lt;/li&gt;
&lt;li&gt;Infrastructure.&lt;/li&gt;
&lt;li&gt;Interfaces.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Dependency injection supports this separation by allowing inner layers to depend on abstractions while outer layers provide concrete implementations.&lt;br&gt;
For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Feymcdy20h7w7pgv7waas.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Feymcdy20h7w7pgv7waas.png" alt=" " width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The application layer does not import infrastructure details.&lt;br&gt;
It only defines the capabilities it needs.&lt;br&gt;
This makes the system easier to evolve and easier to test.&lt;/p&gt;




&lt;h2&gt;
  
  
  When dependency injection may be&amp;nbsp;overkill
&lt;/h2&gt;

&lt;p&gt;Dependency injection is not required for every piece of code.&lt;br&gt;
It is most valuable when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A class has external dependencies.&lt;/li&gt;
&lt;li&gt;You need to test the class in isolation.&lt;/li&gt;
&lt;li&gt;Implementations may change.&lt;/li&gt;
&lt;li&gt;Configuration differs between environments.&lt;/li&gt;
&lt;li&gt;You want to reduce coupling.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It may be unnecessary when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The code is a simple script.&lt;/li&gt;
&lt;li&gt;The function has no external dependencies.&lt;/li&gt;
&lt;li&gt;The behaviour is trivial and stable.&lt;/li&gt;
&lt;li&gt;The overhead outweighs the benefit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use dependency injection intentionally, not as a default ritual.&lt;/p&gt;




&lt;h2&gt;
  
  
  A simple mental&amp;nbsp;model
&lt;/h2&gt;

&lt;p&gt;You can think of dependency injection as answering three questions:&lt;br&gt;
&lt;strong&gt;What does this class&amp;nbsp;need?&lt;/strong&gt;&lt;br&gt;
Declared in the constructor.&lt;br&gt;
&lt;strong&gt;Who provides&amp;nbsp;it?&lt;/strong&gt;&lt;br&gt;
The container, factory, or composition root.&lt;br&gt;
&lt;strong&gt;How is it&amp;nbsp;built?&lt;/strong&gt;&lt;br&gt;
The concrete implementation, configuration, or environment.&lt;br&gt;
The class only answers the first question.&lt;br&gt;
The other questions are handled externally.&lt;br&gt;
This separation is what makes the code more flexible and easier to test.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Dependency injection is not only a framework feature.&lt;br&gt;
It is a design choice about who controls object creation and how dependencies are managed.&lt;br&gt;
When your classes know how to build everything they need, they become tightly coupled to those implementations.&lt;br&gt;
When your classes declare what they need and receive it from the outside, they become easier to test, easier to configure, and easier to change.&lt;br&gt;
This does not mean every function must be a class with injected dependencies.&lt;br&gt;
It means that when a component has external dependencies, those dependencies should be explicit, replaceable, and managed outside the component.&lt;br&gt;
Because good software is not only about making each class work.&lt;br&gt;
It is about designing a system where change does not require rewriting everything that depends on it.&lt;br&gt;
And dependency injection is one of the most practical tools for achieving that.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>backenddevelopment</category>
      <category>designpatterns</category>
    </item>
    <item>
      <title>What Actually Happens When 10,000 Users Hit Your "Buy Now" Button at The Same Time</title>
      <dc:creator>Nsikan Patrick Adaowo</dc:creator>
      <pubDate>Mon, 03 Aug 2026 18:25:56 +0000</pubDate>
      <link>https://dev.to/nsikanadaowo/what-actually-happens-when-10000-users-hit-your-buy-now-button-at-the-same-time-5c18</link>
      <guid>https://dev.to/nsikanadaowo/what-actually-happens-when-10000-users-hit-your-buy-now-button-at-the-same-time-5c18</guid>
      <description>&lt;p&gt;Every e-commerce team dreams of massive traffic spikes—until those spikes actually happen.&lt;/p&gt;

&lt;p&gt;Imagine this: You’ve spent weeks (maybe months) building your e-commerce backend. Your API endpoints are clean, your database schema is normalized, and your authentication flow is solid. You test it with 10 users. It works. You test with 100. It still works.&lt;/p&gt;

&lt;p&gt;Then Black Friday hits. Or your product goes viral on TikTok. Or a celebrity tweet about your app. Suddenly, it’s no longer just one request (or even a hundred)—it’s 10,000, all landing on the same endpoint within the same second, all fighting over the same rows in your database.&lt;/p&gt;

&lt;p&gt;Your server slows to a crawl. Some users get errors. Others get charged twice. A lucky few get their orders, but the rest get nothing.&lt;/p&gt;

&lt;p&gt;This is the exact moment most engineers discover that “it works on my machine” and “it works under concurrency” are two completely different claims.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Went Wrong?
&lt;/h2&gt;

&lt;p&gt;This isn’t just a “traffic spike” problem. It’s a concurrency, architecture, and systems design problem. If you’re building scalable backends—especially in Node.js, NestJS, or any modern stack—you need to understand exactly what happens under the hood when thousands of requests hit your API simultaneously.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Mental Model: One Server, Thousands of Requests
&lt;/h2&gt;

&lt;p&gt;Let’s start with a simple question: Does your server create 10,000 copies of itself when 10,000 users hit it at once?&lt;/p&gt;

&lt;p&gt;No. It doesn’t.&lt;/p&gt;

&lt;p&gt;What actually happens is far more interesting—and more fragile.&lt;/p&gt;

&lt;p&gt;Your server has limited resources: CPU cores, memory, network bandwidth, and database connections. When a flood of requests arrives, your server doesn’t magically scale up. Instead, it tries to juggle those requests using the tools and patterns you’ve (hopefully) built into your architecture.&lt;/p&gt;

&lt;p&gt;In Node.js, for example, there’s only one main JavaScript thread—but that doesn’t mean it handles requests one at a time. It handles them concurrently, not in parallel.&lt;/p&gt;

&lt;h2&gt;
  
  
  Concurrency vs. Parallelism: The Core Distinction
&lt;/h2&gt;

&lt;p&gt;This is where most developers get tripped up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Concurrency&lt;/strong&gt; means multiple requests are in progress at the same time but not necessarily executing at the same time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Parallelism&lt;/strong&gt; means multiple requests are actually executing at the same time, on different CPU cores or threads.&lt;/p&gt;

&lt;p&gt;A common misconception about Node.js is that because it is single-threaded, high traffic will cause requests to pile up like cars stuck at a single toll booth.&lt;/p&gt;

&lt;p&gt;In reality, Node.js is single-threaded but highly concurrent. It uses an event loop to delegate I/O-bound tasks (like database queries, file reads, or external API calls) to the OS or thread pool, then moves on to the next request without waiting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, when 10,000 users hit your “Buy Now” endpoint:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Each request enters the event loop.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the request involves a database query, Node.js offloads that work and immediately starts handling the next request.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the database responds, Node.js picks up where it left off and sends the response.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why Node.js can handle thousands of concurrent connections—even on a single thread.&lt;/p&gt;

&lt;p&gt;But here’s the catch: if your code blocks the event loop, everything grinds to a halt.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where Things Actually Break: The Bottlenecks
&lt;/h2&gt;

&lt;p&gt;Under heavy load, your API doesn’t just fail for no reason—it fails because of bottlenecks. Here are the most common ones:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Blocking the Event Loop&lt;/strong&gt; - If your code does something CPU-heavy in JavaScript—like encrypting passwords synchronously, parsing huge JSON payloads, or running complex calculations—it blocks the event loop. While that one request is hogging the thread, all other requests wait. This is why you should never use fs.readFileSync or heavy crypto in production code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Database Connection Pool Exhaustion&lt;/strong&gt; – Your database doesn’t have infinite connections. Most connection pools are configured to handle 10–100 concurrent connections. If 10,000 requests each try to open a new connection, your pool gets exhausted. Requests start queuing—or worse, failing with “too many connections” errors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Unindexed or Slow Queries&lt;/strong&gt; – A single slow query can block hundreds of requests waiting on the same table. Under load, this becomes a cascade failure: one slow query → connection pool fills up → new requests timeout → users see errors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- External API Calls Without Timeouts&lt;/strong&gt; – If your “Buy Now” endpoint calls a payment gateway, inventory service, or shipping API—and those calls don’t have timeouts—your server can get stuck waiting for responses that never come. Multiply that by 10,000 requests, and you’ve got a server meltdown.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Memory Pressure and Garbage Collection&lt;/strong&gt; – Under heavy load, your server allocates more memory. If you’re not careful, you can trigger frequent garbage collection pauses, which block the event loop and spike latency.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Hidden Threat: Race Conditions
&lt;/h2&gt;

&lt;p&gt;One of the most dangerous—and subtle—problems that emerges under high concurrency is the race condition. This occurs when two or more requests try to modify the same data simultaneously, and the final result depends on the unpredictable order in which the operations complete.&lt;/p&gt;

&lt;p&gt;Consider this classic e-commerce example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fllhwyqd6b8vyp9vt3h57.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fllhwyqd6b8vyp9vt3h57.png" alt=" " width="742" height="146"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Without proper safeguards, two users can both be told an item is in stock, both complete checkout, and both be charged—even though only one unit was available. This is a race condition in its purest form, and it’s exactly why you need the strategies we’ll cover next.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, What Actually Happens When 10,000 Users Click “Buy Now”?
&lt;/h2&gt;

&lt;p&gt;Let’s walk through the sequence step by step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Requests Hit the Load Balancer Your traffic doesn’t hit a single server—it hits a load balancer (like AWS ALB, NGINX, or Cloudflare). The load balancer distributes requests across multiple backend instances. If you don’t have a load balancer, all 10,000 requests hit one server. That server will likely crash.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Each Request Enters the Event Loop On each backend instance (say, a Node.js server), requests enter the event loop. If your code is non-blocking, the server can handle thousands of concurrent requests per instance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Database Queries Are Executed Each request tries to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Check inventory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reserve the item&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Process payment&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create an order record&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your database isn’t optimized (no indexes, no connection pooling, no read replicas), this is where things slow down—and where race conditions can wreak havoc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Responses Are Sent Responses don’t go out in the order the requests arrived. They go out as soon as each request completes. Some users get their order confirmation in 200ms. Others wait 10 seconds. A few get timeouts.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Design for 10,000 Concurrent Users
&lt;/h2&gt;

&lt;p&gt;You don’t need to over-engineer for day one—but you do need to design for scale. Here’s how:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Use Connection Pooling&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Configure your database client (like pg for PostgreSQL or mysql2 for MySQL) to use a connection pool. This reuses connections instead of creating new ones for every request.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fi4f4dw0i2yv2t9l7p98w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fi4f4dw0i2yv2t9l7p98w.png" alt=" " width="577" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Database-Level Locking&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The most direct fix lives at the database layer, using row-level locks. A SELECT ... FOR UPDATE statement tells the database: “Lock this specific row until my transaction finishes—nobody else can read or write it until I’m done.”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdpzlj1y6iy406056l0i2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdpzlj1y6iy406056l0i2.png" alt=" " width="623" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any other transaction trying to touch row 42 simply blocks until this one commits or rolls back. This guarantees correctness, but it introduces lock contention—under extreme concurrency, requests start queuing up waiting for locks, and latency climbs. For a single hyper-popular SKU, this can become a serious bottleneck even though the rest of your database is sitting idle.&lt;/p&gt;

&lt;p&gt;An alternative is optimistic locking, which skips the lock entirely and instead checks a version number at write time:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk56tuewg19j7joky1crw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk56tuewg19j7joky1crw.png" alt=" " width="637" height="105"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Decoupling with Queues&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Locking solves correctness at the cost of making requests wait synchronously. A more scalable pattern is to decouple the write from the request entirely using a message queue (Kafka, RabbitMQ, AWS SQS, Google Pub/Sub).&lt;/p&gt;

&lt;p&gt;Instead of your API handler directly touching the database:&lt;/p&gt;

&lt;p&gt;The incoming request is validated and dropped onto a queue as a message: { userId, productId, requestId }.&lt;/p&gt;

&lt;p&gt;The API immediately responds with something like 202 Accepted — processing.&lt;/p&gt;

&lt;p&gt;A pool of background workers consumes messages off the queue one at a time (or a few at a time) at a rate the database can actually sustain.&lt;/p&gt;

&lt;p&gt;Each worker executes the actual stock-check-and-deduct logic inside a transaction.&lt;/p&gt;

&lt;p&gt;The result (success/failure) gets pushed to the client via websocket, polling, or a notification.&lt;/p&gt;

&lt;p&gt;This converts a spike of 10,000 simultaneous writes into a controlled, sequential stream that the database can process without falling over. The tradeoff is eventual consistency—there’s a small delay between “I clicked buy” and “the system confirms it,” which needs to be communicated clearly in the UI (Spinners) so it doesn’t feel broken.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But here’s the reality check&lt;/strong&gt;: Would you really place all 10,000 requests into Kafka? Not necessarily. Because if you only have 100 units in stock, 99% of those requests are doomed to fail from the start. Dropping them into a queue only wastes worker capacity, memory, and network bandwidth.&lt;/p&gt;

&lt;p&gt;That’s why large-scale flash-sale systems move inventory protection closer to the edge. They preload inventory into a fast in-memory store (like Redis) and use it as a first gate. If Redis says stock = 0, the request is rejected immediately with a polite "Out of Stock" message, never even touching the queue or the primary database. The queue becomes a pathway only for requests that actually have a fighting chance, keeping your workers lean and your database unscathed.&lt;br&gt;
Add a Caching Layer&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Add a Caching Layer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For read-heavy traffic—like thousands of users just checking if a product is in stock before deciding to buy—a cache (Redis, Memcached, or a CDN edge cache) intercepts most of that traffic before it ever reaches the database. Re-running the same read query 10,000 times a second against your primary database is pure waste.&lt;/p&gt;

&lt;p&gt;In high-concurrency “Buy Now” scenarios, Redis can go further and act as the first gate. Many flash-sale systems preload the available inventory into Redis at the start of the sale. When a request arrives, the first thing the API does is try an atomic decrement:&lt;/p&gt;

&lt;p&gt;DECR product:42:stock&lt;/p&gt;

&lt;p&gt;If the value was still positive, the request is allowed to proceed (and later confirmed in the database). If Redis returns a value less than zero, the request is rejected immediately with an “Out of Stock” response. It never reaches Kafka, never reaches the database, and never wastes a connection. This is extremely fast and protects the rest of your system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But… What if Redis crashes after decrementing the stock?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is why Redis should never be your source of truth. It’s a &lt;em&gt;traffic-shaping layer&lt;/em&gt; - an early filter that keeps the stampede from overwhelming the slower, more authoritative systems behind it, not the final authority. The database remains the single source of truth for inventory. If Redis goes down mid-sale, you lose your fast gate, but your database can still fall back on its own row-level locks and atomic updates to guarantee correctness.&lt;/p&gt;

&lt;p&gt;However, The classic hard problem remains: &lt;strong&gt;cache invalidation&lt;/strong&gt;. A stale cache can tell users an item is available when it just sold out. That’s why the Redis inventory numbers are usually treated as a temporary, short-lived view that is reconciled with the database on a short interval or after every successful write.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Use Atomic Operations Instead of Read-Then-Write&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A lot of race conditions disappear entirely if you avoid the "read, then write" pattern altogether and push the arithmetic into the database itself:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnghexbl7qbyy3q010upm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnghexbl7qbyy3q010upm.png" alt=" " width="591" height="60"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This single statement is &lt;strong&gt;atomic&lt;/strong&gt;. The database guarantees that the check and the decrement happen together. Suppose the last item has stock = 1 and two requests arrive at almost the same instant:&lt;/p&gt;

&lt;p&gt;Request A’s UPDATE succeeds. Stock becomes 0.&lt;br&gt;
Request B’s UPDATE finds stock &amp;gt; 0 is now false, so zero rows are updated. The request is rejected.&lt;/p&gt;

&lt;p&gt;No overselling. Only one transaction can successfully claim the final unit.&lt;br&gt;
That’s the pure inventory decrement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But here’s where it gets messy&lt;/strong&gt;. What if you decrement the stock successfully, but the payment gateway times out? Or the user's credit card declines? Or they close their browser mid-checkout?&lt;/p&gt;

&lt;p&gt;Imagine this: Stock is 1. User A reserves it. Your atomic UPDATE sets stock to 0. Then the payment fails. Now the product is completely unavailable—even though nobody actually bought it. You've just locked away your last item for a failed transaction. That's a terrible customer experience.&lt;/p&gt;

&lt;p&gt;Large e-commerce platforms solve this by separating reservation from purchase. Instead of permanently deducting stock at the start of checkout, inventory first moves into a temporary, time-bound reserved state. The stock stays reserved while the user completes payment, but it's not permanently gone yet.&lt;/p&gt;

&lt;p&gt;Only after a successful payment confirmation does the reservation convert into a permanent sale. If the payment fails, or if the user abandons the cart and walks away, the reservation simply expires—say, after 5-10 minutes—and the inventory is atomically released back into the available pool for the next customer.&lt;/p&gt;

&lt;p&gt;This two-phase approach adds a bit of complexity (you now need a background job to sweep expired reservations), but it prevents the heartbreaking scenario where a legitimate buyer is told "out of stock" because someone else's credit card just bounced. It turns your atomic operation from a blunt instrument into a surgical tool—and your users will notice the difference.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Idempotency Keys&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Under load, clients retry. Networks flake, timeouts happen, users double-click. Without protection, this creates duplicate side effects—double charges, double stock deductions.&lt;/p&gt;

&lt;p&gt;The standard fix is an idempotency key: a unique identifier generated client-side (often a UUID) and attached to the request. The server stores which idempotency keys it has already processed and, on seeing a repeat, returns the cached original response instead of re-executing the logic:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5iedx7c0mwr0dqy5uft6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5iedx7c0mwr0dqy5uft6.png" alt=" " width="630" height="82"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Stripe/Paystack’s API is the canonical real-world example of this pattern done well—it’s specifically designed so that retried payment requests never double-charge a customer, as long as the same idempotency key is reused.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Rate Limiting and Backpressure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Not every request deserves to reach your database at full speed. Rate limiting—using algorithms like token bucket or sliding window—caps how many requests per second a given user, IP, or API key can send, rejecting or delaying the rest with a 429 Too Many Requests response.&lt;/p&gt;

&lt;p&gt;This isn’t just about abuse prevention—it’s about protecting your own infrastructure. Related to this is backpressure: a signal that flows backward through a system telling upstream components “slow down, I can’t keep up,” so instead of a slow database causing your API servers to pile up thousands of stalled connections and crash, the system degrades gracefully by shedding or delaying excess load.&lt;/p&gt;

&lt;h2&gt;
  
  
  No Single Solution Solves the Entire Problem
&lt;/h2&gt;

&lt;p&gt;No single technique here solves the whole problem. Locking without queuing creates painful contention. Queuing without idempotency creates duplicate charges. Caching without invalidation lies to users. And none of this work if you haven’t addressed the underlying race conditions that concurrency exposes.&lt;/p&gt;

&lt;p&gt;It’s the combination—each one solving a different failure mode—that lets a system survive a genuine 10,000-request stampede without corrupting data or falling over.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion: Why This Matters for You
&lt;/h2&gt;

&lt;p&gt;As a backend developer, your job isn’t just to write code that works. It’s to write code that works under pressure.&lt;/p&gt;

&lt;p&gt;When you understand what happens when 10,000 users hit your API, you stop thinking in terms of “my code” and start thinking in terms of systems. You ask:&lt;/p&gt;

&lt;p&gt;What happens if this endpoint gets 100x more traffic?&lt;/p&gt;

&lt;p&gt;Where are the bottlenecks?&lt;/p&gt;

&lt;p&gt;How do I fail gracefully?&lt;/p&gt;

&lt;p&gt;What metrics should I monitor?&lt;/p&gt;

&lt;p&gt;This mindset shift is what makes you valuable in remote job markets, freelance gigs, and high-growth startups.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>From Spaghetti to Structure: The Architecture Patterns Every Software Developer Should Know</title>
      <dc:creator>Nsikan Patrick Adaowo</dc:creator>
      <pubDate>Mon, 27 Jul 2026 16:31:00 +0000</pubDate>
      <link>https://dev.to/nsikanadaowo/from-spaghetti-to-structure-the-architecture-patterns-every-software-developer-should-know-2g9l</link>
      <guid>https://dev.to/nsikanadaowo/from-spaghetti-to-structure-the-architecture-patterns-every-software-developer-should-know-2g9l</guid>
      <description>&lt;p&gt;Ever inherited a codebase where changing one thing breaks five others?&lt;/p&gt;

&lt;p&gt;That’s rarely a coding problem. It’s an architecture one.&lt;/p&gt;

&lt;p&gt;When you first join a software team, code organization feels simple. You put database queries in one folder, business logic in another, and UI routes at the top. It feels clean and logical.&lt;/p&gt;

&lt;p&gt;Then the product hits real-world scale.&lt;/p&gt;

&lt;p&gt;Deadlines tighten. Business models pivot based on user behaviour. Features get tacked on hastily over frantic weekends. What started as an elegant codebase slowly evolves into a tangled, unpredictable web. &lt;br&gt;
Adding a simple feature to your checkout flow unexpectedly breaks an analytics pipeline three modules away. You realize you haven’t built a resilient application—you’ve built a house of cards.&lt;/p&gt;

&lt;p&gt;Most developers assume the fix is writing more utilities or refactoring syntax. But syntax doesn’t solve structural decay; architecture does.&lt;/p&gt;

&lt;p&gt;Software architecture patterns aren’t academic exercises or trivia for senior engineering interviews. They are time-tested blueprints designed to manage complexity, preserve developer velocity, and protect systems from falling apart when requirements change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here’s what every developer should understand:&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Real Cost of Spaghetti
&lt;/h2&gt;

&lt;p&gt;Spaghetti architecture doesn’t just make the code ugly. It makes the business slower. Features take longer. Bugs become more expensive. Good engineers start avoiding certain parts of the codebase. New hires take months instead of weeks to become productive. And eventually, someone suggests a full rewrite — which is usually just a more expensive way of making the same mistakes again.&lt;/p&gt;

&lt;p&gt;Most of this pain comes from missing or poorly enforced boundaries. When the UI can talk directly to the database, when domain logic lives inside controllers, when services depend on concrete implementations instead of abstractions, the system becomes a ball of mud. Change anything and the mud shifts.&lt;/p&gt;

&lt;p&gt;Architecture patterns are essentially agreements about where those boundaries should live and what is allowed to cross them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is a definitive guide to the 10 core architectural patterns, broken down by how they actually function in production:&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Layered (N-Tier) Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The Standard Horizontal Separation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Layered architecture is the default structure of almost every traditional backend framework. It divides an application horizontally into stacked tiers—typically Presentation, Business Logic, and Data Access—where each layer only talks to the one directly below it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- How it works in practice:&lt;/strong&gt;&lt;br&gt;
A user hits an endpoint, the Controller parses the request, hands it off to the Service layer to execute business rules, and the Service calls the Repository to write to the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- The Reality Check:&lt;/strong&gt;&lt;br&gt;
 While easy to learn, as your app grows, adding a single feature (like a user preference toggle) requires touching every single layer. Over time, layers become “pass-through tax boundaries” where code just forwards data downward without adding value.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Hexagonal Architecture (Ports &amp;amp; Adapters)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Isolating Core Business Logic from External Tech&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hexagonal Architecture (also known as ports and adapters) flips the traditional hierarchy inside out. Instead of placing the database at the bottom, it places your core domain logic at the exact center. The core defines abstract Ports (interfaces for what it needs—like PaymentGateway or UserRepository), and external technologies implement those ports using Adapters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- How it works in practice:&lt;/strong&gt;&lt;br&gt;
 Your core ordering logic doesn’t care if payments go through Paystack or Stripe, or if data lands in PostgreSQL or MongoDB. It only talks to its internal Port contracts. In other words, your application should not depend on external things. External things should depend on your application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- The Reality Check:&lt;/strong&gt;&lt;br&gt;
 You can test your entire business logic in milliseconds without spinning up a database or mocking complex HTTP calls. The trade-off is writing extra interface boilerplate up front.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Onion Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Concentric Rings of Dependency Control&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Coined by Jeffrey Palermo, Onion Architecture builds on the same principle as Hexagonal, visualization-wise using concentric circles. Domain Entities sit at the very core, surrounded by Domain Services, then Application Services, with Infrastructure and UI residing on the outermost ring.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works in practice:&lt;/strong&gt; The strict rule of Onion Architecture is that dependencies only point inward. Outer rings depend on inner rings, but inner rings never know anything about outer rings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- The Reality Check:&lt;/strong&gt;&lt;br&gt;
It keeps your core domain pristine and framework-agnostic. However, engineers used to quick-and-dirty scripting might find navigating multiple concentric abstraction layers over-engineered for simple CRUD applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Clean Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Uncle Bob’s Unified Structural Blueprint&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Popularized by Robert C. Martin (”Uncle Bob”), Clean Architecture synthesizes Hexagonal and Onion principles into a single, standardized framework. It categorizes code into Entities (enterprise business rules), Use Cases (application business rules), Interface Adapters, and Frameworks/Drivers. The Dependency Rule is the key: source code dependencies can only point inward. Nothing in an inner circle can know about something in an outer circle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- How it works in practice:&lt;/strong&gt; Use Cases orchestrate the flow of data to and from Entities. Frameworks (like Next.js, NestJS, or Express) live in the outermost layer as disposable details.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- The Reality Check:&lt;/strong&gt; It provides absolute clarity on where code belongs. The downside? It can lead to an explosion of files and mappings between DTOs (Data Transfer Objects) and Domain Entities for basic operations. Not every CRUD app needs full Clean Architecture ceremony. The value shows up when the business rules are complex and long-lived. If your domain logic is simple, a lighter approach often wins. The principle, however — protect the business rules from frameworks and delivery mechanisms — remains solid.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Vertical Slice Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Feature-First Autonomy Over Layered Overhead&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of slicing the app horizontally by technical concern (Controllers, Services, Repositories), Vertical Slice Architecture cuts vertically by business feature or use case (e.g., CreateOrder, CancelSubscription).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- How it works in practice:&lt;/strong&gt; Every feature slice is completely self-contained. It owns its endpoint, request handling, business logic, and database access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- The Reality Check:&lt;/strong&gt; It drastically reduces side-effect risks—modifying CreateOrder can never accidentally break CancelSubscription because they share no execution paths. It is arguably the most effective pattern for shipping products fast without technical debt.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Event-Driven Architecture (EDA)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Asynchronous Communication via Reactive Events&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In an Event-Driven system, services don’t make direct, blocking calls to each other. Instead, when a state change occurs, a service emits an Event to a central broker (like Kafka or EventBridge). Other independent services subscribe to those events and react accordingly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- How it works in practice:&lt;/strong&gt; When a user checks out, the Order Service emits OrderPlaced. The Inventory Service listens and reserves stock; the Notification Service listens and emails a receipt; the Analytics Service logs the event.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- The Reality Check:&lt;/strong&gt; It decouples services almost entirely, allowing systems to handle massive asynchronous traffic spikes. The catch? Debugging distributed event chains and handling eventual consistency can be difficult without robust observability. But for many domains, the flexibility is worth the effort.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. CQRS (Command Query Responsibility Segregation)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Splitting Write Operations from Read Pipelines&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;CQRS fundamentally splits an application’s data operations into two distinct pathways: Commands (writes that alter state) and Queries (reads that fetch data).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- How it works in practice:&lt;/strong&gt; The write side focuses heavily on business rules and transactional integrity (using a normalized database). The read side bypasses complex business logic entirely, reading pre-aggregated views from a fast search index or cache.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- The Reality Check:&lt;/strong&gt; It solves extreme performance bottlenecks when read traffic dwarfs write traffic (e.g., social media feeds, e-commerce catalogs). However, running two database models introduces temporary data lag (eventual consistency).&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Service-Oriented Architecture (SOA)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Enterprise-Wide Shared Business Capabilities&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Predating modern microservices, SOA is an architectural style where an enterprise splits its core systems into distinct, reusable services that communicate over a centralized communication bus—historically an Enterprise Service Bus (ESB).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- How it works in practice:&lt;/strong&gt; A centralized ESB handles message transformation, routing, and protocol conversion between large, enterprise-level services (like Billing, ERP, and CRM).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- The Reality Check:&lt;/strong&gt; SOA allowed massive enterprises to integrate disparate legacy systems. However, the central ESB often turned into a heavy, monolithic bottleneck managed by a single team.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Microservices Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Independently Deployable, Fine-Grained Services&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Microservices take the core idea of service decomposition and decentralize it completely. The application is built as a suite of small, autonomous services, each owning its own business domain, repository, and deployment pipeline.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- How it works in practice:&lt;/strong&gt; Instead of sharing a monolithic database, the User Service, Payment Service, and Catalog Service each run their own isolated databases and communicate over lightweight protocols (gRPC or REST).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- The Reality Check:&lt;/strong&gt; Microservices enable large organizations to scale engineering teams independently. But for small teams, they trade simple code problems for complex distributed network, deployment, and tracing problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- The promise is attractive:&lt;/strong&gt; independent deploy-ability, technology freedom, team autonomy, better scaling. But you trade a simple deployment problem for a distributed systems problem. Network latency, partial failures, eventual consistency, distributed transactions, observability, and operational complexity all showing up at the same time.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Serverless Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Function-as-a-Service &amp;amp; Managed Infrastructure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Serverless shifts the entire burden of server management, scaling, and provisioning to cloud providers. You write stateless granular functions (like AWS Lambda) that execute purely in response to triggers or HTTP requests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- How it works in practice:&lt;/strong&gt; When an API route is called, the cloud provider spins up a lightweight execution environment, runs your function, returns the response, and immediately destroys or freezes the container.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- The Reality Check:&lt;/strong&gt; You pay strictly for execution time down to the millisecond, and elasticity is virtually infinite out of the box. The main trade-offs are potential cold-start latencies and vendor lock-in.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, What Actually Matters in Practice
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;- Boundaries beat cleverness:&lt;/strong&gt; Clear ownership and limited knowledge between parts of the system matter more than any specific pattern name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Start simple, evolve deliberately:&lt;/strong&gt; A modular monolith with good internal boundaries is often the best place to begin. Extract services or introduce events when the pain of not doing so becomes clear.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Patterns are tools, not identities:&lt;/strong&gt;&lt;br&gt;
 Saying “we do Clean Architecture” or “we are event-driven” is less useful than understanding the problems each pattern solves and the costs it introduces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Consistency of approach matters more than perfection:&lt;/strong&gt; A codebase that applies one set of principles reasonably well is usually healthier than one that mixes five different architectural styles without clear rules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Architecture is a team sport:&lt;/strong&gt; The best patterns fail if the team doesn’t understand or respect the boundaries. Documentation, code reviews, and shared language matter.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Product Engineering Mindset: Architectural Balance
&lt;/h2&gt;

&lt;p&gt;It’s easy to get seduced by the distributed power of CQRS, Event-Driven processing, and Serverless. But every architectural pattern introduces trade-offs:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Eventual Consistency:&lt;/strong&gt; Reads may lag behind writes by a few milliseconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Operational Overhead:&lt;/strong&gt; Managing message brokers, dead-letter queues, and schema migrations requires real infrastructure discipline.&lt;/p&gt;

&lt;p&gt;A true product engineer doesn’t apply CQRS and Event-Driven systems everywhere. They isolate them to the specific 10% of their application that faces intense write contention or heavy async processing, while keeping the rest of the application simple.&lt;/p&gt;

&lt;p&gt;Scale where it counts, simplify where you can, and keep the user experience at the center of every architectural choice; because architecture isn’t about finding the “best” pattern; it’s about choosing the right set of trade-offs for your current scale.&lt;/p&gt;

</description>
      <category>designpatterns</category>
      <category>backenddevelopment</category>
      <category>eventdriven</category>
      <category>serverless</category>
    </item>
    <item>
      <title>Your Code is a Reflection of Your Standards</title>
      <dc:creator>Nsikan Patrick Adaowo</dc:creator>
      <pubDate>Mon, 20 Jul 2026 22:27:34 +0000</pubDate>
      <link>https://dev.to/nsikanadaowo/your-code-is-a-reflection-of-your-standards-1ed5</link>
      <guid>https://dev.to/nsikanadaowo/your-code-is-a-reflection-of-your-standards-1ed5</guid>
      <description>&lt;p&gt;For a long time, I believed becoming a better developer was simply a matter of writing more code.&lt;br&gt;
The formula seemed straightforward and mechanical: Learn a language. Build a project. Complete a course. Move on to the next framework. Repeat. &lt;/p&gt;

&lt;p&gt;For a while, that high-velocity approach worked surprisingly well. Every new feature taught me something. Every bug forced me to dive into documentation files. Every tutorial introduced another technology that felt exciting.&lt;/p&gt;

&lt;p&gt;But as I began transitioning deeper into product engineering, I noticed a strange paradox.&lt;br&gt;
Although I was learning new tools constantly, I wasn't becoming dramatically better at solving real-world problems. I could spin up an application. I could create an API. I could design a relational database. Yet, whenever I looked at systems designed by experienced product engineers, their work felt fundamentally different.&lt;br&gt;
Not because it was more complex. Actually, it was usually much simpler.&lt;/p&gt;

&lt;p&gt;Their architecture was easier to reason about. Their projects had strategic restraint. Their decisions seemed completely intentional. Somehow, they weren't just writing software; they were designing an asset.&lt;/p&gt;

&lt;p&gt;For months, I assumed this clarity came from sheer years on the job. Eventually, I realized something else was happening. The senior engineers I admired didn't just look at documentation or scroll through technical blog posts. They read books that forced them to think deeply about system dynamics and human context.&lt;/p&gt;

&lt;p&gt;Right now, I am reading Clean Code by Robert C. Martin. It is a text many took for granted during the "vibe coding" boom, but in 2026, its principles have become an aggressive competitive advantage.&lt;/p&gt;

&lt;p&gt;Before picking up this book, my primary goal as a developer was shortsighted: Make the program work. &lt;/p&gt;

&lt;p&gt;If the application produced the correct output and the UI looked clean, I considered the task complete. I was focused on the "surface."&lt;br&gt;
Clean Code forces you to look at the "subsurface."&lt;/p&gt;

&lt;p&gt;Software rarely lives for a week or a month. In a startup environment, a product survives, evolves, and shifts over years. This means the code you ship today will be read, modified, and debugged far more often than it is written. Uncle Bob reminds us of a truth that every product engineer must internalize: Programming isn’t just communication with a compiler; it’s communication with people.&lt;/p&gt;

&lt;p&gt;One specific concept that shifted my mindset was the philosophy behind naming. On the surface, variable naming sounds trivial. The computer doesn’t care if a variable is named &lt;em&gt;t&lt;/em&gt; or &lt;em&gt;total&lt;/em&gt;. They produce identical machine code.&lt;br&gt;
But a product engineer doesn't build for the machine; they build for the team and the user. One variable forces future developers to guess; the other tells a story. &lt;/p&gt;

&lt;p&gt;When you have to touch a codebase six months later to pivot a feature based on customer feedback, clear naming is the difference between an elegant iteration and a broken system.&lt;/p&gt;

&lt;p&gt;Clean Code challenged my habits. Functions that stretched over hundreds of lines suddenly looked like architectural debt. Comments became a symptom of poor expression rather than a sign of good documentation. Complex conditional logic became something to simplify, not tolerate.&lt;/p&gt;

&lt;p&gt;As a product engineer, I’m glued already. This book has permanently changed how I review my own work. I no longer just ask, "Does this run?" I now ask, "Will another developer understand the intent behind this system without needing a meeting?"&lt;/p&gt;

&lt;p&gt;The tools and frameworks change every two months, but the fundamentals of building a sustainable system remain permanent. Clean Code isn't teaching you a tool; it’s training your judgment.&lt;/p&gt;

</description>
      <category>softwareengineering</category>
      <category>cleancode</category>
    </item>
  </channel>
</rss>
