<?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: Leonardo Mangano</title>
    <description>The latest articles on DEV Community by Leonardo Mangano (@hoxon).</description>
    <link>https://dev.to/hoxon</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F618717%2Fb916d55a-862b-4539-837e-b7f5224949b3.jpeg</url>
      <title>DEV Community: Leonardo Mangano</title>
      <link>https://dev.to/hoxon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hoxon"/>
    <language>en</language>
    <item>
      <title>Circular dependencies between Microservices</title>
      <dc:creator>Leonardo Mangano</dc:creator>
      <pubDate>Tue, 17 May 2022 16:38:31 +0000</pubDate>
      <link>https://dev.to/cloudx/circular-dependencies-between-microservices-11hn</link>
      <guid>https://dev.to/cloudx/circular-dependencies-between-microservices-11hn</guid>
      <description>&lt;p&gt;Circular dependencies are a bad practice in Microservices architecture that usually goes unnoticed. Yet avoiding them is crucial to reduce complexity in this architectural pattern.&lt;/p&gt;

&lt;p&gt;When working in monolithic systems, there's widespread agreement that these dependencies are evil. On microservices, though, the story is completely different. In this pattern, we treat services as independent modules that communicate almost without restriction. This approach is very prone to creating circular dependencies.&lt;/p&gt;

&lt;p&gt;Having circular dependencies between microservices will result in services that are hardly maintainable. You'll not be able to think on a single service at a time as they'll be highly coupled.&lt;/p&gt;

&lt;p&gt;Let's explore some practices that we want to avoid to get rid of circular dependencies in our Microservices architecture.&lt;/p&gt;

&lt;h2&gt;
  
  
  Leaking domain knowledge
&lt;/h2&gt;

&lt;p&gt;A very imperceptible way of creating circular dependencies between services is sending requests with IDs, value objects, or any other kind of knowledge that belongs to the client service domain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Suppose we have these services:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ProductService&lt;/li&gt;
&lt;li&gt;InvoiceService&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Inside our &lt;strong&gt;ProductService&lt;/strong&gt; we will surely have a &lt;strong&gt;Product&lt;/strong&gt; with each entity having a &lt;strong&gt;ProductId&lt;/strong&gt;. In this example &lt;strong&gt;ProductService&lt;/strong&gt; will be sending a request to &lt;strong&gt;InvoiceService&lt;/strong&gt; to generate an invoice that could be something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /invoice
{ productId: 3, quantity: 1, ... }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this situation &lt;strong&gt;ProductService&lt;/strong&gt; has to know that some invoice service exists to send the request. However, our &lt;strong&gt;InvoiceService&lt;/strong&gt; will also be aware of the existence of some &lt;strong&gt;Product&lt;/strong&gt; entity that is in the domain of the caller. A good way to verify if we are creating a circular dependency is thinking about the &lt;strong&gt;InvoiceService&lt;/strong&gt; as a separated entity. If we completely remove the &lt;strong&gt;ProductService&lt;/strong&gt;, will this request still make sense as it is? The obvious answer will be 'No'.&lt;/p&gt;

&lt;p&gt;To solve this, first we have to decide which service will depend on the other, say Product on Invoice. After we decide that &lt;strong&gt;InvoiceService&lt;/strong&gt; will not depend on &lt;strong&gt;ProductService&lt;/strong&gt;, we need to think about the former in isolation. Do we really need to receive a &lt;strong&gt;ProductId&lt;/strong&gt; to validate the existence of the product, get its unit price and description, and then generate the invoice? Or do we just need a &lt;strong&gt;ProductCode&lt;/strong&gt; to display in the invoice? Most of the time the answer will be that receiving the &lt;strong&gt;ProductCode&lt;/strong&gt;, &lt;strong&gt;UnitPrice&lt;/strong&gt; and &lt;strong&gt;Description&lt;/strong&gt; in the request is good enough. Trusting the information received from services behind our control is crucial to avoid unnecessary validations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /invoice
{
  productCode: 'A123',
  description: 'Nice product',
  unitPrice: 99.9,
  quantity: 1,
  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is just a simple example but keep in mind that there are a lot of ways to leak domain knowledge to other services without us noticing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choreography
&lt;/h2&gt;

&lt;p&gt;This communication pattern seems to be very common in Event-Driven systems. In it, services emit events into a common Event Bus so others can react to them to complete a complex distributed process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;UserService&lt;/strong&gt; emits a &lt;code&gt;UserRegisteredEvent&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CreditService&lt;/strong&gt; receives that &lt;code&gt;UserRegisteredEvent&lt;/code&gt; and validates the credit of that person&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CreditService&lt;/strong&gt; emits a &lt;code&gt;CreditValidatedEvent&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UserService&lt;/strong&gt; receives that &lt;code&gt;CreditValidatedEvent&lt;/code&gt; and finishes the user creation process&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some people may think that, as we are using events to communicate between services, we are completelly decoupling them. That is only partially true, because the only kind of coupling we are removing is the temporal coupling. On the other hand, we are now creating a circular dependency between them because events, as requests, are part of the API of our services.&lt;/p&gt;

&lt;p&gt;One possible solution to this, without removing the asynchronous nature of our system, is using commands and callbacks for responses. In our example, &lt;strong&gt;UserService&lt;/strong&gt; attempts to validate the credit of the person to finish the bigger  process of registering a user. Modeling this with events is a mistake because this is actually a &lt;em&gt;command&lt;/em&gt;: something to which we expect some response to continue with our work.&lt;/p&gt;

&lt;p&gt;To overcome this, we'll need to change a few things. First, our CreditService will need to have an input topic/queue to receive commands. Apart from that, instead of emiting a &lt;strong&gt;UserRegisteredEvent&lt;/strong&gt; (we can still do it for other purposes) we are going to explicitly send a &lt;strong&gt;ValidateCreditCommand&lt;/strong&gt; into that topic. In the header of that command we'll specify a callback topic where we want to receive the response. In this case, that'll be the input topic of our &lt;strong&gt;UserService&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now, if we look at our &lt;strong&gt;CreditService&lt;/strong&gt; in isolation, we will notice that it doesn't know anything about our &lt;strong&gt;UserService&lt;/strong&gt; anymore. With this simple tweak, we have removed the circular dependency.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Having to think about or change a lot of services for every business requirement is tiresome and a good symptom of circular dependencies in our Microservices architecture. There are plenty of examples that I may be missing, but the main point is that they are something that every developer wants to avoid. As an advice, we also need to pay atention when implementing Microservices to avoid them at all costs. This is especially true when implementing microservices. Avoiding them will allow us to think in one service at a time and reduce the cognitive load in our brain.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>programming</category>
      <category>microservies</category>
      <category>distributedsystems</category>
    </item>
  </channel>
</rss>
