<?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 Mohana Krishna Kavali on Medium]]></title>
        <description><![CDATA[Stories by Mohana Krishna Kavali on Medium]]></description>
        <link>https://medium.com/@mohanakrishnakavali?source=rss-c4e567008fd7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ykmUt1K4QrbppzUvyC91hA.jpeg</url>
            <title>Stories by Mohana Krishna Kavali on Medium</title>
            <link>https://medium.com/@mohanakrishnakavali?source=rss-c4e567008fd7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 19:03:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mohanakrishnakavali/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[How ConcurrentHashMap Achieves Thread Safety]]></title>
            <link>https://medium.com/@mohanakrishnakavali/how-concurrenthashmap-achieves-thread-safety-8b8ba924b222?source=rss-c4e567008fd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b8ba924b222</guid>
            <category><![CDATA[java-concurrency]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[collections-framework]]></category>
            <category><![CDATA[threads]]></category>
            <dc:creator><![CDATA[Mohana Krishna Kavali]]></dc:creator>
            <pubDate>Sat, 05 Sep 2026 00:18:51 GMT</pubDate>
            <atom:updated>2026-09-05T00:20:33.955Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Px_oaZyrOfeAXFEghJo5qQ.png" /></figure><p>Imagine when we are building a <strong>high-traffic e-commerce application</strong>.</p><p>Thousands of users are browsing products, adding items to their carts, checking inventory, and placing orders at the same time.</p><p>Behind the scenes, our application may need to maintain shared data such as:</p><pre>Map&lt;String, Integer&gt; productStock;</pre><p>For example:</p><pre>Product: &quot;iPhone&quot;<br>Stock: 25</pre><p>Now imagine 100 requests arriving at almost the same time:</p><pre>Request 1  → Check iPhone stock<br>Request 2  → Update iPhone stock<br>Request 3  → Check iPhone stock<br>Request 4  → Purchase iPhone<br>...<br>Request 100 → Update iPhone stock</pre><p>All these requests may be handled by different threads.</p><p>If those threads access a normal HashMap concurrently, we can run into <strong>race conditions, visibility problems, and inconsistent results</strong>.</p><p>One obvious solution is to synchronize access:</p><pre>Map&lt;String, Integer&gt; stock =<br>    Collections.synchronizedMap(new HashMap&lt;&gt;());</pre><p>This makes the map thread-safe, but there is a trade-off: synchronization can restrict concurrent access and become a bottleneck when many threads are accessing shared data.</p><p>This is where ConcurrentHashMap becomes interesting.</p><pre>ConcurrentHashMap&lt;String, Integer&gt; stock =<br>    new ConcurrentHashMap&lt;&gt;();</pre><p>It is designed specifically for concurrent access, allowing multiple threads to work with the map while maintaining thread-safety guarantees.</p><p>But this raises an interesting question:</p><blockquote><strong><em>How does </em></strong><strong><em>ConcurrentHashMap actually achieve thread safety without simply locking the entire map?</em></strong></blockquote><p>To answer that, we need to look under the hood at concepts such as <strong>CAS (Compare-And-Swap), volatile memory semantics, bin-level synchronization, and atomic operations</strong>.</p><h4><strong>Let’s break it down :</strong></h4><p>A useful way to visualize a modern ConcurrentHashMap is to think of it as an array of <strong>bins</strong> (buckets), where each bin can contain one or more nodes.</p><pre>ConcurrentHashMap<br>                           <br>                    Internal Table<br>                           |<br>       +---------+---------+---------+---------+<br>       |         |         |         |         |<br>     Bin 0     Bin 1     Bin 2     Bin 3     Bin 4<br>       |         |                   |         |<br>      Node      Node                Node      Node<br>                 |                   |<br>                Node                Node</pre><p>Different threads can work with different bins concurrently.</p><pre>Thread 1 ───────→ Bin 0<br>Thread 2 ───────→ Bin 2<br>Thread 3 ───────→ Bin 4</pre><p>There is no need to lock the entire map for every update.</p><h3>The put() Operation: A Simplified View</h3><p>Suppose we execute:</p><pre>map.put(&quot;Java&quot;, 100);</pre><p>Conceptually, the operation looks like this:</p><pre>                  put(&quot;Java&quot;, 100)<br>                         |<br>                         v<br>                  Calculate hash<br>                         |<br>                         v<br>                  Find bin index<br>                         |<br>                         v<br>                  Is bin empty?<br>                    /        \<br>                  YES         NO<br>                   |           |<br>                   v           v<br>                  CAS      Examine nodes<br>                   |           |<br>              +----+----+      |<br>              |         |      |<br>           Success    Failure  |<br>              |         |      |<br>              v         v      v<br>             Done     Retry   Lock bin if<br>                              modification<br>                              is required</pre><p>The important detail is what happens when the bin is empty.</p><p>The implementation can attempt to install the first node using <strong>CAS (Compare-And-Swap)</strong> rather than immediately acquiring a lock.</p><h4><strong>CAS in Action</strong></h4><p>Imagine two threads trying to insert into the same empty bin.</p><pre>Initial state:<br><br>  Bin 5<br>    |<br>   null</pre><p>Both threads attempt an atomic update:</p><pre>Thread 1 → CAS(null, Node A)<br>Thread 2 → CAS(null, Node B)</pre><p>Only one operation can successfully change the value from null.</p><p>For example:</p><pre>Thread 1 → SUCCESS<br>Thread 2 → FAILURE</pre><p>The resulting state becomes:</p><pre>Bin 5<br>  |<br>Node A</pre><p>Thread 2 observes that the bin is no longer empty and follows the appropriate update path.</p><p>This is one of the reasons ConcurrentHashMap can avoid unnecessary locking for common insertion cases.</p><h4>What If the Bin Already Contains Nodes?</h4><p>Suppose we have:</p><pre>Bin 5<br><br>Node A → Node B → Node C</pre><p>Another thread wants to update this bin.</p><p>Modern ConcurrentHashMap can synchronize on the first node of the bin while performing the necessary update.</p><p>Conceptually:</p><pre>synchronized (firstNode) {<br>    // safely modify the bin<br>}</pre><p>The important point is that this is <strong>not a lock on the entire map</strong>.</p><p>Another thread operating on a different bin may continue:</p><pre>Bin 0 → Thread 1<br>Bin 1 → Thread 2<br>Bin 2 → Thread 3<br>Bin 3 → Thread 4</pre><p>Only threads that need conflicting access to the same protected bin may have to wait.</p><h4>What About concurrencyLevel?</h4><p>This is another detail worth mentioning.</p><p>Older explanations often say:</p><blockquote><em>“concurrencyLevel determines the number of segments.&quot;</em></blockquote><p>That is a <strong>Java 7-era explanation</strong>.</p><p>For modern implementations, you should not describe concurrencyLevel as controlling a fixed number of segments.</p><p>The current JDK API treats it as a <strong>sizing hint</strong>, retained largely for compatibility with older code.</p><p>So this:</p><pre>new ConcurrentHashMap&lt;&gt;(16, 0.75f, 16);</pre><p>should not be interpreted as:</p><pre>16 segments</pre><p>in modern Java.</p><h4>Collision Handling and Tree Bins</h4><p>Another useful diagram for the article is collision handling.</p><p>Normally, a bin may look like:</p><pre>            Bin<br>             |<br>Node A → Node B → Node C</pre><p>If a bin becomes heavily populated, the implementation can transform it into a tree structure:</p><pre>TreeBin<br>                <br>                B<br>              /   \<br>             A     C<br>                  / \<br>                 D   E</pre><p>Modern OpenJDK defines a treeification threshold of 8 nodes, although treeification also depends on the table’s capacity; if the table is too small, resizing can happen instead.</p><p>The purpose is to avoid poor lookup behavior caused by excessive collisions.</p><h4>What Happens During Resizing?</h4><p>Resizing is particularly interesting because ConcurrentHashMap doesn&#39;t simply stop the world and make every thread wait.</p><p>Conceptually:</p><pre>Old Table<br>+----+----+----+----+<br>| B0 | B1 | B2 | B3 |<br>+----+----+----+----+<br>          |<br>          | resize<br>          v<br><br>New Table<br>+----+----+----+----+----+----+----+----+<br>| B0 | B1 | B2 | B3 | B4 | B5 | B6 | B7 |<br>+----+----+----+----+----+----+----+----+</pre><p>Multiple threads can participate in the resizing work.</p><p>The OpenJDK implementation uses a transferIndex and allows threads to claim portions of the table to transfer, so resizing work can be shared rather than performed exclusively by one thread.</p><p>This is a nice example of the broader design philosophy:</p><blockquote><em>Don’t make one thread do all the work when other threads can safely help.</em></blockquote><h4>A Better Mental Model</h4><p>Instead of thinking:</p><pre>ConcurrentHashMap<br>       |<br>   Big Lock<br>       |<br>   HashMap</pre><p>think:</p><pre>                ConcurrentHashMap<br>                         |<br>             +-----------+-----------+<br>             |           |           |<br>            CAS       Volatile     Locks<br>             |           |           |<br>             +-----------+-----------+<br>                         |<br>                  Concurrent access</pre><p>And for updates:</p><pre>                Update<br>                   |<br>             Find the bin<br>                   |<br>        +----------+----------+<br>        |                     |<br>     Empty bin            Non-empty bin<br>        |                     |<br>       CAS              Bin-level locking<br>        |                     |<br>        +----------+----------+<br>                   |<br>              Safe update</pre><p>This mental model is much closer to how modern ConcurrentHashMap actually works.</p><h4>One More Important Point: Thread-Safe ≠ Everything Is Atomic</h4><p>This code:</p><pre>if (!map.containsKey(&quot;Java&quot;)) {<br>    map.put(&quot;Java&quot;, 1);<br>}</pre><p>uses thread-safe methods.</p><p>But the <strong>whole sequence is not atomic</strong>.</p><p>Two threads can observe the key as absent:</p><pre>Thread 1 → containsKey() → false<br>Thread 2 → containsKey() → false<br><br>Thread 1 → put()<br>Thread 2 → put()</pre><p>Instead, use:</p><pre>map.putIfAbsent(&quot;Java&quot;, 1);</pre><p>Similarly, for counters:</p><pre>map.merge(&quot;Java&quot;, 1, Integer::sum);</pre><p>This is an important distinction:</p><pre>Thread-safe operation<br>        ≠<br>Atomic multi-operation workflow</pre><p>ConcurrentHashMap provides atomic compound operations specifically to address this kind of requirement.</p><pre>                  ConcurrentHashMap<br>                         |<br>                         v<br>                  +-------------+<br>                  |    Table    |<br>                  +-------------+<br>                    |    |    |<br>                    v    v    v<br>                   Bin  Bin  Bin<br>                    |    |    |<br>                   CAS  CAS  Lock<br>                    |    |    |<br>                    +----+----+<br>                         |<br>                         v<br>                Thread-safe updates</pre><pre>                    Reads<br>                      |<br>                      v<br>               Usually non-blocking</pre><p>The key takeaway is that ConcurrentHashMap achieves scalability not by eliminating synchronization completely, but by minimizing the amount of synchronization required.</p><p><strong><em>That’s the fundamental idea behind its design.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b8ba924b222" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Microservices: When to Split a Monolith]]></title>
            <link>https://medium.com/@mohanakrishnakavali/microservices-when-to-split-a-monolith-64253fe3fa6a?source=rss-c4e567008fd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/64253fe3fa6a</guid>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[microservice-architecture]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Mohana Krishna Kavali]]></dc:creator>
            <pubDate>Sun, 19 Jul 2026 16:02:13 GMT</pubDate>
            <atom:updated>2026-07-19T16:09:56.693Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong><em>“Don’t start with microservices. Start with a well-designed monolith, then split only when your business and engineering needs demand it.”</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e47FV_9CNS_kijW1s53AIg.png" /></figure><blockquote>Many successful companies — including Amazon, Netflix, Shopify, and Uber — started with monolithic architectures. As their products, traffic, and engineering teams grew, they gradually decomposed their applications into microservices.</blockquote><p>A monolith is not a bad architecture. In fact, for many startups and small teams, it’s the fastest way to build and ship products.</p><p>The challenge begins when growth exposes bottlenecks.</p><h3>Monolith vs Microservices</h3><h3>Monolithic Architecture</h3><pre>Users<br>                      │<br>              ┌───────────────┐<br>              │   Monolithic  │<br>              │  Application  │<br>              ├───────────────┤<br>              │ Authentication│<br>              │ Orders        │<br>              │ Payments      │<br>              │ Inventory     │<br>              │ Notifications │<br>              └───────┬───────┘<br>                      │<br>                Single Database</pre><p>Everything is deployed together.</p><p>Even if you change one line of code in the Notification module, the <strong>entire application must be rebuilt and redeployed</strong>.</p><h4>Microservices Architecture</h4><pre>Users<br>                      │<br>                 API Gateway<br>      ┌────────┬────────┬────────┬────────┐<br>      │        │        │        │        │<br> Auth Service Orders  Payment Inventory Notification<br>      │        │        │        │        │<br>   Auth DB  Order DB Payment DB Stock DB Notify DB</pre><p>Each service:</p><ul><li>Owns its business logic</li><li>Has its own database</li><li>Can be deployed independently</li><li>Can scale independently</li></ul><h4>What Changes When You Split?</h4><p>Decomposing a monolith into microservices is an architectural trade-off.</p><p>You gain:</p><ul><li>Independent deployments</li><li>Better fault isolation</li><li>Targeted scalability</li><li>Team autonomy</li><li>Technology flexibility</li></ul><p>But you also inherit distributed system challenges:</p><ul><li>Network latency</li><li>Service discovery</li><li>API versioning</li><li>Distributed tracing</li><li>Monitoring</li><li>Eventual consistency</li><li>Retries and circuit breakers</li></ul><blockquote><strong>Microservices solve organizational scaling problems more than they solve coding problems.</strong></blockquote><h4>Real-Time Scenario 1: E-Commerce Platform</h4><p>Imagine you’re building an online shopping application.</p><p>Initially, everything runs inside one application.</p><pre>E-Commerce App<br>├── Login<br>├── Product Catalog<br>├── Orders<br>├── Payments<br>├── Inventory<br>└── Notifications</pre><p>Traffic:</p><ul><li>5,000 users/day</li><li>Team of 4 developers</li></ul><p>A monolith works perfectly.</p><p><strong><em>Six months later…</em></strong></p><p>Traffic grows to:</p><ul><li>3 million users/day</li><li>40 developers</li><li>Hundreds of deployments every month</li></ul><p>Now problems begin.</p><p>Whenever the Inventory team releases a feature:</p><ul><li>Order team must wait</li><li>Payment team waits</li><li>Notification team waits</li></ul><p>One deployment blocks everyone.</p><p>This isn’t a software problem.</p><p>It’s an organizational problem.</p><h3>Signals It’s Time to Split</h3><h4>1. Teams Are Growing</h4><p>Suppose your company has expanded.</p><pre>Monolith<br><br>40 Developers<br>       │<br>       ▼<br>Single Repository<br>       │<br>       ▼<br>Single Deployment Pipeline</pre><p>Problems:</p><ul><li>Frequent merge conflicts</li><li>Long build times</li><li>Release coordination meetings</li><li>One bug delays everyone</li></ul><p>Now compare with microservices.</p><pre>Authentication Team<br>        │<br>Deploy IndependentlyOrders Team<br>        │<br>Deploy Independently<br>        │<br>Inventory Team<br>        │<br>Deploy Independently<br>        │<br>Payments Team<br>        │<br>Deploy Independently<br></pre><p>Each team owns its service from development to production.</p><blockquote>This is why companies like Amazon introduced the concept of <strong>“You build it, you run it.”</strong></blockquote><h4>2. Different Parts Need Different Scaling</h4><p>Consider an online shopping application during <strong>Black Friday</strong>.</p><p>Daily traffic:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9nSvCsZDs46-BXR5qkPgDQ.png" /></figure><p>Inventory receives far more traffic.</p><p>With a monolith:</p><pre>Need more Inventory capacity?<br>Scale EVERYTHING.<br>Authentication ✔<br>Orders ✔<br>Payments ✔<br>Inventory ✔<br>Notifications ✔</pre><p>This wastes infrastructure.</p><p>With microservices:</p><pre>Authentication ×2<br>Orders ×3<br>Inventory ×15<br>Notifications ×1</pre><p>Each service scales according to its workload.</p><p>This significantly reduces infrastructure costs.</p><h4>3. Technology Constraints</h4><p>Suppose your application is written entirely in Java.</p><p>Now your business wants an AI-powered recommendation engine.</p><p>The data science team prefers Python because of libraries like:</p><ul><li>TensorFlow</li><li>PyTorch</li><li>Scikit-learn</li></ul><p>Instead of rewriting the entire application, you create:</p><pre>Java Monolith<br>      │<br> REST API<br>      │<br>Python Recommendation Service</pre><p>Only the recommendation engine uses Python.</p><p>Everything else continues running in Java.</p><p>This is a common adoption path in large enterprises.</p><h4>How to Decompose a Monolith</h4><p>One of the most effective approaches is <strong>Domain-Driven Design (DDD)</strong>.</p><p>Instead of splitting code by technical layers, identify <strong>business capabilities</strong>.</p><p>Example:</p><pre>E-Commerce<br>├── Customer Management<br>├── Product Catalog<br>├── Orders<br>├── Payments<br>├── Shipping<br>└── Notifications</pre><p>Each business capability naturally becomes a microservice.</p><p>Avoid this approach:</p><pre>Controllers Service<br>Repositories Service<br>Database Service</pre><p>These are technical boundaries, not business boundaries, and usually lead to tightly coupled services.</p><p>Instead, design around business domains.</p><h4>Real-Time Scenario 2: Food Delivery App</h4><p>Imagine a food delivery platform.</p><p>At lunch time:</p><ul><li>150,000 users browse restaurants.</li><li>40,000 users place orders.</li><li>5,000 users update profiles.</li></ul><p>The Restaurant service experiences far more load than the User Profile service.</p><p>With a monolith:</p><pre>Scale entire application</pre><p>With microservices:</p><pre>Restaurant Service → 20 instances<br>Order Service → 10 instances<br>Payment Service → 5 instances<br>Profile Service → 2 instances</pre><p>Resources are allocated where they’re actually needed.</p><h4>The Hidden Cost of Microservices</h4><p>Many articles focus only on the benefits. The reality is more complex.</p><p>Every new service requires:</p><ul><li>CI/CD pipeline</li><li>Monitoring</li><li>Logging</li><li>Metrics</li><li>Alerting</li><li>Secrets management</li><li>Security policies</li><li>Backup strategy</li><li>Database management</li></ul><p><strong>A system with 25 services often means managing:</strong></p><ul><li>25 deployments</li><li>25 health checks</li><li>25 dashboards</li><li>25 monitoring configurations</li></ul><blockquote><strong>Operational complexity grows quickly.</strong></blockquote><h4>Challenges We’ll Face</h4><p>What happens if:</p><ul><li>Inventory is slow?</li><li>Payment is unavailable?</li><li>Network latency spikes?</li><li>One service times out?</li></ul><p>Now you need patterns such as:</p><ul><li>Circuit Breaker</li><li>Retry</li><li>Timeout</li><li>Bulkhead</li><li>Distributed Tracing</li><li>Event-Driven Communication</li></ul><blockquote><strong>These are essential in distributed systems but unnecessary in many monolithic applications.</strong></blockquote><h4>Decision Checklist</h4><p>Before splitting your monolith, ask:</p><p>✅ Is the engineering team growing rapidly?</p><p>✅ Are deployments slowing development?</p><p>✅ Does one module need significantly more resources?</p><p>✅ Do different domains evolve independently?</p><p>✅ Do you have mature DevOps, monitoring, and observability practices?</p><blockquote><strong>If the answer to most of these is No, a monolith is likely still the better choice.</strong></blockquote><h4>Conclusion</h4><p>Microservices are not the next step after a monolith — they are a response to specific scaling challenges.</p><p>For startups and small teams, a well-structured monolith often delivers features faster with far less operational complexity.</p><p>As your organization grows, teams expand, and different parts of the system evolve at different speeds, decomposing the monolith into carefully designed microservices can unlock independent deployments, targeted scalability, and greater team autonomy.</p><blockquote><strong>The best architecture is not the one with the most services. It’s the one that solves today’s problems while remaining maintainable for tomorrow.</strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64253fe3fa6a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Retry Storms: How Well-Intentioned Retries Can Bring Down Your System]]></title>
            <link>https://medium.com/@mohanakrishnakavali/retry-storms-how-well-intentioned-retries-can-bring-down-your-system-39a85f2ba4b3?source=rss-c4e567008fd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/39a85f2ba4b3</guid>
            <category><![CDATA[design-systems]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Mohana Krishna Kavali]]></dc:creator>
            <pubDate>Thu, 16 Jul 2026 02:19:03 GMT</pubDate>
            <atom:updated>2026-07-16T02:19:03.744Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OQ5wbC0jFWkQS_inESNAxw.png" /></figure><p>Distributed systems are designed with failures in mind. Network timeouts, temporary service outages, and intermittent connectivity issues are expected. One of the most common techniques to improve reliability is <strong>retrying failed requests</strong>.</p><p>However, when we implemented retries without proper safeguards, they can unintentionally become the very reason a system fails. This phenomenon is known as a <strong>Retry Storm</strong>.</p><h3>What is a Retry Storm?</h3><p>A <strong>retry storm</strong> occurs when a large number of clients simultaneously retry failed requests against a service that is already under stress.</p><p>Instead of allowing the service to recover, the flood of retry traffic overwhelms it even further, transforming a temporary issue into a widespread outage.</p><p>Think of it like a traffic jam:</p><ul><li>A road is partially blocked.</li><li>More vehicles keep entering the same road.</li><li>Congestion worsens.</li><li>Recovery becomes increasingly difficult.</li></ul><p>The same thing happens with services during a retry storm.</p><h4>Key Characteristics</h4><p>Retry storms typically exhibit the following symptoms:</p><ul><li>Sudden spikes in request volume</li><li>Increased request latency</li><li>Higher error rates (5xx responses, timeouts, connection failures)</li><li>Cascading failures across dependent services</li><li>Longer recovery times even after the original issue is resolved</li></ul><h4>The Retry Storm Feedback Loop</h4><p>The most dangerous aspect of a retry storm is the <strong>positive feedback loop</strong> it creates.</p><p>The sequence usually looks like this:</p><ol><li>A service becomes slow or starts failing.</li><li>Client requests begin to fail.</li><li>Clients immediately retry those failed requests.</li><li>The additional retry traffic increases the service load.</li><li>The service degrades further.</li><li>Even more requests fail.</li><li>More retries are triggered.</li></ol><p>This cycle continues until the service becomes completely overwhelmed.</p><p><strong>The request flow will be : Service degrades → Requests fail → Clients retry immediately → Load increases → Service degrades further</strong></p><h4>Why Immediate Retries Are Dangerous</h4><p>Many applications retry requests instantly after receiving an error.</p><p>While this may seem harmless for a single client, imagine:</p><ul><li>10,000 clients</li><li>Each request fails</li><li>Every client retries immediately</li><li>Each retry also fails</li><li>Clients retry again</li></ul><p>Very quickly, the service begins receiving <strong>multiple times its normal traffic</strong>, making recovery nearly impossible.</p><p>Instead of reducing downtime, retries actually extend it.</p><h4>Example scenario</h4><p>Suppose your service normally handles:</p><ul><li><strong>1,000 requests per second</strong></li><li>Maximum capacity: <strong>1,200 requests per second</strong></li></ul><p>Now an issue causes 30% of requests to fail.</p><p>If every failed request is retried immediately:</p><ul><li>Original traffic = <strong>1,000 RPS</strong></li><li>Retry traffic = <strong>300 RPS</strong></li><li>Total traffic = <strong>1,300 RPS</strong></li></ul><p>The service is now operating beyond its capacity.</p><p>As more requests fail, retries continue to increase ….</p><h4>How to Mitigate Retry Storms</h4><p>The objective is simple:</p><blockquote><strong><em>Give the struggling service enough time to recover before sending another request.</em></strong></blockquote><p>This requires smarter retry strategies rather than immediate retries.</p><h4>Exponential Backoff</h4><p>The most effective mitigation technique is <a href="https://medium.com/@mohanakrishnakavali/exceptional-backoff-and-retry-strategy-in-spring-boot-0ee22a159fff"><strong>exponential backoff</strong>.</a></p><p>Instead of retrying immediately, each subsequent retry waits progressively longer. This significantly reduces the amount of retry traffic hitting the service.</p><h4>Add Jitter</h4><p>If every client uses the exact same exponential backoff, they may still retry simultaneously.</p><p>For example:</p><pre>Client A → Retry after 4 seconds<br>Client B → Retry after 4 seconds<br>Client C → Retry after 4 seconds</pre><p>Four seconds later…</p><p>All clients hit the service again at the exact same moment.</p><p>To avoid synchronized retries, introduce <strong>jitter</strong>, which adds a small random delay to each retry.</p><p>Instead of:</p><pre>Retry after 4 seconds</pre><p>Use:</p><pre>Retry after 3.4 seconds<br>Retry after 4.7 seconds<br>Retry after 5.2 seconds<br>Retry after 3.8 seconds</pre><p>This spreads requests over time, smoothing traffic spikes and improving the service’s chances of recovery.</p><h4>Immediate Retries vs. Exponential Backoff</h4><ul><li><strong>Immediate retries</strong> create sustained request volumes that exceed service capacity, prolonging the outage.</li><li><strong>Exponential backoff</strong> spreads retry traffic over time, allowing request rates to fall below service capacity so the system can recover.</li></ul><h4>Limit Retry Attempts</h4><p>Avoid infinite retry loops.</p><ul><li>Maximum of 3–5 retry attempts</li><li>Fail gracefully afterward</li></ul><h4>Retry Only Transient Failures</h4><p>Retry only when failures are likely to be temporary, such as:</p><ul><li>Network timeouts</li><li>HTTP 429 (Too Many Requests)</li><li>HTTP 503 (Service Unavailable)</li><li>Temporary connection failures</li></ul><p><strong>Avoid retrying permanent errors like:</strong></p><ul><li>HTTP 400 (Bad Request)</li><li>HTTP 401 (Unauthorized)</li><li>HTTP 403 (Forbidden)</li><li>HTTP 404 (Not Found)</li></ul><h4>Respect Retry-After Headers</h4><p>If a service returns a Retry-After header, clients should honor it instead of using their own retry schedule. This helps coordinate recovery and reduces unnecessary load.</p><h4>Use Circuit Breakers</h4><p>When failure rates become too high, a <a href="https://medium.com/@mohanakrishnakavali/circuit-breaker-pattern-in-microservices-7568100f9f44">circuit breaker</a> temporarily stops sending requests to the unhealthy service.</p><p>This prevents retries from overwhelming an already struggling system and gives it time to recover.</p><h4>Apply Rate Limiting</h4><p>Limit the number of retries generated by each client or application instance.</p><p>Even if many clients fail simultaneously, rate limiting prevents retry traffic from growing uncontrollably.</p><h4>Key Takeaways from this article</h4><p>A retry mechanism is an essential resilience pattern — but only when implemented thoughtfully.</p><p>A poorly designed retry strategy can:</p><ul><li>Amplify traffic during outages</li><li>Increase latency</li><li>Trigger cascading failures</li><li>Delay system recovery</li><li>Cause widespread service disruption</li></ul><p>A well-designed retry strategy should include:</p><ul><li>✅ Exponential backoff</li><li>✅ Random jitter</li><li>✅ Limited retry attempts</li><li>✅ Retry only transient failures</li><li>✅ Respect Retry-After headers</li><li>✅ Circuit breakers</li><li>✅ Rate limiting</li></ul><p>Retries are intended to make distributed systems more resilient, but without safeguards they can create a self-inflicted denial of service. By combining exponential backoff, jitter, retry limits, and complementary resilience patterns such as circuit breakers and rate limiting, you can prevent retry storms and help your services recover gracefully under failure conditions.</p><p><strong>A simple change in retry logic can make the difference between a brief service hiccup and a major production outage.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39a85f2ba4b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why System Design Still Matters in the AI Era]]></title>
            <link>https://medium.com/@mohanakrishnakavali/why-system-design-still-matters-in-the-ai-era-1dde7e50fbd9?source=rss-c4e567008fd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/1dde7e50fbd9</guid>
            <category><![CDATA[ai-and-design-system]]></category>
            <category><![CDATA[scalable-systems]]></category>
            <category><![CDATA[ai-engineering-skills]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[modern-dev-practices]]></category>
            <dc:creator><![CDATA[Mohana Krishna Kavali]]></dc:creator>
            <pubDate>Tue, 10 Feb 2026 01:51:39 GMT</pubDate>
            <atom:updated>2026-07-19T16:13:18.516Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f3pDG9M0uky-_g5vTQujHQ.png" /></figure><p>Every few months, a new AI tool drops and the same question comes up again:</p><blockquote>If AI can write code, explain frameworks, and scaffold entire projects — why should I still learn system design?</blockquote><p>It’s common question arises in our mind. And the honest answer for this is “<strong>system design matters more now than it ever did”</strong>.</p><p>I tried to break down this article <em>why</em> system design remains a critical skill in an AI-driven world, and how its role is quietly becoming more important, not less.</p><h4>AI Writes Code. Systems Still Need Decisions.</h4><p>If we observe, in our daily life we are using AI tools, which is very good at:</p><ul><li>Writing functions</li><li>Generating APIs</li><li>Refactoring codebases</li><li>Producing architecture diagrams on demand</li></ul><p>But system design is not about producing diagrams or code. System design is about <strong>making decisions under constraints</strong>:</p><ul><li>What components should exist?</li><li>How do they communicate?</li><li>Where does data live?</li><li>What happens when something fails?</li><li>How do we scale <em>without blowing up cost or complexity</em>?</li></ul><p>AI can propose answers, but it does not own the <strong>trade-offs</strong>:</p><ul><li>Performance <strong>vs</strong> cost</li><li>Speed <strong>vs</strong> correctness</li><li>Simplicity <strong>vs</strong> flexibility</li><li>Consistency <strong>vs</strong> availability</li></ul><p>Those trade-offs depend on business goals, user behavior, budgets, regulations, and timelines — context that AI does not truly understand. <strong>Humans still make those calls</strong>.</p><h4>AI Makes Bad Architecture Scale Faster</h4><p>One uncomfortable truth: <strong>AI makes it easier to build the wrong thing faster</strong>. When code was expensive, bad architecture took time to accumulate.</p><p>Now:</p><ul><li>AI can generate thousands of lines instantly</li><li>Teams move faster than ever</li><li>Mistakes propagate quickly across services</li></ul><p><strong>A poorly designed system today doesn’t just fail — it fails <em>at scale</em></strong>.</p><p>System design acts as a <strong>multiplier</strong>:</p><ul><li>Good design → AI accelerates success</li><li>Bad design → AI accelerates failure</li></ul><p>In the AI era, architecture becomes the bottleneck, not implementation.</p><h4>System Design Is Knowing What Not to Build</h4><p>This is where experienced engineers stand out.</p><p>Strong system designers know when:</p><ul><li>A monolith is better than microservices</li><li>A cron job beats an event pipeline</li><li>A single database beats Kafka + Redis + queues</li><li>“Boring” is the correct answer</li></ul><p>AI tends to over-engineer because it optimizes for completeness, not necessity. It doesn’t feel the pain of maintenance, debugging, or on-call rotations.</p><p>Humans with system design intuition keep systems <strong>simple, understandable, and resilient</strong> — the qualities that actually matter in production.</p><h4>The Career Ladder Is Shifting — System Design Is the Divider</h4><p>As AI handles more implementation, the engineering career ladder is quietly changing:</p><ul><li><strong>Junior engineers</strong> focus on “Can you write this?”</li><li><strong>Mid-level engineers</strong> focus on “Can you ship this?”</li><li><strong>Senior engineers</strong> focus on “Should we build this — and this way?”</li></ul><p><strong>System design:</strong></p><ul><li>Separates senior engineers from mid-level ones</li><li>Remains central to technical interviews</li><li>Is what leadership relies on for long-term decisions</li></ul><p>When AI can do the “how,” <strong>your value becomes the “why” and the “shape.”</strong></p><h4>System Design Makes You Better at Using AI</h4><p>Engineers who understand system design:</p><ul><li>Prompt AI more precisely</li><li>Define constraints clearly</li><li>Evaluate whether AI suggestions are sane</li><li>Spot hidden failure modes immediately</li></ul><p>Think of AI as a <strong>very fast junior engineer</strong>:</p><ul><li>Great at execution</li><li>Weak at context</li><li>Needs direction and review</li></ul><p>System design is how you <em>lead</em> that engineer.</p><p>Without it, you’re accepting suggestions blindly. With it, you’re orchestrating outcomes.</p><h4>AI Systems Themselves Need System Design</h4><p>If you’re building anything with AI, system design becomes unavoidable:</p><ul><li>Model serving <strong>vs</strong> batch inference</li><li>Latency <strong>vs</strong> cost trade-offs</li><li>Data pipelines and feedback loops</li><li>Caching, rate limits, and fallbacks</li><li>Monitoring, guardrails, and failure handling</li></ul><p>Prompt engineering alone doesn’t save us when:</p><ul><li>APIs time out</li><li>Costs spike unexpectedly</li><li>Models hallucinate in production</li><li>Dependencies go down</li></ul><p>These are classic system design problems — just with new components.</p><h4><strong>System Design Isn’t About Whiteboards Anymore</strong></h4><p>Learning system design today is not only about memorizing the classic designs. It’s about learning how to:</p><ul><li>Make good decisions with incomplete information</li><li>Design systems that tolerate failure</li><li>Keep complexity under control</li><li>Prevent AI from confidently building the wrong thing … etc.</li></ul><p>As code becomes commoditized, <strong>judgment becomes the skill</strong>.</p><h4>Final Thought</h4><p>In My view, AI is not replacing system design, It is <strong>removing everything else that distracted us from it</strong>.</p><p>In the AI era:</p><ul><li>Code is cheap</li><li>Architecture is expensive</li><li>Mistakes scale instantly</li><li>Good decisions compound</li></ul><blockquote>System design is no longer a “senior interview topic.” It’s the core engineering skill that keeps everything else from collapsing.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1dde7e50fbd9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Circuit Breaker Pattern in Microservices]]></title>
            <link>https://medium.com/@mohanakrishnakavali/circuit-breaker-pattern-in-microservices-7568100f9f44?source=rss-c4e567008fd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/7568100f9f44</guid>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[circuit-breaker]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Mohana Krishna Kavali]]></dc:creator>
            <pubDate>Tue, 25 Nov 2025 18:59:05 GMT</pubDate>
            <atom:updated>2025-11-25T19:00:22.405Z</atom:updated>
            <content:encoded><![CDATA[<p>Modern software is no longer a neatly packaged monolith running in a predictable environment. Instead, today’s distributed applications consist of dozens — or hundreds — of microservices, each communicating over unreliable networks, orchestrated by container platforms, and scaled dynamically in response to ever-changing workloads. In such environments, failure is not an incident — it is a <strong>constant expectation</strong>.</p><p>As systems scale, the challenge is not eliminating failures but <strong>containing</strong> them. And among all resilience mechanisms, the <strong>Circuit Breaker pattern</strong> remains one of the most powerful and foundational for achieving fault isolation, graceful degradation, and preventing cascading failures.</p><h4><strong>1. Why Distributed Systems Need Circuit Breakers</strong></h4><p>Microservices depend on networks, external systems, cloud APIs, databases, third-party services, and internal dependencies — any of which can fail. But failures are rarely clean or obvious.</p><p><strong>They often start subtly:</strong></p><ul><li>Latency slowly increases in a downstream service</li><li>A database becomes overloaded</li><li>A network partition appears between clusters</li><li>A third-party service hits a rate limit</li><li>A legacy SOAP endpoint becomes momentarily slow under high load</li></ul><p><strong>These issues frequently trigger a chain reaction:</strong></p><ol><li><strong>Service A</strong> calls <strong>Service B</strong>, which is slow.</li><li>Service A’s threads start waiting on timeouts.</li><li>Service A stops responding to upstream traffic.</li><li>Upstream services retry aggressively, adding even more load.</li><li>The entire microservice cluster experiences resource starvation.</li><li>The platform begins restarting pods, worsening the storm.</li><li>The outage spreads throughout the system.</li></ol><p>This is the classic cascading failure scenario, and it is one of the most common large-scale outage patterns in distributed systems.</p><p>Circuit Breakers play one crucial role in preventing these catastrophic scenarios: They protect the system from repeated, pointless attempts to call an unhealthy dependency.</p><h4><strong>2. Limitations of Retries (and Why Circuit Breakers Must Co-Exist)</strong></h4><p>Retries seem harmless — after all, many failures <em>are</em> transient. But in distributed systems, aggressive retry behavior becomes dangerous:</p><p><strong>Problem</strong> <strong>Impact</strong> Retry storms during outages Overloads downstream services Blind retries on slow endpoints Exponential latency for end users Thread and connection pool exhaustion System-wide unavailability Multi-layer retries (client, API gateway, service) Domino effect failures</p><p><strong>Retries alone cannot differentiate between:</strong></p><ul><li>A temporary glitch</li><li>A persistent failure</li><li>An overloaded downstream system</li><li>A network partition</li><li>A logical error vs infrastructure fault</li></ul><p>This is where the Circuit Breaker becomes essential.</p><h4><strong>3. Core Purpose and Philosophy of the Circuit Breaker Pattern</strong></h4><p>Circuit Breakers enforce <strong>failure boundaries</strong>.</p><p>They ensure that one malfunctioning component never takes down the entire application by:</p><ul><li>Detecting unhealthy downstream dependencies</li><li>Temporarily blocking calls to give the system time to recover</li><li>Providing fallback responses</li><li>Preventing resource exhaustion</li><li>Reducing latency during outages</li><li>Allowing controlled recovery instead of full load burst</li></ul><p><strong>In other words:</strong></p><p><strong><em>“Circuit Breakers help services fail fast, fail safely, and recover intelligently.”</em></strong></p><p>They allow microservices to remain <em>partially functional</em>, rather than failing catastrophically.</p><h4><strong>4. The Three States of the Circuit Breaker (Expanded Deep Dive)</strong></h4><p>Circuit Breakers operate as a <strong>state machine</strong>, transitioning between:</p><ul><li><strong>Closed</strong> → Normal behavior</li><li><strong>Open</strong> → Fail fast</li><li><strong>Half-Open</strong> → Controlled recovery</li></ul><p>Let’s examine them with architectural detail.</p><p><strong>4.1 Closed State (Normal Operation)</strong></p><p><strong>In the Closed state:</strong></p><ul><li>All requests flow to the downstream service.</li><li>The Circuit Breaker monitors:</li><li>Success vs failure ratio</li><li>Latency distribution</li><li>Timeout frequency</li><li>Exception types</li><li>Rate of slow calls</li></ul><p>The breaker stays in Closed state <strong>as long as behavior is stable and meets SLAs</strong>.</p><p><strong>What causes transition out of Closed state?</strong></p><p>We can define threshold rules such as:</p><ul><li>“If 50% of the last 20 requests fail, open the breaker.”</li><li>“If more than 10 timeouts occur in 10 seconds, open the breaker.”</li><li>“If 70% of calls exceed 2 seconds latency, open the breaker.”</li></ul><p>This protects the system early — when signals of instability emerge.</p><p><strong>4.2 Open State (Fail Fast Mode)</strong></p><p>When thresholds exceed limits, the breaker switches to <strong>Open state</strong>, meaning:</p><ul><li>No real calls are made to the downstream system</li><li>The service immediately returns fallback responses</li><li>Failure responses are extremely fast</li></ul><p><strong>This prevents:</strong></p><p>✓ Thread exhaustion<br> ✓ TCP connection storms<br> ✓ Unnecessary load on unhealthy services<br> ✓ Upstream services piling up retries</p><p>While the breaker is open, it enters a <strong>cooldown window</strong>, during which:</p><ul><li>The dependency is assumed unhealthy</li><li>No traffic is allowed through</li><li>Metrics continue collecting data</li><li>Alerting may trigger operations intervention</li></ul><p><strong>Why cooldown period is crucial</strong></p><p>If traffic is allowed too quickly, recovering systems become overloaded immediately and fail again. This “flapping” effect is common in poorly configured systems.</p><p><strong>4.3 Half-Open State (Controlled, Safe Recovery)</strong></p><p>After the cooldown period, the breaker transitions to <strong>Half-Open</strong>, allowing:</p><ul><li>A limited number of test requests</li><li>Controlled traffic, not full load</li><li>Collection of success rate data</li></ul><p><strong>Success → Closed State</strong></p><p>If test calls succeed consistently:</p><ul><li>The breaker is closed</li><li>Normal traffic resumes</li><li>Metrics reset</li></ul><p><strong>Failure → Open State</strong></p><p>If failures persist:</p><ul><li>The breaker reopens</li><li>Cooldown timer resets</li><li>Recovery is delayed further</li></ul><p><strong>Why Half-Open is one of the most critical states</strong></p><p>Without Half-Open state, the system would switch between Open and Closed too aggressively, causing oscillations and system instability.</p><h4><strong>5. When Architects Should Enforce Circuit Breakers</strong></h4><p>Not all calls need Circuit Breakers. But in enterprise architectures, they should be used for:</p><p><strong>5.1 External API Calls</strong></p><p>Anything outside your control:</p><ul><li>Payment gateways</li><li>Cloud provider APIs</li><li>SMS/email gateways</li><li>Third-party SaaS</li><li>Legacy on-prem APIs</li></ul><p>These have unpredictable latency and control.</p><p><strong>5.2 Inter-Microservice Calls (Internal Services)</strong></p><p>Especially:</p><ul><li>Services with variable load</li><li>High fan-out call graphs (one service calls many others)</li><li>Synchronous request–response workflows</li></ul><p>Circuit Breakers here prevent one microservice from taking down others.</p><p><strong>5.3 Data Layer Dependencies</strong></p><p>Yes — DB calls also need Circuit Breakers:</p><ul><li>Slow queries</li><li>Network partitions between app and DB</li><li>Connection pool exhaustion</li><li>Failover windows in cluster election</li></ul><p>A degraded database is one of the most common outage sources.</p><p><strong>5.4 Long-Running or Resource-Heavy Operations</strong></p><p>Use Circuit Breakers for:</p><ul><li>Large ML inference services</li><li>Media processing functions</li><li>Batch processing APIs</li><li>File conversion services</li></ul><p>These tend to fail under load spikes.</p><p><strong>5.5 Calls Over Unreliable Networks</strong></p><ul><li>Multi-AZ communication</li><li>Cross-region calls</li><li>Hybrid cloud connectivity</li><li>On-prem ↔ cloud integrations</li></ul><p>Latency and packet loss become major concerns here.</p><h4><strong>6. Key Design Decisions for Circuit Breakers</strong></h4><p>Configuring a Circuit Breaker is as important as using one.</p><p>Wrong configuration = more outages.</p><p>Let’s break down the major architectural decisions.</p><p><strong>6.1 Error Thresholds and Sliding Window Strategy</strong></p><p>Circuit Breakers typically monitor failures using:</p><ul><li><strong>Count-based sliding windows</strong> (e.g., last 100 requests)</li><li><strong>Time-based sliding windows</strong> (e.g., last 10 seconds)</li></ul><p><strong>You must choose:</strong></p><ul><li>Minimum request volume</li><li>Failure rate threshold</li><li>Time window for evaluation</li></ul><p><strong>Example Thresholds</strong></p><p>Setting Typical Value Minimum request count 20–50 Failure percentage 30–70% Slow call threshold &gt; 1–2 seconds Time window 10–60 seconds</p><p><strong>We can decide based on:</strong></p><ul><li>SLA requirements</li><li>Traffic patterns</li><li>Dependency reliability</li><li>Latency profile</li></ul><p>No single configuration works for all systems.</p><p><strong>6.2 Timeouts — The Most Misconfigured Component</strong></p><p>Timeouts define:</p><ul><li>How long a service waits before giving up</li><li>How quickly a system detects latency issues</li></ul><h3>Failure scenario: slow downstream service</h3><p>If Service B takes 20 seconds to respond:</p><ul><li>Service A waits 20 seconds</li><li>Threads get stuck</li><li>Thread pools fill</li><li>Service A becomes unavailable</li><li>Circuit Breaker reacts too late</li><li>The outage has already spread</li></ul><p><strong>Solution: fail fast with low timeouts.</strong></p><p>Architects typically recommend:</p><ul><li>Downstream service timeout = 200–500ms (for most internal calls)</li><li>External service timeout = 1–3 seconds</li><li>Always shorter than the upstream timeout</li></ul><p>Timeouts must be coordinated across service layers.</p><p><strong>6.3 Cooldown Duration (Open State Duration)</strong></p><p>The cooldown period (e.g., 20 seconds or 60 seconds) should reflect:</p><ul><li>How long downstream services usually take to recover</li><li>SLA expectations</li><li>Business tolerance for degraded behavior</li></ul><p>Too short → flapping<br> Too long → unnecessarily long degraded periods</p><p><strong>6.4 Fallback Strategies (Detailed)</strong></p><p>A fallback is how the system responds during Open state.</p><p><strong>We must define:</strong></p><ul><li>Business-safe fallback responses</li><li>Consistent behavior across services</li><li>Degradation levels</li></ul><p><strong>Types of fallbacks:</strong></p><p><strong>6.4.1. Stale/cached data : </strong>For read-heavy queries.</p><p><strong>6.4.2. Default behavior : </strong>E.g., shows limited UI options.</p><p><strong>6.4.3. Graceful error message : </strong>e.g., “Service temporarily unavailable.”</p><p><strong>6.4.4. Backup/secondary service : </strong>Load shifts to alternative providers.</p><p>Fallbacks must never violate business correctness or data consistency.</p><p><strong>6.5 Observability Requirements</strong></p><p>A Circuit Breaker without visibility is a silent failure.</p><p><strong>We must integrate:</strong></p><ul><li>Logs of state transitions</li><li>Metrics</li><li>Failure rates</li><li>Slow call rates</li><li>Timeout frequency</li><li>Half-Open success/failure count</li><li>Dashboards (Grafana / New Relic / CloudWatch / Dynatrace)</li><li>Alerts for: Open state too long, Repeated Half-Open failures and Spike in fallback invocations</li></ul><p>Observability makes Circuit Breakers operationally useful.</p><h4><strong>7. Circuit Breaker and Other Resilience Patterns</strong></h4><p>Circuit Breakers often coexist with related patterns.</p><p><strong>7.1 Circuit Breaker vs Retry</strong></p><p>Retry Circuit Breaker Helps with transient failures Stops calling unhealthy services Increases load if misused Protects downstream services Should be limited Should be mandatory</p><p><strong>Best practice:</strong></p><p><strong>Retry (small) → Circuit Breaker → Fallback</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zn3fxiVgH_nFQw_AUkJUGg.png" /></figure><p><strong>7.2 Circuit Breaker vs Bulkheads :</strong>Bulkheads isolate resources (threads, memory). Circuit Breakers isolate failing dependencies.</p><p>Both should be used together.</p><p><strong>7.3 Circuit Breaker vs Rate Limiter : </strong>Rate limiters protect your service.<br> Circuit Breakers protect downstream services.</p><p>Both complement each other.</p><h4><strong>8. Anti-Patterns and Common Mistakes (Extended)</strong></h4><p><strong>8.1. Configuring timeout longer than downstream SLA : </strong>This causes slow failures and system-wide latency inflation.</p><p><strong>8.2. Blind retries without exponential backoff : </strong>Creates retry storms during outages.</p><p><strong>8.3. Missing fallbacks : </strong>Failing fast without graceful degradation is still a failure.</p><p><strong>8.4. Applying Circuit Breakers everywhere : </strong>Some internal calls don’t need them (e.g., local caches).</p><p><strong>8.5. No governance across teams : </strong>Different configurations per team cause unpredictable behavior.</p><p><strong>8.6. Ignoring slow failures : </strong>Slow calls are more dangerous than outright failures.</p><p><strong>8.7. Overly sensitive thresholds : </strong>Cause unnecessary Open states (self-inflicted outages).</p><h4><strong>9. How Circuit Breakers Improve System Resilience</strong></h4><p>Circuit Breakers enable:</p><p><strong>9.1 Fault Containment : </strong>Failures stay localized; other services remain stable.</p><p><strong>9.2 Graceful Degradation : </strong>Users see partial functionality rather than errors.</p><p><strong>9.3 Stability Under Load : </strong>Prevents thread pool exhaustion and resource starvation.</p><p><strong>9.4 Predictable Fast Failure : </strong>Keeps system responsiveness even when dependencies fail.</p><p><strong>9.5 Controlled Recovery : </strong>Avoids overload bursts during service restoration.</p><p><strong>9.6 Improved Operational Visibility : </strong>Gives early warning signals for downstream failures.</p><h4><strong>10. Governance and Organizational Adoption</strong></h4><p>Large organizations require a standardized approach.</p><p><strong>10.1 Establish Default Policies</strong></p><p>Define corporate-wide defaults for:</p><ul><li>Thresholds</li><li>Timeout values</li><li>Cooldown windows</li><li>Fallback strategies</li><li>Retry policies</li><li>Alerting conditions</li></ul><p><strong>10.2 Provide Shared Libraries / Platform Services</strong></p><p>Teams should not reinvent Circuit Breaker configurations in each microservice.</p><p>Platform engineering teams should provide:</p><ul><li>Shared libraries (e.g., Resilience4j policy bundles)</li><li>API gateway-level policies</li><li>SRE-approved configuration templates</li><li>Dashboards and alerts out of the box</li></ul><p><strong>10.3 Organizational Training and Playbooks</strong></p><p><strong>Document:</strong></p><ul><li>How to configure a breaker</li><li>How to test failure scenarios</li><li>How to respond to repeated breaker openings</li></ul><p><strong>10.4 Chaos Engineering Integration</strong></p><p><strong>Inject failures intentionally:</strong></p><ul><li>Slow responses</li><li>Outages</li><li>Network latency</li><li>Throttling</li><li>Partial availability</li></ul><p><strong>To validate:</strong></p><ul><li>Breaker configurations</li><li>Fallback behavior</li><li>Recovery behavior</li><li>Alerting signals</li></ul><p>We should consider periodic chaos testing mandatory.</p><h4>11. Final Thoughts :</h4><p>The Circuit Breaker pattern is not just a technical configuration — it is a system-wide philosophy of <strong>resilience</strong>, <strong>fault isolation</strong>, and <strong>graceful degradation</strong>.</p><p>A well-designed Circuit Breaker strategy helps distributed systems:</p><ul><li>Contain failures</li><li>Prevent cascading outages</li><li>Maintain partial availability</li><li>Protect critical resources</li><li>Recover safely and predictably</li></ul><p><strong>At enterprise scale, Circuit Breakers must be:</strong></p><ul><li>Standardized</li><li>Governed</li><li>Observable</li><li>Continuously tested</li><li>Consistently tuned</li></ul><p>When We design and implemented thoughtfully, Circuit Breakers transform microservices from fragile, interconnected components into <strong>self-protecting, fault-tolerant systems capable of surviving real-world chaos</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7568100f9f44" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RAG vs. Fine-Tuning: Which One Should We Use?]]></title>
            <link>https://medium.com/@mohanakrishnakavali/rag-vs-fine-tuning-which-one-should-we-use-d95e0ecd13ab?source=rss-c4e567008fd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/d95e0ecd13ab</guid>
            <category><![CDATA[rags]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[fine-tuning]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <dc:creator><![CDATA[Mohana Krishna Kavali]]></dc:creator>
            <pubDate>Tue, 28 Oct 2025 23:34:25 GMT</pubDate>
            <atom:updated>2025-10-28T23:34:25.663Z</atom:updated>
            <content:encoded><![CDATA[<p>As businesses increasingly adopt <strong>Large Language Models (LLMs)</strong>, a common question arises:</p><blockquote><em>“How can we make these models understand </em>our<em> data and perform </em>our<em> specific tasks?”</em></blockquote><p>Two popular techniques dominate this space — <strong>Retrieval-Augmented Generation (RAG)</strong> and <strong>Fine-Tuning</strong>.<br> Both aim to tailor LLMs for specialized applications, but they work very differently and suit different needs.</p><h4>What Is Retrieval-Augmented Generation (RAG)?</h4><p>Retrieval-Augmented Generation (RAG) enhances a base language model by giving it access to <em>external knowledge sources</em> — such as documents, databases, or APIs — at query time.</p><p>Instead of training the model with new data, RAG allows it to retrieve relevant context dynamically and generate answers based on that context.</p><p><strong>How It Works:</strong></p><ol><li><strong>Retrieve:</strong> When a user asks a question, the system searches external documents (like PDFs, FAQs, or wikis) for relevant passages.</li><li><strong>Augment:</strong> These retrieved snippets are added to the model’s input prompt.</li><li><strong>Generate:</strong> The LLM uses both the question and retrieved context to produce an accurate, up-to-date response.</li></ol><p><strong>Example Use Case:</strong><br> A company uses RAG to build a chatbot that answers customer queries using real-time company documentation — without retraining the model.</p><p><strong>Advantages of RAG:</strong><br> ✅ Easy to update — just change the documents, not the model.<br> ✅ Keeps information current and dynamic.<br> ✅ Cost-effective — no expensive retraining required.<br> ✅ Reduces hallucination by grounding answers in real data.</p><p><strong>Limitations:</strong></p><ul><li>Relies heavily on retrieval quality.</li><li>May produce inconsistent tone or style.</li><li>Requires strong vector database and embedding setup (e.g., Pinecone, FAISS, Weaviate).</li></ul><h4>What Is Fine-Tuning?</h4><p><strong>Fine-tuning</strong> involves <strong>training the base LLM further</strong> using a curated dataset specific to your task or domain.<br> You essentially teach the model new behaviors or refine its responses through additional training cycles.</p><p><strong>How It Works:</strong></p><ol><li>Prepare domain-specific or task-specific training data.</li><li>Train the model (or adapter layers) on this data.</li><li>Deploy the fine-tuned version for specialized use.</li></ol><p><strong>Example Use Case:</strong><br> A legal tech company fine-tunes a model on thousands of legal documents to make it better at summarizing case law and drafting contracts.</p><p><strong>Advantages of Fine-Tuning:</strong><br> ✅ Produces highly domain-specific expertise.<br> ✅ Consistent style, tone, and behavior.<br> ✅ Faster inference — no retrieval step needed.<br> ✅ Works well for structured tasks (classification, summarization, etc.).</p><p><strong>Limitations:</strong></p><ul><li>Time-consuming and costly.</li><li>Needs large, high-quality datasets.</li><li>Risk of outdated information — the model’s knowledge is fixed at training time.</li><li>More complex to manage and update than RAG.</li></ul><h4>RAG vs. Fine-Tuning: Head-to-Head Comparison</h4><p>Feature / Criteria RAG Fine-Tuning Approach Adds external knowledge dynamically Modifies the model’s internal weights Data Updates Simple — update documents Requires retraining Cost &amp; Complexity Lower Higher Accuracy for Specific Tasks Depends on retrieval quality High (if fine-tuned well) Maintenance Easy Ongoing effort Response Consistency Variable Very consistent Best For Knowledge retrieval, FAQs, dynamic content Specialized tone, workflow automation, domain expertise</p><h4>When to Use RAG</h4><p>Use <strong>RAG</strong> if:</p><ul><li><strong>You need up-to-date answers from constantly changing data.</strong></li><li><strong>Your application deals with large document repositories (e.g., enterprise knowledge bases).</strong></li><li><strong>You want a low-cost and low-maintenance way to improve model performance.</strong></li></ul><p><strong>Ideal for:</strong><br> Customer support bots, internal knowledge assistants, document search tools, and research summarization systems.</p><h4>When to Use Fine-Tuning</h4><p>Choose <strong>Fine-Tuning</strong> if:</p><ul><li>You need a custom model behavior (tone, structure, or task-specific logic).</li><li>You have stable, high-quality data for your domain.</li><li>Latency is important (no need for retrieval).</li></ul><p><strong>Ideal for:</strong><br> Legal, healthcare, or financial applications, content generation with consistent tone, and task automation (e.g., classification, summarization).</p><h4>Best of Both Worlds: Hybrid Approach</h4><p>Many modern AI solutions combine both techniques.<br> For example:</p><ul><li>Use RAG for retrieving the latest company policies or news.</li><li>Use Fine-Tuning to ensure the model responds in your brand’s tone and format.</li></ul><p>This hybrid method offers fresh knowledge with customized behavior — the best of both worlds.</p><h4>Conclusion</h4><p>Choosing between <strong>RAG</strong> and <strong>Fine-Tuning</strong> depends on your goals:</p><ul><li>Pick <strong>RAG</strong> when agility, freshness, and cost-efficiency matter most.</li><li>Choose <strong>Fine-Tuning</strong> when accuracy, consistency, and specialization are key.</li><li>Or, blend both for maximum flexibility and performance.</li></ul><p>In the evolving world of AI, it’s not about which method is “better” — it’s about <strong>which one fits your use case best</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d95e0ecd13ab" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How will you handle scaling in and scaling out to balance user demand?]]></title>
            <link>https://medium.com/@mohanakrishnakavali/how-will-you-handle-scaling-in-and-scaling-out-to-balance-user-demand-0459e09796c6?source=rss-c4e567008fd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/0459e09796c6</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[gcp]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Mohana Krishna Kavali]]></dc:creator>
            <pubDate>Tue, 28 Oct 2025 23:23:13 GMT</pubDate>
            <atom:updated>2025-10-28T23:23:13.255Z</atom:updated>
            <content:encoded><![CDATA[<p>In today’s digital era, user traffic can change in seconds — a viral post, a flash sale, or a global event can push systems to their limits. To ensure applications remain responsive and cost-efficient, cloud environments rely on <strong>scaling in</strong> and <strong>scaling out</strong>.</p><p>Mastering these techniques helps organizations maintain a seamless user experience without overspending on idle infrastructure.</p><h4>🧩 Understanding Scaling In and Scaling Out</h4><h4>Scaling Out (Horizontal Scaling)</h4><p>Scaling out means adding more instances or nodes to distribute increasing workloads. It’s the go-to approach for handling traffic spikes or growing user bases.</p><p><strong>Benefits:</strong></p><ul><li>Increases capacity and fault tolerance</li><li>Supports elasticity — resources grow with demand</li><li>Perfect for microservices and distributed architectures</li></ul><p><strong>Example:</strong><br> During a Black Friday sale, an e-commerce platform automatically adds more web and database instances to handle thousands of concurrent shoppers.</p><h4>Scaling In (Horizontal Shrinking)</h4><p>Scaling in is the process of <strong>removing instances</strong> when demand decreases. It’s the key to optimizing costs without compromising service quality.</p><p><strong>Benefits:</strong></p><ul><li>Reduces unnecessary cloud expenses</li><li>Lowers idle resource consumption.</li><li>Simplifies monitoring and management.</li></ul><p><strong>Example:</strong><br> After peak hours, the same e-commerce platform scales back its instances to match reduced traffic, ensuring no resource wastage.</p><h4>⚙️ Strategies to Balance User Demand</h4><h3>1. Implement Smart Auto-Scaling Policies</h3><p>Use your cloud provider’s native tools — such as AWS Auto Scaling, Azure VM Scale Sets, or Google Cloud Autoscaler — to automatically adjust capacity based on real-time performance metrics like:</p><ul><li>CPU or memory utilization</li><li>Network I/O</li><li>Queue length or request latency.</li></ul><p><strong>Pro Tip:</strong><br> Set minimum and maximum limits to prevent sudden cost spikes or resource shortages.</p><h4>2. Monitor and Predict Demand Patterns</h4><p>Effective scaling depends on knowing <em>when</em> to scale. Use monitoring tools like <strong>Amazon CloudWatch</strong>, <strong>Datadog</strong>, or <strong>Prometheus</strong> to analyze usage patterns.</p><ul><li>Identify daily and seasonal traffic peaks.</li><li>Apply <strong>predictive scaling</strong> to proactively add capacity before spikes occur.</li><li>Continuously review logs and metrics to refine thresholds.</li></ul><p><strong>Example:</strong><br> Netflix uses predictive auto-scaling to allocate resources based on viewing trends — automatically adjusting server capacity during prime-time hours.</p><h4>3. Use Load Balancers for Smooth Traffic Distribution</h4><p>Load balancers (like AWS Elastic Load Balancer or NGINX) ensure incoming traffic is evenly distributed across all active instances.</p><p><strong>Benefits:</strong></p><ul><li>Prevents overload on single instances</li><li>Enables zero-downtime scaling</li><li>Supports automatic registration of new instances during scale-out events.</li></ul><h4>4. Design for Scalability from the Start</h4><p>Your application architecture should be <strong>stateless</strong> — meaning no single instance stores user session data or critical state information.</p><p><strong>Best Practices:</strong></p><ul><li>Use shared data stores (e.g., <strong>Amazon S3</strong>, <strong>Google Cloud Storage</strong>) for persistent data.</li><li>Employ distributed caches (<strong>Redis</strong>, <strong>Memcached</strong>) for session management.</li><li>Break monolithic applications into smaller, independently scalable microservices.</li></ul><h4>5. Balance Cost, Performance, and Efficiency</h4><p>Scaling should enhance performance <em>without</em> draining your budget.</p><p>Tips for Cost-Effective Scaling:</p><ul><li>Use Reserved or Spot Instances for predictable workloads.</li><li>Adopt serverless services (like AWS Lambda or Azure Functions) for variable demand — pay only when code runs.</li><li>Regularly audit your auto-scaling policies to ensure they align with both cost and performance goals.</li></ul><h4>6. Automate Everything</h4><p>Automation is the backbone of reliable scaling. Use <strong>Infrastructure as Code (IaC)</strong> tools like <strong>Terraform</strong>, <strong>AWS CloudFormation</strong>, or <strong>Pulumi</strong> to define scaling policies, environment configurations, and deployments.</p><p>This ensures consistency, repeatability, and faster recovery during failures or rapid scale-ups.</p><h4>🚀 Emerging Trends: Predictive and AI-Driven Scaling</h4><p>Traditional auto-scaling reacts to metrics — but <strong>AI-driven scaling</strong> predicts demand before it happens.</p><p>Modern systems use machine learning models to analyze traffic history, weather patterns, and event calendars to scale resources <em>ahead of time</em>.<br> This approach reduces latency, avoids downtime, and optimizes cost even further.</p><p><strong>Example:</strong><br> Spotify employs machine learning models to forecast streaming demand during global music releases, ensuring users enjoy a lag-free experience.</p><h4>🧠 Conclusion</h4><p>Balancing scaling in and scaling out isn’t just about technology — it’s about strategy. By combining auto-scaling policies, predictive analytics, load balancing, and automation, you can deliver exceptional user experiences while keeping infrastructure costs under control.</p><p>In the cloud, scaling is more than a performance metric — it’s a business enabler. Organizations that scale intelligently not only stay agile but also gain a competitive edge in reliability, cost-efficiency, and innovation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0459e09796c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Balancing Scalability and Security in Cloud Computing]]></title>
            <link>https://medium.com/@mohanakrishnakavali/balancing-scalability-and-security-in-cloud-computing-e68e13977af2?source=rss-c4e567008fd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/e68e13977af2</guid>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Mohana Krishna Kavali]]></dc:creator>
            <pubDate>Tue, 28 Oct 2025 23:12:01 GMT</pubDate>
            <atom:updated>2025-10-28T23:12:01.389Z</atom:updated>
            <content:encoded><![CDATA[<p>Balancing scalability and security is one of the most critical challenges in modern cloud computing. While cloud platforms provide immense flexibility, agility, and scalability, they also introduce new security risks that organizations must manage carefully.</p><p>Below are some key Principles to ensure your cloud environment remains <strong>both secure and scalable</strong>.</p><h4>Core Principles</h4><h4>1. Robust Identity and Access Management (IAM)</h4><ul><li><strong>Least Privilege Principle:</strong> Grant users only the minimum permissions necessary to perform their tasks.</li><li><strong>Strong Password Policies:</strong> Enforce strong password policies and enable multi-factor authentication (MFA).</li><li><strong>Regular Access Reviews:</strong> Conduct periodic reviews to identify and revoke unnecessary or outdated access rights.</li></ul><h4>2. Data Encryption</h4><ul><li><strong>Encryption at Rest:</strong> Encrypt data stored on disks, databases, and storage buckets.</li><li><strong>Encryption in Transit:</strong> Use HTTPS and secure transport protocols to protect data in motion.</li><li><strong>Key Management:</strong> Implement robust key management practices, including regular rotation and secure storage of encryption keys.</li></ul><h4>3. Network Security</h4><ul><li><strong>Network Segmentation:</strong> Isolate sensitive workloads and data within separate network segments.</li><li><strong>Firewall Rules:</strong> Configure firewalls to restrict unnecessary inbound and outbound traffic.</li><li><strong>Intrusion Detection and Prevention Systems (IDPS):</strong> Monitor network traffic for suspicious or malicious activity.</li><li><strong>Web Application Firewalls (WAFs):</strong> Protect web applications from attacks such as SQL injection and cross-site scripting (XSS).</li></ul><h4>4. Regular Security Audits and Penetration Testing</h4><ul><li><strong>Identify Vulnerabilities:</strong> Conduct frequent security assessments and penetration tests to discover weaknesses.</li><li><strong>Patch Management:</strong> Keep operating systems, software, and cloud services updated with the latest security patches.</li><li><strong>Incident Response Plan:</strong> Develop and maintain a clear, tested plan for responding to security incidents.</li></ul><h4>5. Cloud Provider Security Features</h4><ul><li><strong>Leverage Built-in Security Tools:</strong> Take advantage of your cloud provider’s native tools for firewall management, encryption, and access control.</li><li><strong>Monitor Security Alerts:</strong> Stay informed about potential threats and act promptly.</li><li><strong>Data Classification:</strong> Categorize data based on sensitivity.</li><li><strong>DLP Tools:</strong> Use DLP solutions to monitor, control, and prevent unauthorized data movement.</li></ul><h4>6. Security Awareness Training</h4><p>Educate employees about best practices in cloud security, including:</p><ul><li>Recognizing phishing attempts and social engineering tactics.</li><li>Safely handling sensitive data.</li><li>Reporting suspicious activities promptly.</li></ul><p>Conduct regular refresher sessions to reinforce secure behavior across the organization.</p><h4>Achieving Scalability with Security</h4><p>To balance scalability and security effectively, organizations must embed security practices into their cloud architecture from the start.</p><ul><li><strong>Scalable Security Infrastructure:</strong> Deploy security solutions that grow with your workloads.</li><li><strong>Automation:</strong> Automate routine security tasks (e.g., compliance checks, patching, access provisioning) to reduce human error and improve efficiency.</li><li><strong>Continuous Monitoring:</strong> Use Security Information and Event Management (SIEM) systems to detect, analyze, and respond to threats in real time.</li><li><strong>Regular Security Assessments:</strong> Reassess security controls regularly to ensure they evolve alongside your scaling environment.</li></ul><h4>Conclusion</h4><p>By combining these strategies, organizations can achieve the ideal balance between <strong>cloud scalability and security</strong>. Remember: security is not a one-time effort but an ongoing process. Staying updated on the latest threats, technologies, and best practices is essential for maintaining a resilient and future-ready cloud infrastructure.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e68e13977af2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Taming the Memory Monster with Spring Boot: CSV to DB Bulk Processing Without Crashing]]></title>
            <link>https://medium.com/@mohanakrishnakavali/taming-the-memory-monster-with-spring-boot-csv-to-db-bulk-processing-without-crashing-bf72fec0ac0e?source=rss-c4e567008fd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf72fec0ac0e</guid>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[jvm-memory]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[spring-batch]]></category>
            <category><![CDATA[rest-api]]></category>
            <dc:creator><![CDATA[Mohana Krishna Kavali]]></dc:creator>
            <pubDate>Sun, 26 Oct 2025 23:39:21 GMT</pubDate>
            <atom:updated>2025-10-26T23:39:21.650Z</atom:updated>
            <content:encoded><![CDATA[<p>The java.lang.OutOfMemoryError is a nightmare for developers, especially when dealing with high-volume tasks like importing massive CSV files into a database. In a <strong>Spring Boot</strong> environment, the temptation is to use simple @Service methods to read all data at once.</p><p>However, the Spring framework provides robust, memory-efficient tools perfectly suited for this task: the <strong>Spring Batch</strong> framework and optimized <strong>data streaming</strong> patterns.</p><p>This happens because your application is trying to <strong>load too much data into memory at once</strong>.<br> In projects — especially when dealing with millions of records — this can crash your service or make your system unstable.</p><p>In this article, we’ll explore <strong>how to process large CSV files safely in Spring Boot</strong>, without running out of memory, using <strong>streaming, batching, and optimized transactions</strong>.</p><h4>🧩 Why This Happens</h4><p>When you read a CSV file like this:</p><pre>List&lt;String[]&gt; records = new CSVReader(new FileReader(&quot;data.csv&quot;)).readAll();</pre><p>We’re loading <strong>every record into memory</strong>.<br> If the file has 1 million rows, you now have 1 million objects sitting in RAM — before a single insert happens!</p><p>This easily triggers:</p><ul><li>OutOfMemoryError: Java heap space</li><li>Slow performance</li><li>GC (Garbage Collection) overhead</li><li>Database timeouts due to massive transactions</li></ul><h4>⚙️ The Right Approach — Stream + Batch</h4><p>Instead of reading the entire file, we’ll <strong>process it in chunks</strong>.</p><h4>✅ Step 1. Stream the CSV Instead of Loading All at Once</h4><p>Using <strong>OpenCSV</strong> or <strong>Apache Commons CSV</strong>, we can read the file line by line.</p><pre>try (CSVReader reader = new CSVReaderBuilder(new FileReader(&quot;data.csv&quot;))<br>        .withSkipLines(1)<br>        .build()) {<br>    <br>    String[] line;<br>    while ((line = reader.readNext()) != null) {<br>        processRecord(line);<br>    }<br>}</pre><p>This way, only one line stays in memory at a time — no bulk loading.</p><h4>✅ Step 2. Batch Database Inserts</h4><p>Instead of saving each record individually, <strong>use batching</strong> to reduce database I/O.</p><p>Example using JPA and EntityManager:</p><pre>@Autowired<br>private EntityManager entityManager;<br>@Transactional<br>public void importDataInBatches(List&lt;Book&gt; books) {<br>    int batchSize = 1000;<br>    for (int i = 0; i &lt; books.size(); i++) {<br>        entityManager.persist(books.get(i));<br>        if (i % batchSize == 0) {<br>            entityManager.flush();<br>            entityManager.clear();<br>        }<br>    }<br>}</pre><p><strong>What this does:</strong></p><ul><li>Inserts 1000 records per transaction.</li><li>Flushes and clears the persistence context after each batch.</li><li>Prevents memory buildup from managed entities.</li></ul><h4>✅ Step 3. Stream + Batch Together</h4><p>Combine both ideas:</p><pre>public void importLargeCsv(String filePath) throws IOException {<br>    int batchSize = 1000;<br>    List&lt;Book&gt; batch = new ArrayList&lt;&gt;();<br>try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {<br>        String line;<br>        br.readLine(); // skip header<br>        while ((line = br.readLine()) != null) {<br>            String[] data = line.split(&quot;,&quot;);<br>            Book book = new Book(data[0], data[1], Integer.parseInt(data[2]));<br>            batch.add(book);<br>            if (batch.size() == batchSize) {<br>                importDataInBatches(batch);<br>                batch.clear();<br>            }<br>        }<br>        // handle remaining records<br>        if (!batch.isEmpty()) {<br>            importDataInBatches(batch);<br>        }<br>    }<br>}</pre><p>This pattern is extremely memory-efficient — you only hold a small list in memory at a time.</p><h4>✅ Step 4. Tune Your JVM</h4><p>If you still face memory issues, tweak your JVM settings to allocate more heap space (as a temporary fix):</p><pre>java -Xms512m -Xmx2048m -jar app.jar</pre><blockquote><em>But remember — increasing heap is a </em><strong><em>band-aid</em></strong><em>, not a real solution.<br> Streaming and batching are the sustainable fixes.</em></blockquote><h4>🚀 Bonus: Use Spring Batch (If You Want a Framework-Based Solution)</h4><p>For <strong>large-scale enterprise imports</strong>, Spring provides <strong>Spring Batch</strong>, which:</p><ul><li>Automatically handles chunk-based processing.</li><li>Supports restart/resume after failure.</li><li>Uses a minimal memory footprint.</li></ul><p><strong>Example snippet:</strong></p><pre>@Bean<br>public Step csvToDbStep() {<br>    return stepBuilderFactory.get(&quot;csvToDbStep&quot;)<br>            .&lt;Book, Book&gt;chunk(1000)<br>            .reader(csvItemReader())<br>            .processor(csvItemProcessor())<br>            .writer(jdbcItemWriter())<br>            .build();<br>}</pre><p>If you’re processing <strong>millions of records daily</strong>, Spring Batch is your best friend.</p><h4>🧾 Conclusion</h4><p>When dealing with bulk data, avoid loading everything into memory at once. Instead, process it in manageable chunks to maintain stability and performance.</p><p><strong>Instead:</strong></p><ol><li>Stream the file line by line.</li><li>Batch your inserts.</li><li>Flush and clear JPA sessions frequently.</li><li>Scale up with Spring Batch for heavy workloads.</li></ol><p>These optimizations help you process bulk CSV data efficiently while minimizing memory usage and keeping your Spring Boot app stable under load</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf72fec0ac0e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Set Up Different Logging Levels for Different Environments in Spring Boot]]></title>
            <link>https://medium.com/@mohanakrishnakavali/how-to-set-up-different-logging-levels-for-different-environments-in-spring-boot-d094ebfaced1?source=rss-c4e567008fd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/d094ebfaced1</guid>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[logging]]></category>
            <category><![CDATA[spring-framework]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Mohana Krishna Kavali]]></dc:creator>
            <pubDate>Sat, 25 Oct 2025 20:49:24 GMT</pubDate>
            <atom:updated>2025-10-25T20:49:24.812Z</atom:updated>
            <content:encoded><![CDATA[<p>Logging is one of those things we don’t appreciate until something breaks in production. When it comes to Spring Boot applications, logging gives you a window into what’s happening inside your app — but not all environments need the same level of detail.</p><p><strong>For example:</strong></p><ul><li>In <strong>development</strong>, you want detailed DEBUG logs to trace behavior.</li><li>In <strong>production</strong>, you only want ERROR or WARN logs to avoid noise and bloated files.</li></ul><p>So, how do we configure different logging levels for each environment cleanly?<br> Let’s walk through the most effective ways to do it.</p><h4>🧩 Why This Matters</h4><p>Many developers run into one of these issues:</p><ul><li>Too much noise in production logs (GBs of unnecessary data).</li><li>Not enough detail in development or test logs.</li><li>Manual editing of application.properties before deployment (risky and error-prone).</li></ul><p><strong>The good news?</strong><br> Spring Boot makes this incredibly easy using <strong>profiles</strong> and <strong>logback</strong>.</p><h4>⚙️ Approach 1 — Using application-*.yml Files</h4><p>Spring Boot allows you to define environment-specific configurations in separate property files.<br> For example, you can create application-dev.yml for development and application-prod.yml for production.</p><p><strong>📁 Directory structure:</strong></p><pre>src/<br> └── main/<br>      └── resources/<br>          ├── application.yml<br>          ├── application-dev.yml<br>          └── application-prod.yml</pre><p><strong>application-dev.yml</strong></p><pre>logging:<br>  level:<br>    root: DEBUG<br>    org.springframework.web: INFO<br>    com.example: DEBUG</pre><p><strong>application-prod.yml</strong></p><pre>logging:<br>  level:<br>    root: ERROR<br>    org.springframework.web: WARN<br>    com.example: INFO</pre><p>Spring Boot automatically loads the right file when you set the active profile.</p><p><strong>Activate a profile:</strong></p><pre>java -jar app.jar --spring.profiles.active=prod</pre><blockquote><em>✅ </em><strong><em>Tip:</em></strong><em> You can also set </em><em>spring.profiles.active=dev in your IDE’s run configuration or environment variables.</em></blockquote><h4>🧠 Approach 2 — Using logback-spring.xml</h4><p>If you need more flexibility, like different appenders or log formats, use <strong>Logback</strong>, the default logging framework in Spring Boot.</p><blockquote><em>Always name it </em><em>logback-spring.xml, not just </em><em>logback.xml — this allows Spring to interpret </em><em>&lt;springProfile&gt; tags.</em></blockquote><p><strong>Example </strong><strong>logback-spring.xml:</strong></p><pre>&lt;configuration&gt;<br>    &lt;!-- Development environment --&gt;<br>    &lt;springProfile name=&quot;dev&quot;&gt;<br>        &lt;logger name=&quot;com.example&quot; level=&quot;DEBUG&quot;/&gt;<br>        &lt;root level=&quot;INFO&quot;&gt;<br>            &lt;appender-ref ref=&quot;CONSOLE&quot;/&gt;<br>        &lt;/root&gt;<br>    &lt;/springProfile&gt;<br>&lt;!-- Testing environment --&gt;<br>    &lt;springProfile name=&quot;test&quot;&gt;<br>        &lt;logger name=&quot;com.example&quot; level=&quot;INFO&quot;/&gt;<br>        &lt;root level=&quot;WARN&quot;&gt;<br>            &lt;appender-ref ref=&quot;CONSOLE&quot;/&gt;<br>        &lt;/root&gt;<br>    &lt;/springProfile&gt;<br>    &lt;!-- Production environment --&gt;<br>    &lt;springProfile name=&quot;prod&quot;&gt;<br>        &lt;logger name=&quot;com.example&quot; level=&quot;ERROR&quot;/&gt;<br>        &lt;root level=&quot;ERROR&quot;&gt;<br>            &lt;appender-ref ref=&quot;FILE&quot;/&gt;<br>        &lt;/root&gt;<br>    &lt;/springProfile&gt;<br>    &lt;!-- Appenders --&gt;<br>    &lt;appender name=&quot;CONSOLE&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;<br>        &lt;encoder&gt;<br>            &lt;pattern&gt;%d{HH:mm:ss} %-5level [%thread] %logger{36} - %msg%n&lt;/pattern&gt;<br>        &lt;/encoder&gt;<br>    &lt;/appender&gt;<br>    &lt;appender name=&quot;FILE&quot; class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;<br>        &lt;file&gt;logs/app.log&lt;/file&gt;<br>        &lt;rollingPolicy class=&quot;ch.qos.logback.core.rolling.TimeBasedRollingPolicy&quot;&gt;<br>            &lt;fileNamePattern&gt;logs/app-%d{yyyy-MM-dd}.log&lt;/fileNamePattern&gt;<br>            &lt;maxHistory&gt;7&lt;/maxHistory&gt;<br>        &lt;/rollingPolicy&gt;<br>        &lt;encoder&gt;<br>            &lt;pattern&gt;%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n&lt;/pattern&gt;<br>        &lt;/encoder&gt;<br>    &lt;/appender&gt;<br>&lt;/configuration&gt;</pre><p><strong>This setup:</strong></p><ul><li>Prints <strong>colorful logs</strong> to console in dev.</li><li>Writes logs to rotating files in production.</li><li>Uses different log levels automatically based on spring.profiles.active.</li></ul><h4>🧪 Testing It Out</h4><p>Let’s test different environments.</p><pre># Dev environment<br>java -jar app.jar --spring.profiles.active=dev<br># Logs<br>2025-10-25 10:00:12 DEBUG [BookService] - Fetching all books<br># Prod environment<br>java -jar app.jar --spring.profiles.active=prod<br># Logs<br>2025-10-25 10:00:12 ERROR [BookService] - Failed to connect to database</pre><p>Instantly, we’ll notice that only the relevant logs appear for each environment.</p><h4>🚀 Some useful information for Production</h4><ul><li><strong>Log rotation:</strong> Avoid huge log files — rotate daily or by size.</li><li><strong>External logging tools:</strong> Forward logs to ELK, Loki, or CloudWatch.</li><li><strong>Structured logs:</strong> Use JSON layout for better parsing.</li><li><strong>Mask sensitive info:</strong> Avoid logging passwords or tokens.</li></ul><h4>🧾 Conclusion</h4><p>Configuring different logging levels for each environment keeps your application clean, efficient, and production-ready.</p><p>With Spring Boot, you can:</p><ul><li>Use simple YAML configs for quick setups, or</li><li>Go full power with logback-spring.xml for advanced customization.</li></ul><p>Next time when deploy, we’ll have the right logs — just enough to debug, but never too noisy to drown in.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d094ebfaced1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>