<?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: Prateek</title>
    <description>The latest articles on DEV Community by Prateek (@jainsahab).</description>
    <link>https://dev.to/jainsahab</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%2F20613%2F8b72c0f1-a4e2-4271-b0bd-28f03b59222d.jpg</url>
      <title>DEV Community: Prateek</title>
      <link>https://dev.to/jainsahab</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jainsahab"/>
    <language>en</language>
    <item>
      <title>Asynchronous Processing with message queues</title>
      <dc:creator>Prateek</dc:creator>
      <pubDate>Tue, 24 Sep 2019 13:21:45 +0000</pubDate>
      <link>https://dev.to/jainsahab/asynchronous-processing-with-message-queues-189b</link>
      <guid>https://dev.to/jainsahab/asynchronous-processing-with-message-queues-189b</guid>
      <description>&lt;p&gt;Ever heard of &lt;em&gt;fire and forget&lt;/em&gt; processing? If not, a short and simple scenario which can help us understand this is where a &lt;em&gt;caller&lt;/em&gt; returns immediately upon invocation, while the actual execution of &lt;em&gt;callee&lt;/em&gt; is being executed parallelly. In a nutshell, &lt;em&gt;caller&lt;/em&gt; is not blocked for the task to be finished after initiating &lt;em&gt;callee's&lt;/em&gt; execution. Let's see a real-life example to understand it better.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RequestController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nd"&gt;@PostMapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/order"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;placeOrder&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;OrderService&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;placeOrder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nc"&gt;MailService&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;sendMail&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SUCCESS&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MailService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;sendMail&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// mail sending logic&lt;/span&gt;
    &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above &lt;em&gt;kotlin&lt;/em&gt; snippet, we can see RequestController upon receiving the request does two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Place an actual order by using &lt;code&gt;OrderService&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Send a mail notification using &lt;code&gt;MailService&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If we again focus on &lt;code&gt;MailService&lt;/code&gt; code, it sends a mail in a new &lt;code&gt;Thread&lt;/code&gt;. By using a new &lt;code&gt;Thread&lt;/code&gt;, &lt;code&gt;MailService&lt;/code&gt; is ensuring that it's caller isn't blocked for it to finish. Since sending a mail could be a heavy IO operation (establishing a connection with SMTP server which is prone to network errors), Hence &lt;code&gt;Client&lt;/code&gt; shouldn't wait for the server to send mail which can be executed in an &lt;code&gt;Asynchronous&lt;/code&gt; manner.&lt;/p&gt;

&lt;p&gt;Apparently, if an order is placed but mail sending logic fails because of any error. &lt;code&gt;Client&lt;/code&gt; would still receive a &lt;strong&gt;success&lt;/strong&gt; response indicating the status of an order placed. Since mail notification is a way to notify the user regarding the order placed, it's failure shouldn't be a snag in the user's main journey.&lt;/p&gt;

&lt;p&gt;It's kind of &lt;strong&gt;Notification&lt;/strong&gt; to &lt;code&gt;MailService&lt;/code&gt; to initiate its execution which is achieved by calling a function of &lt;code&gt;MailService&lt;/code&gt;. This kind of communication is required where time-consuming processing needs to be done, such as processing large volumes of data but no immediate result is sought by the caller.&lt;/p&gt;

&lt;p&gt;Now we all know what asynchronous procedures are and how they can be effectively leveraged in an application process, but &lt;em&gt;What about multiple processes? such as Microservices !!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In a microservices architecture, each service is designed as a self-sufficient, independent piece of software. In such an architecture, a single-use case often consists of multiple HTTP calls to multiple services. HTTP calls are the synchronous way of microservices to communicate with each other because requester expects an immediate response. Similarly, we want our services to work in an asynchronous manner too. But wait, we can't call a function which is running as part of a different process (might be on a different machine). This is where &lt;a href="https://en.wikipedia.org/wiki/Message_queue" rel="noopener noreferrer"&gt;message-queue&lt;/a&gt; comes to rescue us.&lt;/p&gt;

&lt;h4&gt;
  
  
  Message Queues
&lt;/h4&gt;

&lt;p&gt;Message queues provide an asynchronous way of communication to multiple application processes. A process that passes the message to other process is called a &lt;em&gt;Producer&lt;/em&gt; and the system receives the message is known as &lt;em&gt;Consumer&lt;/em&gt;. &lt;em&gt;Producer&lt;/em&gt; puts a message on the message-queue and does not require an immediate response and continues its own processing.&lt;/p&gt;

&lt;p&gt;Our last &lt;code&gt;Email&lt;/code&gt; example is a perfect candidate to understand this asynchronous messaging system.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F2lgq2gcwy39d4pcw8m2s.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F2lgq2gcwy39d4pcw8m2s.jpg" alt="Message broker email example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When order service receives an order request from the client, it notifies email service by publishing a message on message broker and doesn't wait for it to finish and continue doing its own processing if any.&lt;/p&gt;

&lt;h5&gt;
  
  
  Decoupling
&lt;/h5&gt;

&lt;p&gt;This way of communication decouples the &lt;em&gt;Producer&lt;/em&gt; from &lt;em&gt;Consumer&lt;/em&gt; i.e. they have no knowledge of each other and communicate through a message broker. Components in such an architecture are oblivious to each other and are easier to maintain, extend and debug.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fzntza4umk2bmaxru6t5f.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fzntza4umk2bmaxru6t5f.jpg" alt="Message broker multiple consumers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We just added two more components which are interested in listening &lt;code&gt;order placed&lt;/code&gt; message. &lt;code&gt;SMS service&lt;/code&gt; now can send a message when an order is placed and same way &lt;code&gt;Inventory service&lt;/code&gt; can start packaging and deliver the goods. We can see how easy it is to extend the system to multiple consumers listening to the same kind of messages without &lt;strong&gt;disturbing&lt;/strong&gt; the producer since &lt;code&gt;Message broker&lt;/code&gt; is the only part which consumers are concerned about. These decoupled components can evolve differently with different languages, different frameworks or by different teams.&lt;/p&gt;

&lt;h5&gt;
  
  
  Reliability
&lt;/h5&gt;

&lt;p&gt;Both &lt;em&gt;Producer(s)&lt;/em&gt; and &lt;em&gt;Consumer(s)&lt;/em&gt; need not interact with message breaker simultaneously. &lt;em&gt;Producer&lt;/em&gt; and &lt;em&gt;Consumer&lt;/em&gt; are totally isolated and need not follow each other states. &lt;em&gt;Producer&lt;/em&gt; can just put the message on the queue and then continue processing. &lt;em&gt;Consumer&lt;/em&gt; which is working independently will pick up the message from the queue when it is able to process them. If &lt;em&gt;Consumer&lt;/em&gt; isn't ready or not available to process yet, those messages will be stored and delivered when &lt;em&gt;Consumer&lt;/em&gt; is healthy and available to process the messages.&lt;/p&gt;

&lt;p&gt;Scenarios, where you can't afford to miss these messages, can be a strong use case for Message queues. Ex: &lt;a href="https://gdpr.eu/" rel="noopener noreferrer"&gt;GDPR compliance&lt;/a&gt;. Let's say in a microservices architecture, Our application has the user data saved across multiple DBs. When a user from the provided user interface triggers a &lt;a href="https://gdpr-info.eu/art-17-gdpr/" rel="noopener noreferrer"&gt;Delete my data&lt;/a&gt; action, that action has to be propagated to multiple services to delete data from multiple DBs. What if one of the services is not healthy and not able to receive any HTTP action, It'll not be triggered and data remain undeleted which makes the whole system GDPR &lt;em&gt;uncompliant&lt;/em&gt;. 😵&lt;/p&gt;

&lt;p&gt;Reliable delivery of messages would be beneficial in this scenario. On the initial trigger, a &lt;em&gt;delete all data&lt;/em&gt; can be published to all the queues. At the appropriate time, when consumers are healthy and ready to process messages, they will be delivered to and processed by consumers.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;We saw how async processing helps in building interactive systems by reducing the waiting time for end-users. Along with that, we also saw how message queues bring in decoupling resulting in a well structured, extendable, maintainable and easy to debug systems.&lt;br&gt;
Last but not least, &lt;em&gt;Reliability&lt;/em&gt; ensures &lt;em&gt;No message left behind. :)&lt;/em&gt;&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>queue</category>
      <category>asynchronous</category>
    </item>
    <item>
      <title>Hi, I'm Prateek</title>
      <dc:creator>Prateek</dc:creator>
      <pubDate>Fri, 02 Jun 2017 01:06:30 +0000</pubDate>
      <link>https://dev.to/jainsahab/hi-im-prateek</link>
      <guid>https://dev.to/jainsahab/hi-im-prateek</guid>
      <description>&lt;p&gt;I have been coding for 2 years.&lt;/p&gt;

&lt;p&gt;You can find me on GitHub as &lt;a href="https://github.com/jainsahab" rel="noopener noreferrer"&gt;jainsahab&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I live in New Delhi.&lt;/p&gt;

&lt;p&gt;I work for ThoughtWorks&lt;/p&gt;

&lt;p&gt;I mostly program in these languages: Java, Javascript.&lt;/p&gt;

&lt;p&gt;I am currently learning more about Data Science.&lt;/p&gt;

&lt;p&gt;Nice to meet you.&lt;/p&gt;

</description>
      <category>introduction</category>
    </item>
  </channel>
</rss>
