<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shubham Singh Parmar on Medium]]></title>
        <description><![CDATA[Stories by Shubham Singh Parmar on Medium]]></description>
        <link>https://medium.com/@shubhamparmar92?source=rss-f1465e7ea2d3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*bC8QbgZfZsgWp61S</url>
            <title>Stories by Shubham Singh Parmar on Medium</title>
            <link>https://medium.com/@shubhamparmar92?source=rss-f1465e7ea2d3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 13:50:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shubhamparmar92/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Managing Distributed Transactions: Rollback Strategies in Microservices Architecture]]></title>
            <link>https://shubhamparmar92.medium.com/managing-distributed-transactions-rollback-strategies-in-microservices-architecture-5cfabec73fdc?source=rss-f1465e7ea2d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/5cfabec73fdc</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[saga-pattern]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <dc:creator><![CDATA[Shubham Singh Parmar]]></dc:creator>
            <pubDate>Thu, 16 May 2024 05:22:17 GMT</pubDate>
            <atom:updated>2024-05-16T05:22:17.756Z</atom:updated>
            <content:encoded><![CDATA[<p>In the realm of microservices architecture, where applications are composed of loosely coupled, independently deployable services, managing distributed transactions becomes a crucial challenge. The decentralized nature of microservices introduces complexities in maintaining data consistency across multiple services, especially during failures or errors. In this blog, we will explore the importance of rollback strategies in distributed transactions within a microservices architecture and discuss implementation techniques with example code snippets in Java.</p><p><strong>Understanding Distributed Transactions:</strong></p><p>Distributed transactions involve multiple services coordinating to execute a single transaction that spans across different databases or services. Ensuring the atomicity, consistency, isolation, and durability (ACID) properties of transactions becomes intricate when they span across microservices due to network latencies, failures, and varying execution speeds.</p><p><strong>Rollback Strategies in Microservices:</strong></p><p>Rollback strategies play a vital role in maintaining data consistency and integrity within a microservices environment. Here are some common rollback strategies employed:</p><ol><li><strong>Compensation Transactions:</strong></li></ol><ul><li>In this approach, compensating transactions are executed to undo the changes made by the original transaction in case of failure.</li><li>Example: Suppose a transfer of funds fails midway due to an error. A compensating transaction could be initiated to reverse the transfer, ensuring data consistency.</li></ul><p>2. <strong>Saga Pattern:</strong></p><ul><li>The Saga pattern breaks down a distributed transaction into a series of smaller, localized transactions, each with its compensating transaction.</li><li>If a step fails, the corresponding compensating transaction is executed to revert the changes made by previous steps.</li><li>Example: In an e-commerce application, when a user places an order, each microservice involved (inventory, payment, shipping) performs its part of the transaction, with compensating actions in case of failure.</li></ul><p><strong>Example Implementation in Java:</strong></p><p>Let’s consider a simplified scenario of a banking system where funds transfer between accounts is performed across multiple microservices.</p><pre>// TransferService.java<br>public class TransferService {<br>    public void transferFunds(Account source, Account destination, double amount) {<br>        try {<br>            // Deduct funds from source account<br>            source.debit(amount);<br>            <br>            // Transfer funds to destination account<br>            destination.credit(amount);<br>            <br>            // Commit transaction if successful<br>            // ...<br>        } catch (Exception ex) {<br>            // Rollback transaction if any step fails<br>            // source.rollbackDebit(amount);<br>            // destination.rollbackCredit(amount);<br>            // Log error<br>        }<br>    }<br>}</pre><pre>// Account.java<br>public class Account {<br>    private double balance;<br><br>    public void debit(double amount) {<br>        // Deduct amount from balance<br>        // ...<br>        // Throw exception if insufficient funds<br>    }<br>    <br>    public void credit(double amount) {<br>        // Add amount to balance<br>        // ...<br>    }<br>}</pre><p>In the above example, the TransferService orchestrates the transfer transaction between two accounts. If any step fails, rollback actions can be performed by invoking compensating methods (rollbackDebit and rollbackCredit) on the respective accounts.</p><p><strong>Conclusion:</strong></p><p>Managing distributed transactions in a microservices architecture requires careful consideration of rollback strategies to maintain data consistency and integrity. By implementing compensation transactions or adopting patterns like Saga, developers can mitigate the complexities associated with distributed transactions. However, it’s essential to tailor these strategies according to specific application requirements and business needs. With the right approach and implementation techniques, ensuring transactional integrity in a microservices environment becomes achievable.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5cfabec73fdc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reversing a String Sentence in Java.]]></title>
            <link>https://shubhamparmar92.medium.com/reversing-a-string-sentence-in-java-2e4cbf21f0fc?source=rss-f1465e7ea2d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e4cbf21f0fc</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Shubham Singh Parmar]]></dc:creator>
            <pubDate>Wed, 14 Apr 2021 15:21:13 GMT</pubDate>
            <atom:updated>2021-04-14T15:21:13.668Z</atom:updated>
            <content:encoded><![CDATA[<p>This problem is one of the variants of String manipulation questions asked in interviews.</p><h4>Problem:</h4><p>Given a sentence “Welcome to my blog”, the expectation is to reverse the sentence in such a way all the alphabets get in reverse order but the spaces position remains intact.</p><p><strong>Sample Input:</strong> Welcome to my blog</p><p><strong>Sample Output:</strong> Golbymo te mo clew</p><h4>Solution:</h4><pre>public class ReverseString {<br> public static void main(String[] args) {<br>   <br>   String s = &quot;Welcome to my blog&quot;;<br>   int len = s.length();<br>   int count = 0;<br>   <br>   for (int i = 0; i &lt; len; i++) {</pre><pre>    if (s.charAt(i) == &#39; &#39;) {</pre><pre>      System.out.print(&#39; &#39;);</pre><pre>     } <br>    else if (s.charAt(len - 1 - count) == &#39; &#39;) {<br>     count++;<br>     i--;<br>    } <br>    else {<br>     System.out.print(s.charAt(len - 1 - count));<br>     count++;<br>    }<br>  }<br> }<br>}</pre><h4>Run Complexity:</h4><ul><li><strong>Time Complexity : O(n)</strong></li><li><strong>Space Complexity : O(1)</strong></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e4cbf21f0fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sorting an array containing 0s, 1s & 2s.]]></title>
            <link>https://shubhamparmar92.medium.com/sorting-an-array-containing-0s-1s-2s-46e5035057a6?source=rss-f1465e7ea2d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/46e5035057a6</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[sorting-algorithms]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[arrays]]></category>
            <dc:creator><![CDATA[Shubham Singh Parmar]]></dc:creator>
            <pubDate>Tue, 13 Apr 2021 16:54:10 GMT</pubDate>
            <atom:updated>2021-04-13T16:54:10.381Z</atom:updated>
            <content:encoded><![CDATA[<p>This question was asked to one of my friend in an interview so I thought sharing the solution for future reference.</p><h4>Problem :</h4><p>Given an array of integers containing only 0s, 1s and 2s. we need to sort the array in ascending order.</p><p><strong>Sample Input : 2, 2, 2, 0, 0, 0, 0, 1, 1</strong></p><p><strong>Sample Output : 0, 0, 0, 0, 1, 1, 2, 2, 2, 2</strong></p><h4><strong>Solution:</strong></h4><p>The approach that I have applied here is maintaining the count of each digit in <em>count </em>variables respective to the digits. Once we have the count for each digit we can simply run three for loops sequentially to print the digits as per respective count.</p><h4>Code:</h4><pre>static void sortandPrint(int a[]){<br> <br>  int x[] = new int[3];<br> <br>  for(Integer i:x){<br>   i = 0;<br>  }<br>  <br>  for(Integer i : a){<br>   x[i] = x[i]+1;<br>  }<br>  <br>  for(int i= 0;i&lt;x[0];i++)<br>   System.out.print(0+ &quot; &quot;);</pre><pre>  for(int i= 0;i&lt;x[1];i++)<br>   System.out.print(1+ &quot; &quot;);</pre><pre>  for(int i= 0;i&lt;x[2];i++)<br>   System.out.print(2+ &quot; &quot;);</pre><pre>}</pre><h4><strong>Complexity:</strong></h4><p>Run complexity of the code is O(n) as the traversal of the array is only once.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46e5035057a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stock Purchase and Sell to Maximize Profit in a week in Java]]></title>
            <link>https://shubhamparmar92.medium.com/stock-purchase-and-sell-to-maximize-profit-in-a-week-in-java-c140638bf4cf?source=rss-f1465e7ea2d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/c140638bf4cf</guid>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Shubham Singh Parmar]]></dc:creator>
            <pubDate>Sat, 10 Apr 2021 18:46:52 GMT</pubDate>
            <atom:updated>2021-04-11T04:32:39.727Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This is a trending problem based on array implementations and is being liked by plenty of the interviewers now a days, I have encountered many times and I also love to ask this problem to the candidates I come across.</em></p><h3><strong>Problem :</strong></h3><p>Given a list containing future predictions of share prices, find the maximum profit earned by buying and selling using 1 transaction.</p><p><em>Sample input</em> : [3,5,2,6,9,1,4]</p><p><em>Sample output</em> : 7</p><h3><strong>Solution:</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*kn1zY2IaCP4PDvEhnAqyDg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c140638bf4cf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>