<?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 Harish Balakrishnar on Medium]]></title>
        <description><![CDATA[Stories by Harish Balakrishnar on Medium]]></description>
        <link>https://medium.com/@bharishx?source=rss-0311e3ceba7a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GCuFYD_u1vI1J3puKi6ifQ.jpeg</url>
            <title>Stories by Harish Balakrishnar on Medium</title>
            <link>https://medium.com/@bharishx?source=rss-0311e3ceba7a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:49:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bharishx/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[Docker internals explained]]></title>
            <link>https://medium.com/@bharishx/docker-internals-explained-03d2251244e4?source=rss-0311e3ceba7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/03d2251244e4</guid>
            <category><![CDATA[technical-deep-dive]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[linux-containers]]></category>
            <category><![CDATA[linux-commands]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Harish Balakrishnar]]></dc:creator>
            <pubDate>Sun, 09 Nov 2025 13:40:10 GMT</pubDate>
            <atom:updated>2025-11-09T13:50:34.002Z</atom:updated>
            <content:encoded><![CDATA[<p>When we use Docker, we interact with a simple command-line interface that abstracts away a great deal of complexity. But what is actually happening on the system when you execute docker run? The answer lies in a precise application of fundamental Linux kernel features.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ff2oCI0bLu4J9eTqMDH5tA.png" /></figure><p>This article breaks down the core mechanics of how a Docker container comes to life, based on practical, verifiable evidence. We will explore:</p><ul><li>The kernel-level events that occur during a <strong><em>docker run </em></strong>command.</li><li>How namespaces and <strong><em>cgroups </em></strong>work together to create the isolated container environment.</li><li>How a container’s filesystem is managed and why its data persists on <strong><em>docker stop </em></strong>but is deleted on <strong><em>docker rm</em></strong>.</li></ul><h3>The docker run Command</h3><p>The journey of a container begins with a chain of delegation. The docker run command you type acts as a client, sending an API request to the Docker daemon (<strong><em>dockerd</em></strong>). This daemon manages high-level operations and delegates the core task of running the container to a supervisor called <strong><em>containerd</em></strong>. Finally, <em>containerd </em>uses a low-level, OCI-compliant runtime called <em>runc </em>to interface directly with the Linux kernel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*1i3ScglkeiUtMOG3szJtWw.png" /></figure><p>To create the container’s isolated environment, <em>runc </em>makes a specific system call (<em>syscall</em>) to the kernel — typically <strong><em>clone() </em></strong>or <strong><em>unshare().</em></strong> These syscalls accept flags that instruct the kernel to place a process into new, isolated <strong>namespaces</strong>. We can capture this event using a syscall tracer like <strong>strace</strong>. By attaching strace to the <em>containerd </em>daemon, we can see the exact instruction <em>runc </em>sends to the kernel:</p><p><strong><em>Example : </em></strong><em>This namespace experiment was done in an ubuntu system</em></p><p><strong>1.</strong> Terminal 1: Find the <em>containerd</em> PID and Attach strace</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*sQ9bqsQ0fe1ptUHPXvdSHg.png" /></figure><p>2. Terminal 2: Run a Container:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*sQ9bqsQ0fe1ptUHPXvdSHg.png" /></figure><p>3. Terminal 1: Capture format output showing the exact syscal</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/848/1*Ekgn_TOoRsF1bG23UaTCqw.png" /></figure><p>This single line is the container’s birth event. The <strong><em>CLONE_NEW</em></strong>* flags are the direct commands that tell the kernel to build the walls of isolation for the new process.</p><h3>The Container Environment: Namespaces and Cgroups</h3><p>The isolated environment is primarily the product of two independent but complementary kernel features.</p><h4>Namespaces: Controlling What a Container “Sees”</h4><p>Namespaces provide a process with its own isolated view of a specific system resource. docker uses <strong>Linux kernel clone/unshare/setns flags</strong> internally (and exposed via system calls) to create namespaces.</p><p>Linux Kernel Namespace Flags</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/706/1*N6Og-hJjVx3rjxIyxeplRA.png" /></figure><p><strong>Mount Namespace(<em>CLONE_NEWNS)</em> </strong>is arguably the most critical namespace. It solves the paradox of how an isolated container can see shared image files that exist on the host. The process is a two-phase operation:</p><ol><li><strong>Preparation (by containerd):</strong> Before the container starts, containerd tells the kernel’s OverlayFS driver to create a virtual MergedDir on the host. This directory is a unified view of the shared, read-only image layers (LowerDir) and a new, private writable layer (UpperDir).</li><li><strong>Setup (by runc):</strong> runc, executing inside the newly created and private mount namespace, performs a mount syscall. It instructs the kernel to make that host-prepared MergedDir the <strong>root filesystem (/)</strong> for this namespace only.</li></ol><p>The container’s process doesn’t “see” shared files from an isolated box. Rather, the runtime constructs a specific filesystem view for it and makes that view its entire world.</p><h4>Cgroups: Controlling What a Container USES</h4><p>While namespaces limit what a container can see, Control Groups (cgroups) limit what it can <strong>use</strong>. When you run a container with flags like — <strong><em>cpus=”0.5&quot; or — memory=”100m”</em></strong>, Docker creates a cgroup and writes these limits into control files within the<strong><em> /sys/fs/cgroup </em></strong>virtual filesystem. These are actively enforced by the kernel:</p><p><strong>1.CPU Throttling:</strong> If a process inside a container limited to 0.5 CPUs tries to use more, the kernel’s scheduler throttles it. The docker stats command shows this in real-time, with the CPU usage hitting a hard ceiling at the configured limit.</p><p><strong>2. Memory Termination:</strong> If a process attempts to allocate more memory than its cgroup allows, the kernel invokes the Out-Of-Memory (OOM) Killer, which forcibly terminates the process. This is confirmed by inspecting the container’s state, which will show an OOMKilled: true flag.</p><p><strong><em>Example : </em></strong><em>This cgroups experiment was done in an ubuntu system</em></p><p>1.Run a CPU-intensive process (loop) inside the limited container</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/915/1*-AEzvPEY3yCTwHTulR9rVQ.png" /></figure><p>2.Monitor CPU Throttling <em>(docker stats)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*7rdAsi2FR2H9EMrWC48p8A.png" /></figure><p>3.Install <em>stress-ng </em>and attempt to allocate 120 MB of memory (stress test)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*i0GNKzYaFSMUcaRo-4H-nw.png" /></figure><h3>Container State and the Filesystem Lifecycle</h3><p>The separation of concerns between containerd and its per-container child, containerd-shim, is key to understanding filesystem management.</p><ul><li><strong>containerd (The Resource Manager):</strong> As we saw, containerd prepares the filesystem resources. It identifies the shared image layers (LowerDir), creates the private writable layer (UpperDir), and prepares the unified MergedDir. It then generates a detailed config.json file — the container’s blueprint — which includes the path to this MergedDir.</li><li><strong>containerd-shim (The Process Manager):</strong> containerd launches a shim for each container. The shim’s job is to take the config.json blueprint and execute runc with it. runc reads the path to the MergedDir from the config and sets up the mount namespace. After runc exits, the shim’s only job is to supervise the running container process. For resilience, the shim is “re-parented” to the system’s init process (PID 1), decoupling its lifecycle from the daemons.</li></ul><p>This separation directly explains the difference between stopping and removing a container:</p><ul><li><strong>docker stop:</strong> This command signals the containerd-shim to terminate the container’s main process. The process is gone, but the filesystem resources prepared by containerd — specifically the writable UpperDir — <strong>remain on the host’s disk</strong>. The container’s state is preserved.</li><li><strong>docker rm:</strong> This command is an instruction to containerd to clean up the resources it manages. This includes <strong>deleting the container’s unique UpperDir from the disk</strong>. All data specific to that container is now permanently lost.</li></ul><p>We can prove this by capturing a container’s UpperDir path, stopping the container, and seeing that the directory still exists. Then, after removing the container, the same path is no longer valid.</p><p><strong><em>Example : </em></strong><em>This file system experiment was done in an ubuntu system</em></p><p>1. Start a Container and add Upper Dir path to a variable.</p><p>2. Create a file inside container and verify its existence in host</p><p>3. Stop container and verify Upper Dir existence (Directory Exists: the file is persisted)</p><p>4. Remove container and Verify Upper Dir deletion. (Directory and file are removed)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*uthu1-_rYoNDns1I_PakCA.png" /></figure><h3>Conclusion</h3><p>Docker’s power comes from its elegant use of fundamental Linux kernel features. A docker run command initiates a chain of delegation that results in a kernel syscall to create a namespaced process. This process is constrained by cgroups, which limit its resources. Its view of the filesystem is a carefully constructed mount namespace, and its data lifecycle is tied to a distinct writable layer. Understanding these core mechanics reveals that a container is a well-defined, kernel-enforced design, providing an efficient and powerful tool for modern software development.</p><h3>References and Further Reading:</h3><ul><li><a href="https://www.codementor.io/blog/docker-technology-5x1kilcbow">Docker: What&#39;s Under the Hood?</a></li><li><a href="https://spacelift.io/blog/docker-architecture">Docker Architecture Overview - Structure &amp; Components</a></li><li><a href="https://www.docker.com/blog/how-docker-desktop-networking-works-under-the-hood/">How Docker Desktop Networking Works Under the Hood | Docker</a></li><li><a href="https://blog.nginx.org/blog/what-are-namespaces-cgroups-how-do-they-work">What Are Namespaces and cgroups, and How Do They Work?</a></li><li><a href="https://dev.to/pemcconnell/docker-overlayfs-network-namespaces-docker-bridge-and-dns-52jo">Docker Overlayfs: How filesystems work in Docker</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=03d2251244e4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Practical Guide to the Circuit Breaker Pattern with Spring Boot]]></title>
            <link>https://medium.com/@bharishx/a-practical-guide-to-the-circuit-breaker-pattern-with-spring-boot-87c9dbd26e53?source=rss-0311e3ceba7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/87c9dbd26e53</guid>
            <category><![CDATA[practical-guides]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[circuit-breaker]]></category>
            <category><![CDATA[resilience]]></category>
            <dc:creator><![CDATA[Harish Balakrishnar]]></dc:creator>
            <pubDate>Sat, 27 Sep 2025 21:04:10 GMT</pubDate>
            <atom:updated>2025-09-27T21:20:41.891Z</atom:updated>
            <content:encoded><![CDATA[<p>In a microservice architecture, services often depend on each other. A <strong><em>User Service </em></strong>might need to call a <strong><em>Catalog Service </em></strong>to get product information. This works fine until the <strong><em>Catalog Service </em></strong>slows down or crashes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aA1VjbRCQJxJr6vPNxxraw.png" /></figure><h3>The Problem: Cascading Failures</h3><p>When a dependent service fails, the services that call it start getting errors. If the <em>User Service</em> keeps trying to call the dead <em>Catalog Service</em>, it will waste its own resources, like network connections and threads. This can cause the <em>User Service</em> to become slow and unresponsive, leading to a chain reaction where one service failure brings down other services. This is called a <strong>cascading failure</strong>.</p><p>The <strong>Circuit Breaker pattern</strong> is a robust way to prevent this.</p><h3>What is the Circuit Breaker Pattern?</h3><p>The Circuit Breaker is a component that wraps network calls to external services. It monitors these calls for failures. If the number of failures reaches a certain limit, it <em>“opens the circuit,”</em> meaning it stops trying to make calls to the failing service for a while. Instead, it immediately returns an error or a default <em>“fallback”</em> response.</p><p>This gives the failing service time to recover and prevents the calling service from wasting resources.</p><p>The pattern has three states:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/944/1*1xxCL3_8V6uUYogmDGHSSw.png" /></figure><ul><li><strong>CLOSED</strong>: The normal state. All calls are permitted to go to the external service. The circuit breaker counts failures. If the failure rate exceeds a configured threshold, it moves to the <strong>OPEN </strong>state.</li><li><strong>OPEN</strong>: The circuit is <em>“tripped.”</em> For a configured duration, all calls to the external service are blocked immediately. The circuit breaker returns a fallback response without attempting the call. After the timeout, it moves to the <strong>HALF_OPEN </strong>state.</li><li><strong>HALF_OPEN</strong>: A small, limited number of test calls are allowed to pass through to the external service. Based on the success or failure of these test calls, the circuit breaker decides whether to move back to <strong>CLOSED </strong>or return to <strong>OPEN</strong>.</li></ul><h3>A Practical Implementation</h3><p>Let’s build this with two Spring Boot services:</p><ol><li><strong>catalog-service</strong>: Exposes product order data on port <em>9191</em>.</li><li><strong>user-service</strong>: Calls the<em> catalog-service</em> and implements the circuit breaker on port <em>9292</em>.</li></ol><p>You can find the complete source code for this project on GitHub. <br><a href="https://github.com/Harish3000/internship-with-krish/tree/main/2-springbootj-circuit-breaker">🔗GitHub Link to project files</a></p><h3>Step 1: Project Setup and Dependencies</h3><p>When creating your <em>user-service </em>, you will start with standard dependencies like <strong>Spring-web</strong>. To implement the circuit breaker, you need to add three more dependencies to your <em>pom.xml</em>.</p><pre>  &lt;dependency&gt;<br>   &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>   &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;<br>  &lt;/dependency&gt;<br><br>  &lt;dependency&gt;<br>   &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>   &lt;artifactId&gt;spring-boot-starter-aop&lt;/artifactId&gt;<br>  &lt;/dependency&gt;<br><br>  &lt;dependency&gt;<br>   &lt;groupId&gt;io.github.resilience4j&lt;/groupId&gt;<br>   &lt;artifactId&gt;resilience4j-spring-boot2&lt;/artifactId&gt;<br>   &lt;version&gt;1.5.0&lt;/version&gt;<br>  &lt;/dependency&gt;</pre><ul><li><strong>spring-boot-starter-actuator</strong> : Gives us endpoints to monitor the application&#39;s health and the circuit breaker&#39;s state.</li><li><strong>spring-boot-starter-aop </strong>: Aspect-Oriented Programming. Resilience4J uses this to wrap your method in the circuit breaker logic without you having to write extra code.</li><li><strong>resilience4j-spring-boot2</strong>: The main Resilience4J library.</li></ul><h3>Step 2: Protecting the Method Call</h3><p>In the <em>user-service</em>, we apply the <strong>@CircuitBreaker</strong> annotation to the method that makes the REST call to the <em>catalog-service</em>.</p><pre>@SpringBootApplication<br>@RestController<br>@RequestMapping(&quot;/user-service&quot;)<br>public class UserServiceApplication {<br><br>    @Autowired<br>    @Lazy<br>    private RestTemplate restTemplate;<br><br>    public static final String USER_SERVICE=&quot;userService&quot;;<br><br>    private static final String BASEURL = &quot;http://localhost:9191/orders&quot;;<br><br>    private int attempt=1;<br><br><br>    @GetMapping(&quot;/displayOrders&quot;)<br>    @CircuitBreaker(name =USER_SERVICE,fallbackMethod = &quot;getAllAvailableProducts&quot;)<br>    //@Retry(name = USER_SERVICE,fallbackMethod = &quot;getAllAvailableProducts&quot;)<br>    public List&lt;OrderDTO&gt; displayOrders(@RequestParam(&quot;category&quot;) String category) {<br>        String url = category == null ? BASEURL : BASEURL + &quot;/&quot; + category;<br>        return restTemplate.getForObject(url, ArrayList.class);<br>    }<br><br><br>    public List&lt;OrderDTO&gt; getAllAvailableProducts(Exception e){<br>        return Stream.of(<br>                new OrderDTO(119, &quot;OLD-LED TV&quot;, &quot;electronics&quot;, &quot;white&quot;, 45000),<br>                new OrderDTO(345, &quot;OLD-Headset&quot;, &quot;electronics&quot;, &quot;black&quot;, 7000),<br>                new OrderDTO(475, &quot;OLD-Sound bar&quot;, &quot;electronics&quot;, &quot;black&quot;, 13000),<br>                new OrderDTO(574, &quot;OLD-Puma Shoes&quot;, &quot;foot wear&quot;, &quot;black &amp; white&quot;, 4600),<br>                new OrderDTO(678, &quot;OLD-Vegetable chopper&quot;, &quot;kitchen&quot;, &quot;blue&quot;, 999),<br>                new OrderDTO(532, &quot;OLD-Oven Gloves&quot;, &quot;kitchen&quot;, &quot;gray&quot;, 745)<br>        ).collect(Collectors.toList());<br>    }</pre><ul><li><strong>@CircuitBreaker(name = &quot;userService&quot;, ...)</strong>: This enables the circuit breaker. The <em>name </em>is important, as we&#39;ll use it to configure this specific circuit breaker instance.</li><li><strong>fallbackMethod = &quot;getAllAvailableProducts&quot;</strong>: If the <em>displayOrders </em>call fails or the circuit is open, Resilience4J will call this method instead. This provides a graceful failure response to the user.</li></ul><h3>Step 3: Configuring the Circuit Breaker</h3><p>In the <em>user-service’s</em> <strong>application.yml</strong>, we define the rules for our userService instance.</p><pre>management:<br>  health:<br>    circuitbreakers:<br>      enabled: true<br>  endpoints:<br>    web:<br>      exposure:<br>        include: health<br>  endpoint:<br>    health:<br>      show-details: always<br><br><br>resilience4j:<br>  circuitbreaker:<br>    instances:<br>      userService:<br>        registerHealthIndicator: true<br>        eventConsumerBufferSize: 10<br>        failureRateThreshold: 50<br>        minimumNumberOfCalls: 5<br>        automaticTransitionFromOpenToHalfOpenEnabled: true<br>        waitDurationInOpenState: 5s<br>        permittedNumberOfCallsInHalfOpenState: 3<br>        slidingWindowSize: 10<br>        slidingWindowType: COUNT_BASED</pre><ul><li><strong><em>failureRateThreshold: 50</em></strong>: If 50% of the last 10 calls fail, open the circuit.</li><li><strong><em>minimumNumberOfCalls: 5</em></strong>: Don&#39;t calculate the failure rate until at least 5 calls have been made.</li><li><strong><em>waitDurationInOpenState: 5s</em></strong>: Keep the circuit open for 5 seconds before moving to <strong>HALF_OPEN</strong>.</li><li><strong><em>permittedNumberOfCallsInHalfOpenState: 3</em></strong>: Allow 3 test calls in the <strong>HALF_OPEN </strong>state.</li></ul><h3>Step 4: Observing the Circuit Breaker in Action</h3><p>Let’s see how the states change. We will use a tool like Postman for this.</p><p><strong>Endpoints to use:</strong></p><ul><li><strong>User Service API:</strong> <br><strong>GET : </strong><em>http://localhost:9292/user-service/displayOrders?category</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NIt4RU8QULGh8V4QWPjxfA.png" /><figcaption>Expected response</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*duTjojhGCJ2rnRieBwCEBA.png" /><figcaption>Fall back response</figcaption></figure><ul><li><strong>Actuator Health API:</strong> <br><strong>GET:<em> </em></strong><em>http://localhost:9292/actuator/health</em></li></ul><h3>State 1: CLOSED (Normal Operation)</h3><ol><li>Start both the <em>catalog-service</em> and the <em>user-service</em>.</li><li>In Postman, send a request to the User Service API. You’ll get a successful JSON response with order data.</li><li>Now, check the Actuator Health API. In the JSON response, you will find the circuit breaker details showing <strong><em>&quot;status&quot;: &quot;CLOSED&quot;</em></strong>. This is the healthy, default state.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KLKzcKGunQyjSuPk97MNUA.png" /></figure><h3>State 2: Transitioning to OPEN (Service Failure)</h3><ol><li>Stop the <em>catalog-service</em>.</li><li>In Postman, send requests to the User Service API again. The first few requests will fail.</li><li>Keep sending requests. After the 5th request (<strong><em>minimumNumberOfCalls</em></strong>), the circuit breaker has enough data. It sees the failure rate is 100%, which is greater than our 50% threshold, so it <strong>trips the circuit</strong>.</li><li>Now, when you send another request, you will instantly get the hardcoded fallback data.</li><li>Check the Actuator Health API. <br>The status will now be <strong><em>&quot;status&quot;: &quot;OPEN&quot;</em></strong>.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hIkW5FioRteCQDIeqW5B-g.png" /></figure><h3>State 3: Transitioning to HALF_OPEN (The Waiting Period)</h3><ol><li>With the circuit <strong>OPEN</strong>, wait for 5 seconds (<strong><em>waitDurationInOpenState</em></strong>).</li><li>Refresh the Actuator Health API endpoint. The status will automatically change to <strong><em>&quot;status&quot;: &quot;HALF_OPEN&quot;</em></strong>. The system is now ready to send 3 test requests to see if the <em>catalog-service</em> has recovered.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yrmGTjVPYSZHR10T-acVmQ.png" /></figure><h3>State 4: Testing Recovery (Two Possible Outcomes)</h3><p><strong>Outcome A: Service Recovers (HALF_OPEN → CLOSED)</strong></p><ol><li>Restart the <em>catalog-service</em>.</li><li>Send 3 requests to the User Service API. Since the circuit is <strong>HALF_OPEN</strong>, all 3 calls are allowed. They will all succeed.</li><li>After the 3rd successful call, the circuit evaluates the window: <br>(0 failures / 3 calls) = 0% failure rate. <br>This is less than the 50% threshold.</li><li>The circuit breaker now moves to the <strong>CLOSED </strong>state. <br>Normal operation has resumed.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gfHkcURi7Ah6DdT8Iau2sw.png" /></figure><p><strong>Outcome B: Service is Still Down (HALF_OPEN → OPEN)</strong></p><ol><li>Ensure the <em>catalog-service</em> is still stopped. <br>The circuit is in the <strong>HALF_OPEN </strong>state.</li><li>Send 3 requests to the User Service API. <br>All 3 are permitted and all 3 will fail, returning the fallback data.</li><li>After the 3rd failed call, the circuit evaluates the window: <br>(3 failures / 3 calls) = 100% failure rate. <br>This is greater than the 50% threshold.</li><li>The circuit breaker immediately transitions back to the <strong>OPEN </strong>state and the <strong><em>waitDurationInOpenState </em></strong>timer starts again.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/993/1*MFywmjeEZCzKvAHUeFsU5A.png" /></figure><p>The Circuit Breaker pattern is a fundamental concept for building resilient microservices. By using Spring Boot with Resilience4J, you can prevent cascading failures, allow your system to handle downstream service outages gracefully, and enable automatic recovery.</p><h3>References and Further Reading:</h3><ul><li><a href="https://resilience4j.readme.io/docs/circuitbreaker">CircuitBreaker</a></li><li><a href="https://spring.io/guides/gs/cloud-circuit-breaker">Getting Started | Spring Cloud Circuit Breaker Guide</a></li><li><a href="https://www.geeksforgeeks.org/advance-java/spring-boot-circuit-breaker-pattern-with-resilience4j/">Spring Boot - Circuit Breaker Pattern with Resilience4J - GeeksforGeeks</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87c9dbd26e53" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An Overview of Essential Deployment Patterns]]></title>
            <link>https://medium.com/@bharishx/an-overview-of-essential-deployment-patterns-fe71ea49cb53?source=rss-0311e3ceba7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe71ea49cb53</guid>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[ci-cd-pipeline]]></category>
            <category><![CDATA[reliability]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[software-design-patterns]]></category>
            <dc:creator><![CDATA[Harish Balakrishnar]]></dc:creator>
            <pubDate>Sat, 27 Sep 2025 17:56:59 GMT</pubDate>
            <atom:updated>2025-09-27T18:52:03.694Z</atom:updated>
            <content:encoded><![CDATA[<p>In software engineering, we often focus on design patterns for building our applications. But how we release that software is just as important. This guide explores the fundamental patterns that govern how we safely and efficiently introduce new code into production.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j7ZaBBlBwNz65I_IIXZosg.png" /></figure><p>We’ll break down these patterns into two categories:</p><ol><li><strong>Component Patterns :</strong> These define how services and their operational helpers are packaged and deployed together.<br><strong>Eg:</strong> Sidecar, Ambassador, Adapter</li><li><strong>Strategic Patterns :</strong> These are high-level strategies for managing the release process itself.<br><strong>Eg:</strong> Blue-Green, Canary, Traffic Shadowing</li></ol><h3>Part 1: Component Deployment Patterns</h3><p>These patterns focus on decomposing application responsibilities at the deployment level, often within a single execution environment like a Kubernetes Pod.</p><h3>1. The Sidecar Pattern</h3><p>The Sidecar pattern involves deploying a helper container alongside your main application container within the same pod. They share the same lifecycle and network space, communicating via <em>localhost</em>.</p><p><strong>Why use it?</strong> <br>To offload cross-cutting concerns (non-business logic) from your application. This keeps your primary service lean and focused on its core domain. Common tasks for a sidecar include logging, metrics collection, and security.</p><p><strong>Real-World Example: Security Proxy</strong> <br>Imagine an <em>Employee service </em>whose only job is to manage employee data. Instead of building JWT token validation, mTLS, and other security checks directly into this service, we can offload it to a sidecar proxy.</p><ol><li>An incoming request first hits the security sidecar container.</li><li>The sidecar validates the JWT token and performs all necessary security handshakes.</li><li>If the request is valid, the sidecar forwards it to the <em>Employee service </em>container running on <em>localhost</em>.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/993/1*kVYgaVn6suboqhAK944F8Q.png" /></figure><p>With this, the <em>Employee service</em> developers never have to worry about security implementation details. A key point is that each service pod gets its own dedicated sidecar instance.</p><p><strong>Pro:</strong> Decouples application logic from operational concerns.</p><p><strong>Con:</strong> Can increase pod startup time, as all containers (main app and sidecars) must be running before the pod is considered “ready.” This can impact recovery time and availability.</p><h3>2. The Ambassador Pattern</h3><p>The Ambassador pattern is another proxy, but its primary job is to manage <strong>outgoing</strong> network communication from your main application.</p><p><strong>Why use it?</strong> <br>To abstract away the complexity of connecting to external or legacy systems. Your application makes a simple, local request, and the Ambassador handles the difficult translation, authentication, or protocol negotiation.</p><p><strong>Real — World Example: Legacy System Integration</strong> <br><em>Employee service</em> needs to fetch data from a legacy mainframe that communicates using a complex, non-HTTP protocol like RPC.</p><ol><li>Instead of embedding legacy client libraries and complex logic into the <em>Employee service</em>, it makes a simple REST call to <em>localhost</em>.</li><li>The Ambassador container receives this call. It contains all the necessary logic to translate the REST request into an RPC call, handle the mainframe’s specific authentication, and make the connection.</li><li>It receives the response from the mainframe, translates it back into a standard JSON format, and returns it to the <em>Employee service</em>.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HwtEph30W1LdwnakH7zQqw.png" /></figure><p>This pattern keeps your core services clean and free from legacy integration code.</p><h3>3. The Adapter Pattern</h3><p>The Adapter pattern acts as a “translator between worlds.” It is a standalone service responsible for transforming data from your service’s canonical format into the specific formats required by various external consumers.</p><p><strong>Why use it?</strong> <br>To decouple your service from the unique requirements of its clients. Your service should not contain business logic that exists only to satisfy an external system’s needs.</p><p><strong>Real-World Example: Data Transformation for Different Clients</strong> <br>Your <em>Employee service</em> exposes an endpoint that returns data with <em>firstName </em>and <em>lastName</em>.</p><ul><li><strong>External System A</strong> requires a single concatenated <em>fullName </em>field.</li><li><strong>External System B</strong> requires both <em>fullName </em>and the user&#39;s <em>initials</em>.</li></ul><p>Instead of adding this transformation logic to the <em>Employee service</em>, we use an Adapter.</p><ol><li>The <em>Employee service</em> sends its standard response.</li><li>The Adapter service receives this response.</li><li>It has specific logic to transform the data into the format required by System A and forwards it. It does the same for System B.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i5Wkatwt60-r27amQRjz9A.png" /></figure><h3>Part 2: Strategic Deployment Patterns</h3><p>These patterns are high-level strategies for rolling out new versions of your application to production.</p><h3>1. Blue-Green Deployment</h3><p>This strategy involves running two identical, isolated production environments: “Blue” (the current live version) and “Green” (the new version).</p><p><strong>How it works:</strong></p><ol><li>The Blue environment is live, serving all production traffic with version 1.0.</li><li>A completely identical Green environment is created. Version 2.0 of the application is deployed to it.</li><li>The Green environment can be thoroughly tested (load tests, integration tests) without any impact on live users.</li><li>Once you are confident in the Green environment, you configure your load balancer to switch all traffic from Blue to Green instantly. Green is now live.</li><li>The Blue environment is kept on standby.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L-2hh3ixSmZGEnf3osKxbg.png" /></figure><p><strong>Rollback:</strong> If a critical bug is discovered in version 2.0, the rollback is instantaneous: simply flip the switch at the load balancer back to the Blue environment.</p><p><strong>Pro:</strong> Instant, zero-downtime rollout and rollback. Eliminates risk by testing on a production-like environment.</p><p><strong>Con:</strong> Cost. You must maintain and pay for double the production infrastructure, even though half of it is idle at any given time.</p><h3>2. Canary Deployment</h3><p>Named after the “canary in a coal mine,” this pattern involves slowly rolling out a new version to a small subset of users before making it available to everyone.</p><p><strong>How it works:</strong></p><ol><li>The production environment is running version 1.0.</li><li>You deploy a few instances of version 2.0 alongside the 1.0 instances.</li><li>You configure your load balancer or gateway to route a small percentage of traffic (e.g., 1% or 5%) to the new 2.0 instances.</li><li>You monitor the performance and error rates of the new version closely.</li><li>If it remains stable, you gradually increase the traffic percentage (10%, 25%, 50%) until 100% of the traffic is on version 2.0.</li><li>The old 1.0 instances are then removed.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AqN5LHscx5McTkrWTA53Rw.png" /></figure><p><strong>Rollback:</strong> If error rates spike at any stage, the system can automatically route 100% of traffic back to the stable version 1.0.</p><p><strong>Pro:</strong> More cost-effective than Blue-Green. Allows for testing with real user traffic with minimal impact.</p><p><strong>Con:</strong> The rollout is slower, and managing state can be complex if a user session bounces between different versions. This is why stateless services are ideal for this pattern.</p><h3>3. Traffic Shadowing (Dark Launch)</h3><p>This pattern involves testing a new version with real production traffic without affecting users. A copy of the live traffic is sent to the new version, but the response is never sent back to the user.</p><p><strong>How it works:</strong></p><ol><li>A user sends a request, which goes to the stable version 1.0.</li><li>Version 1.0 processes the request and sends its response back to the user as normal.</li><li>Simultaneously, a copy of that incoming request is “shadowed” and sent to the new version 2.0.</li><li>Version 2.0 processes the request, but its response is either logged for analysis or completely discarded.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CrRNEiPJFtenAimIxxV8Lg.png" /></figure><p><strong>Challenge:</strong> This is dangerous for requests that change data (non-idempotent operations), as you would be creating duplicate records. Solutions include:</p><ul><li>Only shadowing read-only traffic.</li><li>Implementing a “dry run” mode in the new service that rolls back database transactions.</li><li>Having the shadow service write to a separate sandbox database.</li></ul><p><strong>Pro:</strong> Safely test performance and functionality of a new version with real, unpredictable production load.</p><p><strong>Con:</strong> Complex to implement correctly, especially with stateful services.</p><h3>4. Rollout Deployment</h3><p>This is one of the most common and straightforward deployment patterns. It involves incrementally replacing old instances with new ones.</p><p><strong>How it works:</strong></p><ol><li>You have 100 instances running version 1.0.</li><li>You deploy 10 new instances of version 2.0.</li><li>The system waits for these 10 new instances to become “healthy” (i.e., pass their health checks).</li><li>Once healthy, 10 old 1.0 instances are terminated.</li><li>This process repeats until all 100 instances are running version 2.0.</li></ol><p><strong>Pro:</strong> Simple and widely supported by container orchestrators like Kubernetes.</p><p><strong>Con:</strong> Health checks are often basic (e.g., “is the process running?”). This method doesn’t validate business functionality or performance under load, so a faulty but technically “healthy” version could be fully rolled out.</p><p>Choosing the right deployment pattern is a trade-off between cost, speed, and risk. By understanding these fundamental patterns, we can make informed decisions to deliver software more reliably and with greater confidence.</p><h3>References and Further Reading:</h3><ul><li><a href="https://learn.microsoft.com/en-us/azure/architecture/patterns/sidecar">Sidecar pattern - Azure Architecture Center</a></li><li><a href="https://www.geeksforgeeks.org/system-design/ambassador-pattern-in-distributed-systems/">Ambassador Pattern in Distributed Systems - GeeksforGeeks</a></li><li><a href="https://docs.aws.amazon.com/whitepapers/latest/blue-green-deployments/welcome.html">Blue/Green Deployments on AWS</a></li><li><a href="https://cloud.google.com/deploy/docs/deployment-strategies/canary">Use a canary deployment strategy | Cloud Deploy | Google Cloud</a></li><li><a href="https://cloud.google.com/blog/products/gcp/cre-life-lessons-what-is-a-dark-launch-and-what-does-it-do-for-me">Dark launches: What they are and how to do them | Google Cloud Blog</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe71ea49cb53" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data Management Patterns for Microservice Architectures.]]></title>
            <link>https://medium.com/@bharishx/data-management-patterns-for-microservice-architectures-7045bbd9e053?source=rss-0311e3ceba7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/7045bbd9e053</guid>
            <category><![CDATA[data]]></category>
            <category><![CDATA[microservice-architecture]]></category>
            <category><![CDATA[eventual-consistency]]></category>
            <category><![CDATA[software-design-patterns]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Harish Balakrishnar]]></dc:creator>
            <pubDate>Sat, 27 Sep 2025 12:02:52 GMT</pubDate>
            <atom:updated>2025-09-27T18:49:51.750Z</atom:updated>
            <content:encoded><![CDATA[<p>When moving from a monolithic architecture to microservices, one of the most significant challenges is managing data. In a monolith, a single database with ACID transactions keeps everything consistent. In the distributed world of microservices, where each service owns its database, that safety net is gone. This article breaks down the essential data-centric patterns you need to maintain consistency and build resilient systems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hF6XdtQ6twJW1G5oU4uzZw.png" /></figure><h3>The Distributed Transaction Problem</h3><p>In a monolithic application, creating a customer order might involve updating the <strong><em>Order</em></strong>, <strong><em>Stock</em></strong>, and <strong><em>Customer </em></strong>tables. This is wrapped in a single database transaction.</p><ol><li><strong><em>BEGIN TRANSACTION</em></strong></li><li>Update Order table.</li><li>Update Stock table.</li><li>Update Customer table.</li><li><strong><em>COMMIT TRANSACTION</em></strong></li></ol><p>If any step fails, a <strong>Rollback </strong>undoes all changes, ensuring the system remains consistent. This is simple and effective.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*L1AuFTihfWtKI2bXGpKp6Q.png" /></figure><p>In a <strong>microservices architecture</strong>, this process is split across an Order Service, Inventory Service, and Customer Service, each with its own private database. You cannot run a single database transaction across these independent services. If the Customer Service fails after the Order and Inventory services have succeeded, the system is left in an inconsistent state. This is the Distributed Transaction Problem. We will discuss some patterns commonly used to overcome this limitation.</p><h3>1. The Saga Pattern</h3><p>The Saga pattern manages data consistency across multiple services by coordinating a sequence of local transactions. If a step fails, the saga executes compensating transactions to undo the preceding work.</p><h4>Orchestration-based Saga</h4><p>An orchestrator (which can be a dedicated service or part of an existing one) coordinates the entire workflow. It tells each service what to do and reacts to the outcome.</p><p><strong>Example: E-commerce Order Placement</strong></p><ol><li><strong>Request:</strong> A client sends a <em>“Create Order”</em> request to the <strong>Orchestrator</strong>.</li><li>The orchestrator commands the Order service to create an order in a <strong>PENDING </strong>state. The Order service, succeeds and confirms.</li><li>The orchestrator commands the Inventory service to reserve stock. The Inventory Service succeeds and confirms.</li><li>The orchestrator commands thePayment service to process the payment. <strong>This step fails.</strong></li><li><strong>Compensation:</strong> The orchestrator initiates a rollback.</li></ol><ul><li>It commands the Inventory service to release the reserved stock (compensating transaction).</li><li>It commands the Order service to update the order status to <strong>CANCELLED </strong>(compensating transaction).</li></ul><p><strong>Disadvantage:</strong> The orchestrator is a single point of failure and can become a bottleneck.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M8tvC5HB1S3MIwpHEtFfVw.png" /></figure><h4>Choreography-based Saga</h4><p>This decentralized approach uses an event-driven model. Services react to events from other services without a central coordinator.</p><p><strong>Example: E-commerce Order Placement (Event-Driven)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TopBZ1lIvO1ybsN-oET4lA.png" /></figure><ol><li>The <em>Order service</em> creates a <strong>PENDING </strong>order and publishes an <strong><em>OrderCreated </em></strong>event to a message broker like Kafka.</li><li>The <em>Inventory service</em> subscribes to <strong><em>OrderCreated </em></strong>events. It receives the event, reserves stock, and publishes an <strong><em>InventoryReserved </em></strong>event.</li><li>The <em>Payment Service </em>subscribes to <strong><em>InventoryReserved </em></strong>events. It receives the event, attempts to process payment, and publishes a <strong><em>PaymentProcessed </em></strong>event (or a <strong><em>PaymentFailed </em></strong>event).</li><li><strong>Success/Failure — </strong>Other services subscribe to these events to complete or cancel the order.</li></ol><p><strong>Advantages:</strong> No single point of failure and services are loosely coupled. <strong>Disadvantages:</strong> Harder to debug as there’s no central view of the process flow.</p><h3>2. Try-Confirm/Cancel (TCC) Pattern</h3><p>TCC is a pattern where each step in a distributed transaction has three phases:</p><ol><li><strong>Try:</strong> Provisionally reserve a resource. This action must be reversible.</li><li><strong>Confirm:</strong> Commit the resource reservation, making the change permanent.</li><li><strong>Cancel:</strong> Release the provisionally held resource if any step fails.</li></ol><p><strong>Example: Booking a Flight</strong></p><p><strong>1.Try:</strong></p><ul><li>The Booking Service asks the Seat Service to temporarily hold a seat for 10 minutes.</li><li>It asks the Payment Service to pre-authorize the payment on your credit card.</li></ul><p><strong>2.Confirm:</strong> If both “try” steps succeed, the Booking Service executes the confirm phase:</p><ul><li>It tells the Seat Service to confirm the seat reservation.</li><li>It tells the Payment Service to capture the pre-authorized funds.</li></ul><p><strong>3.Cancel:</strong> If the payment pre-authorization fails (or the user abandons the booking), the Booking Service executes the cancel phase:</p><ul><li>It tells the Seat Service to release the held seat.</li></ul><p>A key challenge is eventual inconsistency. If all “try” phases succeed but a “confirm” call fails (e.g., a network error), the system is stuck. This requires background jobs or schedulers to periodically check for and resolve these incomplete transactions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*ng76l8XqFbV23g-sisqXUA.png" /></figure><h3>3. Event Sourcing</h3><p>Instead of storing the current state of an entity, Event Sourcing stores the full sequence of events that have happened to that entity. The current state is calculated by replaying the events.</p><p><strong>Example: Inventory Management</strong></p><p>A traditional database would store <strong>product_id: 123</strong>.</p><p>With Event Sourcing, you store a log of events:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*0rqiKVG-G3aY3sLTbADMfA.png" /></figure><p>To get the current stock, you replay these events: <strong>100 - 10 - 50 + 5 = 45</strong>.</p><p><strong>Advantages:</strong></p><ul><li><strong>Audit Trail:</strong> You have an immutable, complete history of every change.</li><li><strong>Temporal Queries:</strong> You can reconstruct the state of an entity at any point in time (e.g., “What was the stock level last Tuesday?”).</li></ul><p><strong>Disadvantages:</strong> Querying the current state can be slow, as it requires processing the entire event log.</p><h3>4. Command Query Responsibility Segregation (CQRS)</h3><p>CQRS solves the problem where read and write workloads are fundamentally different. For instance, heavy reporting queries (reads) can lock database tables and slow down customer orders (writes). CQRS separates these concerns.</p><ul><li><strong>Command Side:</strong> Handles all write operations (<em>create, update,delete</em>). It uses a data model optimized for transactions and consistency (e.g., a normalized SQL database).</li><li><strong>Query Side:</strong> Handles all read operations. It uses one or mor<br>e read-optimized data models, which can be denormalized or even use different technologies (e.g., Elasticsearch for search, a data warehouse for analytics).</li></ul><p>Data is synchronized from the write database to the read database(s), often through events.</p><p><strong>Example: E-commerce Platform</strong></p><ul><li><strong>Command:</strong> A user places an order. This <em>CreateOrder </em>command goes to the write database, which is normalized to ensure data integrity.</li><li><strong>Synchronization:</strong> The write database publishes an <em>OrderPlaced </em>event.</li><li><strong>Query:</strong> A listener updates a denormalized read database (e.g., a document in Elasticsearch containing order, customer, and product details) to power the user’s order history page. Another listener updates a data warehouse for sales analytics.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/823/1*yEdlXh4SJtTJXrbSM1d4lA.png" /></figure><h3>5. Change Data Capture (CDC)</h3><p>CDC is a technique for automatically capturing data changes from a database and streaming them to other systems. It’s a highly efficient way to implement the synchronization required by CQRS. Instead of the application publishing an event, CDC tools tap directly into the database’s internal transaction log (<em>binlog </em>in MySQL, <em>WAL </em>in Postgres).</p><p><strong>Example: Modernizing a Legacy ERP</strong></p><ol><li>A new sale is entered into a legacy MS SQL database.</li><li><strong>Debezium</strong> (a popular CDC tool) is configured as a connector monitoring the MS SQL transaction log. It captures the <strong>INSERT </strong>record as an event.</li><li>Debezium publishes this event to a <strong>Kafka</strong> topic.</li><li>Downstream consumers, such as a new <em>Shipping microservice</em> (using Postgres) and a <em>Search Service</em> (using Elasticsearch), subscribe to the topic and update their own local databases in real-time.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/773/1*7A2tQ6hTmm-nlo7FnW1hkA.png" /></figure><h3>6. Schema Evolution and Contracts</h3><p>In an event-driven system, if a service that produces events (producer) changes an event’s structure, it can break services that consume those events.</p><ul><li><strong>Schema Evolution:</strong> Refers to managing schema changes over time. Adding an optional field is a backward-compatible change. Removing a required field is a breaking change. A <strong>Schema Registry</strong> is a tool that helps enforce compatibility rules.</li><li><strong>Consumer-Driven Contract Testing:</strong> This practice flips the responsibility. Consumers define a “contract” specifying the event format they expect. Using tools like <strong>Pact</strong>, before a producer service is deployed, its new event format is automatically tested against the contracts of all its consumers. This catches breaking changes before they reach production.</li></ul><p>While monolithic transactions are simple, microservices demand patterns like Saga and CQRS to ensure data consistency and system resilience.</p><h3>References and Further Reading:</h3><ul><li><a href="https://learn.microsoft.com/en-us/azure/architecture/patterns/saga">Saga Design Pattern - Azure Architecture Center</a></li><li><a href="https://docs.oracle.com/en/database/oracle/transaction-manager-for-microservices/24.2/tmmdg/tcc-transaction-model.html">Transaction Manager for Microservices Developer Guide</a></li><li><a href="https://www.geeksforgeeks.org/system-design/event-sourcing-pattern/">Event Sourcing Pattern - GeeksforGeeks</a></li><li><a href="https://microservices.io/patterns/data/cqrs.html">Microservices Pattern: Pattern: Command Query Responsibility Segregation (CQRS)</a></li><li><a href="https://www.confluent.io/blog/how-change-data-capture-works-patterns-solutions-implementation/">How Change Data Capture (CDC) Works</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7045bbd9e053" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fundamentals of Reliable Digital Services]]></title>
            <link>https://medium.com/@bharishx/fundamentals-of-reliable-digital-services-63a3211b68d0?source=rss-0311e3ceba7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/63a3211b68d0</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[low-latency]]></category>
            <category><![CDATA[reliability]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[software-architecture]]></category>
            <dc:creator><![CDATA[Harish Balakrishnar]]></dc:creator>
            <pubDate>Fri, 12 Sep 2025 10:22:16 GMT</pubDate>
            <atom:updated>2025-09-14T02:42:54.184Z</atom:updated>
            <content:encoded><![CDATA[<p>When you build with microservices, you have lots of small, separate services to manage. While this gives you freedom and scalability, it creates two big challenges: how do your apps talk to all these services, and how does the system survive when one of those services eventually fails?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YKSaJlhmZT942y2FM8QtXg.png" /></figure><p>Let’s walk through the essential patterns that provide the answers.</p><h3>The Front Door: Managing Inbound Traffic</h3><p>In a microservice cluster, there are two kinds of network traffic. <strong>North-South traffic</strong> flows from an external client (like a web app) into our cluster. <strong>East-West traffic</strong> flows between services inside the cluster. Securing and managing the North-South entry point is the top priority.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*mHSV3g62pRRVxuCmTv57XA.png" /></figure><h4>API Gateway Pattern</h4><p>Instead of forcing a client to know the address of every single service, an API Gateway (a server-side pattern) acts as a single front door. It receives all incoming requests and routes them to the correct service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*0tgDreLyKIjWS_02XzcVcg.png" /></figure><p>This simplifies the client, handles common tasks like security in one place, and provides a layer for API management (versioning, metrics, etc.).</p><h4>Proxy Pattern</h4><p>A proxy also forwards traffic. An API Gateway is a specialized Reverse Proxy (a server-side pattern), which takes requests from the outside world and routes them to internal services, hiding the details of your internal services. This is different from a Forward Proxy, which is used by internal clients to access the outside world, hiding the client’s identity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*nMat892UaAP1rg7fQOAckw.png" /></figure><h4><strong>Service Discovery Pattern</strong></h4><p>How does the gateway know where to send requests, especially when service IPs change constantly? It uses a <strong>Service Discovery</strong> tool.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lsVpQePHIgQkDg_Jz196dg.png" /></figure><ul><li><strong>Server-Side Discovery<br></strong>In this <strong>server-side</strong> approach, the gateway (the server) looks up in a central directory (like Eureka or Consul) to find a healthy service instance before forwarding the request. The client doesn’t know this is happening.</li><li><strong>Client-Side Discovery<br></strong>In this <strong>client-side</strong> pattern, the client first asks the directory for a service address and then makes a direct call. This is less common when a gateway is used.</li></ul><h3>The Safety Net: Resilience and Fault Tolerance Patterns (Client-Side)</h3><p>Failure is going to happen. These patterns ensure that a failure in one part doesn’t cause the whole system to crash.</p><h4><strong>Timeout</strong></h4><p>The simplest pattern. Stop waiting for a response from another service after a set amount of time. This frees up your application so it doesn’t wait forever.</p><h4><strong>Retry with Exponential Backoff and Jitter</strong></h4><p>When a request fails, don’t retry immediately. <strong>Exponential Backoff</strong> increases the wait time between each retry (e.g., 2s, 4s, 8s), giving a struggling service a chance to recover. Adding <strong>Jitter</strong> (a small, random delay) stops all the retries from happening at once.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*vTPH-yU3nbbArSVjVEjhpg.png" /></figure><h4><strong>Hedging</strong></h4><p>For systems that need to be extremely fast, send an initial request. If you don’t get a response within a short time limit (e.g., 50ms), send a duplicate request to another instance. Use whichever response comes back first. This uses more resources but makes things faster.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/963/1*sVIzTB5StgEo6R6aHDSoMQ.png" /></figure><h4><strong>Bulkhead</strong></h4><p>This pattern separates your resources to stop failures from spreading. Just as a ship has sealed compartments (bulkheads) to contain a leak, you can give separate worker pools to different services. If the “Product Service” slows down and uses up all its workers, the workers for the “Payment Service” are still free, stopping one problem from crashing everything else.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LeDYNbrRucTv1NPuGJmp8A.png" /></figure><h4><strong>Rate Limiting vs. Load Shedding</strong></h4><ul><li><strong>Rate Limiting (Server-Side):</strong> A fixed rule you set for clients. If they send too many requests (e.g., over 100 per second), you turn them away.</li><li><strong>Load Shedding (Server-Side):</strong> This is self-defense. The service watches its own health (CPU/memory). If it gets too busy, it starts turning away new requests to avoid crashing.</li></ul><h4><strong>Deployment Pattern— Backend for Frontend (BFF)</strong></h4><p>Instead of a single API for everyone, a BFF is a special <strong>server-side</strong> backend service built just for one frontend application. It calls other core microservices and combines the data into the exact format the frontend needs. This separates the user interface from the main services.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*TgkuryJa2V1P3y0YpaZaJQ.png" /></figure><p>These patterns work together to turn a fragile system into a strong and reliable application.</p><h4><strong>References and Further Reading:</strong></h4><ul><li><a href="https://docs.aws.amazon.com/prescriptive-guidance/latest/modernization-integrating-microservices/introduction.html">Integrating microservices by using AWS serverless services</a></li><li><a href="https://www.geeksforgeeks.org/system-design/difference-between-forward-proxy-and-reverse-proxy/">Difference between Forward Proxy and Reverse Proxy - GeeksforGeeks</a></li><li><a href="https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/retry-backoff.html">Retry with backoff pattern</a></li><li><a href="https://grpc.io/docs/guides/request-hedging/">Request Hedging</a></li><li><a href="https://learn.microsoft.com/en-us/azure/architecture/patterns/backends-for-frontends">Backends for Frontends Pattern - Azure Architecture Center</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=63a3211b68d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Modern Service Architecture Explained: SOA to Microservices with Key Patterns]]></title>
            <link>https://medium.com/@bharishx/modern-service-architecture-explained-soa-to-microservices-with-key-patterns-3446d2da3883?source=rss-0311e3ceba7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/3446d2da3883</guid>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[modern-software-delivery]]></category>
            <category><![CDATA[software-design-patterns]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[software-architecture]]></category>
            <dc:creator><![CDATA[Harish Balakrishnar]]></dc:creator>
            <pubDate>Fri, 12 Sep 2025 04:50:31 GMT</pubDate>
            <atom:updated>2025-09-14T12:17:01.986Z</atom:updated>
            <content:encoded><![CDATA[<p>Software architecture has changed over time, moving from big, all-in-one systems to smaller, independent services. Understanding this change is key to building modern apps that can grow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PE2he9WPaGvNM2xpPb4ExQ.png" /></figure><p>Let’s look at this shift and the design patterns that make it work.</p><h3>The Old World: Service-Oriented Architecture (SOA)</h3><p>In the past, businesses struggled to connect different systems — a SOAP service, an FTP server, and a partner’s REST API — all needed to talk. The solution was the <strong>Enterprise Service Bus (ESB)</strong>, a powerful central middleware. The ESB acted as a universal translator, handling<strong> protocol switching</strong> (HTTP to FTP), <strong>message translation </strong>(JSON to XML), and <strong>data transformation</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pD9a0dXO0iY9HKhH9jycrA.png" /></figure><p>This model, known as Service-Oriented Architecture (SOA), followed the principle of an Intelligent Pipe and Dumb Backend. All the complex business logic was implemented in the ESB, while the backend services were simple, single-task performers.</p><h3>The New Paradigm: Microservices</h3><p>The microservices architecture flipped this model, following the idea of an Intelligent Backend and a Dumb Pipe. The business logic moved out of the center and into the services themselves. Each microservice is built around a specific business capability, or <strong>bounded context</strong>, following the Single Responsibility Principle. An <em>“Order Service”</em> only knows about orders, making it independently deployable and easier to maintain.</p><h3>Patterns for Service Collaboration</h3><p>When services must work together, specific patterns manage how they work together, often led by an <strong>Aggregator</strong> pattern.</p><h4><strong>Scatter/Gather</strong></h4><p>The aggregator sends requests to multiple services at the same time (scatters) and then combines their responses (gathers). This is great for tasks like getting flight prices from several airline services at once.</p><h4><strong>Chaining</strong></h4><p>Services are called one after another, where the output of one service is the input for the next. This can be done in two ways:</p><ul><li><strong>Delegation:</strong> Service A calls Service B, which then calls Service C.</li><li><strong>Orchestration:</strong> a central aggregator service calls A, gets the response, then calls B with that data.</li></ul><h4><strong>Branching</strong></h4><p>The flow of requests changes based on certain rules.</p><ul><li><strong>Conditional Branching:</strong> The aggregator picks which service to call based on the request data (e.g., if department is “Sales,” call the Sales Service). This is also known as decision based branching.</li><li><strong>Selective Aggregation:</strong> The aggregator calls multiple services but only uses data from some of them depending on the situation (e.g., a checkout page needs Inventory and Price data but ignores the Review service).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7wVlj2Ndf0rzY0zKv0GhBA.png" /></figure><h3>Patterns for Resilience and Evolution</h3><h4><strong>The Circuit Breaker Pattern</strong></h4><p>In a distributed system, a single service failure can cause a <strong>cascading failure</strong>, where other services that depend on it also fail one by one. The Circuit Breaker pattern stops this. It watches calls to a service and works in three states:</p><ul><li><strong>Closed:</strong> Requests pass through normally. If too many fail, it trips.</li><li><strong>Open:</strong> The circuit is open. All requests are immediately blocked and get a fallback response, protecting the system.</li><li><strong>Half-Open:</strong> After a cooldown, it lets a few test requests through. If they succeed, the circuit closes; if not, it stays open.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/944/1*1xxCL3_8V6uUYogmDGHSSw.png" /></figure><p>Circuit breaker simply ensures you don’t fail just because others fail. It also helps the failing system recover instead of being overloaded.</p><h4><strong>The Strangler Fig Pattern</strong></h4><p>Updating a live service without breaking clients is a big challenge. The Strangler Fig pattern allows for a gradual change. You deploy the new service (V2) alongside the old one (V1).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*hPXcD4ACfTG51bIRcwpklw.png" /></figure><p>A router first sends all traffic to V1, then you slowly shift traffic to V2. Once V1 gets no more traffic, it can be safely shut down, making the change a smooth one.</p><p>Using these patterns helps us build strong, distributed systems that can handle failures and be updated easily.</p><h4><strong>References and Further Reading:</strong></h4><ul><li><a href="https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/scatter-gather.html">Scatter-gather pattern</a></li><li><a href="https://learn.microsoft.com/en-us/azure/architecture/patterns/circuit-breaker">Circuit Breaker Pattern - Azure Architecture Center</a></li><li><a href="https://martinfowler.com/bliki/StranglerFigApplication.html">bliki: Strangler Fig</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3446d2da3883" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Core Principles of Microservice Architecture]]></title>
            <link>https://medium.com/@bharishx/the-core-principles-of-microservice-architecture-17ccbb75c810?source=rss-0311e3ceba7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/17ccbb75c810</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[principles]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Harish Balakrishnar]]></dc:creator>
            <pubDate>Thu, 11 Sep 2025 03:16:52 GMT</pubDate>
            <atom:updated>2025-09-11T03:16:52.017Z</atom:updated>
            <content:encoded><![CDATA[<p>Microservices are a modern way of building applications, but despite their popularity, they are often implemented incorrectly. The key isn’t which framework you use, but whether you understand the core ideas. Changing the architecture without understanding these ideas often leads to a “distributed monolith” — a system that has all the old problems plus new ones introduced by being distributed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wk2GES3OCp4K5AsppWR3Zg.png" /></figure><p>This article explains the essential concepts of microservices, focusing on why they exist and the rules that make them work.</p><h3>The Problem with Monolithic Architecture</h3><p>Before microservices, applications were built as one big system. All parts of the application (Inventory, Payments, etc.) were bundled together in one package, like a WAR file. This caused big problems as the system and team got bigger.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1skwjAujc_qka5CozCTjmg.png" /><figcaption>Monolithic vs Microservices Overview</figcaption></figure><p>The biggest issue was the <strong>“release train.”</strong> Imagine a system where ten teams are working. If one team’s feature is delayed for a week, the entire release is blocked. A finished and tested feature has to wait weeks because of unrelated, unfinished work.</p><p>Deploying a monolith was a high-risk, all-or-nothing process. A small change required rebuilding the whole application, which could take hours or days. If a bug was found after deployment, rolling back was just as hard.</p><h3>The Three Pillars of a Microservice</h3><p>Microservices solve these problems by breaking a big application into smaller, independent services. This idea is guided by a concept called Domain-Driven Design (DDD). Each service has a clear boundary around its specific business area, called a <strong>Bounded Context</strong>. A true microservice follows three key rules.</p><ol><li><strong>It Has a Single Purpose</strong> <br>A microservice should “do one thing and do it well.” An <strong>Item Service</strong>, for example, only manages item data. A separate <strong>Order Service</strong> handles creating orders. It gets item information by communicating with the Item Service through its API; it doesn’t manage item data itself.</li><li><strong>It Is Independently Scalable</strong> <br>Different services handle different amounts of traffic. An exam results website may get a huge spike in traffic to its “result search” service when results are out. With microservices, you can scale up only that service by adding more instances, while other services run normally. This saves resources and money.</li><li><strong>It Is Independently Deployable</strong> <br>This is the most important rule. You can deploy a change to one service without touching any other service. This breaks the monolithic release train and lets teams deliver new features to users much faster.</li></ol><h3>Achieving True Independence: Code and Data</h3><p>To be truly independent, services need clear separation for their code and data.</p><h4><strong>Code Management: Poly-repo is Ideal</strong></h4><ul><li><strong>Poly-repo:</strong> Each microservice has its own code repository and its own deployment pipeline. This approach is usually the best because it allows independent deployments that can be completed in minutes.</li><li><strong>Mono-repo:</strong> All services live in a single repository. This often reintroduces the release train problem, where slow pipelines can make deployments take hours or even days.</li></ul><h4><strong>Data Management: You Own Your Data</strong></h4><p>A service must have its own database. Services should <strong>never</strong> access another service’s database directly. All communication must happen through APIs. This stops one service’s problem from affecting others. For example, if a busy Order Service uses up all its database connections, the Item Service will not fail because it has its own separate database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*PH6YRh72-JhLWxXfHu_aEg.png" /></figure><p>In practice, a <strong>database-per-domain</strong> is a good compromise. Services in the same domain (like HR) can share a database, making it easier to manage. However, a very busy service might still get its own database to keep it completely separate.</p><h4>Team Structure: Small, Autonomous, Full-Stack Teams</h4><p>Microservices also change how teams are structured. The best team is a small <strong>“two-pizza team”</strong> (around 8–10 people). These are <strong>full-stack teams</strong>, meaning the team has all the skills (backend, frontend, QA, etc.) to own their service completely, from the first idea to production. The independence provided by these features is key to achieving the speed and flexibility that microservices offer.</p><p>By following core principles like single purpose, independent deployment, and data ownership, a microservice architecture effectively solves the rigidity of monolithic systems.</p><p><strong>References and Further Reading:</strong></p><ul><li><a href="https://microservices.io/patterns/index.html">Microservices Pattern: A pattern language for microservices</a></li><li><a href="https://aws.amazon.com/microservices/">Microservices</a></li><li><a href="https://www.geeksforgeeks.org/system-design/microservices/">What are Microservices? - GeeksforGeeks</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17ccbb75c810" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Guide to Java’s Class Loader and Delegation Model]]></title>
            <link>https://medium.com/@bharishx/a-guide-to-javas-class-loader-and-delegation-model-1ea44bb0c7c1?source=rss-0311e3ceba7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ea44bb0c7c1</guid>
            <category><![CDATA[delegation]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[jvm]]></category>
            <category><![CDATA[class-loader]]></category>
            <dc:creator><![CDATA[Harish Balakrishnar]]></dc:creator>
            <pubDate>Tue, 09 Sep 2025 07:27:47 GMT</pubDate>
            <atom:updated>2025-09-09T07:27:47.974Z</atom:updated>
            <content:encoded><![CDATA[<p>When a Java application is executed, the Java Virtual Machine (JVM) loads class files dynamically as they are needed. This task is managed by a system called the Class Loader, which operates on a strict hierarchy and a principle known as the <strong><em>parent-first delegation model</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKcCE-Mjot90VzXgs2re-g.png" /></figure><h3>The Class Loader Hierarchy</h3><p>The JVM organizes class loaders into a parent-child hierarchy, with each level having a specific duty.</p><ol><li><strong>Bootstrap Class Loader</strong> <br>At the top of the hierarchy, the Bootstrap Class Loader is an integral part of the JVM. It loads the core Java APIs from <em>rt.jar, </em>including fundamental classes like <em>java.lang.Object.</em> Since it&#39;s implemented in native code, asking a class it loaded for its class loader will return <em>null</em>.</li><li><strong>Extension Class Loader (Java 8) / Platform Class Loader (Java 9+)<br></strong>This loader is the child of the Bootstrap loader. In Java 8, it loaded classes from the extensions directory <em>(&lt;JAVA_HOME&gt;/jre/lib/ext)</em>. In Java 9 and later, it was replaced by the <strong>Platform Class Loader</strong> to work with the Java module system.</li><li><strong>Application (or System) Class Loader</strong> <br>As the child of the Extension loader, this is the loader for your application. It finds and loads classes from the application’s classpath.</li><li><strong>Custom Class Loader</strong> <br>Developers can also create their own class loaders to load classes from non-standard sources, like a network or database.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yyvvFi0VLAhQTFGB9eCFeA.png" /></figure><h3>The Parent-First Delegation Model</h3><p>This model dictates the rules for loading classes within the hierarchy. When a class loader receives a request to load a class, it first delegates the task to its parent. The request travels up the hierarchy to the Bootstrap Class Loader. A loader will only attempt to find and load a class itself if its parent (and all ancestors) could not.</p><p>This mechanism prevents the same class from being loaded multiple times and safeguards core Java classes from being replaced by malicious code.</p><h3>The Delegation Model in Practice</h3><p>Let’s observe this model with a practical example using two classes, <em>Employee </em>and <em>Main</em>.</p><pre>// Employee.java<br>public class Employee {<br>    public void printClassLoader() {<br>        System.out.println(&quot;Employee class loaded by: &quot; + this.getClass().getClassLoader());<br>    }<br>}</pre><pre>// Main.java<br>public class Main {<br>    public static void main(String[] args) {<br>        Employee employee = new Employee();<br>        employee.printClassLoader();<br>    }<br>}</pre><h4>Step 1: Default Loading via Application Class Loader</h4><p>First, we compile and run the code. The Application Class Loader gets the request to load <em>Employee.class.</em> It delegates the request upwards. Neither the Extension nor the Bootstrap loader can find the class. The request then returns to the Application Class Loader, which locates <em>Employee.class </em>on the classpath and loads it.</p><pre>javac Employee.java Main.java<br>java Main</pre><p><strong>Output :</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*yMsDDYOEhLPp_VUcSnebrw.png" /></figure><h4>Step 2: Prioritizing the Extension Class Loader</h4><p>Next, we package our <em>employee </em>class into <em>employee.jar</em> and move it to the Java 8 extension directory (<em>&lt;JAVA_HOME&gt;\jre\lib\ext </em>).</p><pre>jar cf employee.jar Employee.class<br>move employee.jar &quot;C:\Program Files\Java\jdk8\jre\lib\ext&quot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/827/1*j1DxFIfq0VvBIRFENSlJWQ.png" /></figure><p>When we run the application again, the delegation process starts. This time, the Extension Class Loader finds <em>employee.jar </em>in its designated path and loads the class. The search concludes here, and the Application Class Loader is never involved.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/1*7-g8YgS12QwttVWXsou0ug.png" /></figure><h4>Step 3: Testing the Bootstrap Class Loader</h4><p>Finally, let’s see what happens if we place employee.jar in the core Java library directory ( <em>&lt;JAVA_HOME&gt;\jre\lib</em> ), where <em>rt.jar</em> is located.</p><pre>move employee.jar &quot;C:\Program Files\Java\jdk8\jre\lib&quot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/920/1*9vGwk4AryrfDVXKhl9QV4g.png" /></figure><p>Running<em> java Main</em> again shows the class is still loaded by the Application Class Loader.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*cD7DxkI3VdwwIeEnrWifUw.png" /></figure><p>This is because the Bootstrap Class Loader is hard-coded to load classes only from specific, trusted files like <em>rt.jar</em>. For security, it ignores any other files or JARs in its directory.</p><p>The request to load <em>Employee </em>travels up to the Bootstrap loader, which doesn’t find it in its trusted sources. The request then falls back down the hierarchy until the Application Class Loader finds the class on the original classpath.</p><h4>Complete Delegation flow.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0XxZaxqLtGg_Oi6DJDphyw.png" /></figure><ol><li>A request to load a class is made.</li><li>The Application Classloader delegates the request to the Extension Classloader.</li><li>The Extension Classloader delegates the request to the Bootstrap Classloader.</li><li>The Bootstrap Classloader searches for the class in the core Java libraries <em>(rt.jar).</em></li><li>If found, the Bootstrap Classloader loads it; if not, control passes down.</li><li>The Extension Classloader searches for the class in the extensions directory <em>(lib/ext)</em>.</li><li>If found, the Extension Classloader loads it; if not, control passes down.</li><li>The Application Classloader searches its classpath. If found, it’s loaded; otherwise, a `ClassNotFoundException` is thrown.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xbqp3BYHTY0sKfrwrFQFUg.png" /></figure><p>This strict, hierarchical delegation ensures that the Java runtime remains both stable and secure.</p><p><strong>References and Further Reading:</strong></p><ul><li><a href="https://www.geeksforgeeks.org/java/classloader-in-java/">ClassLoader in Java - GeeksforGeeks</a></li><li><a href="https://www.digitalocean.com/community/tutorials/java-classloader">Java ClassLoader | DigitalOcean</a></li></ul><p><a href="https://www.baeldung.com/java-classloaders">https://www.baeldung.com/java-classloaders</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ea44bb0c7c1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Catch Me If You Can: Exception Handling in Java.]]></title>
            <link>https://medium.com/@bharishx/catch-me-if-you-can-exception-handling-in-java-c0c0f9edb125?source=rss-0311e3ceba7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0c0f9edb125</guid>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[exception-handling]]></category>
            <dc:creator><![CDATA[Harish Balakrishnar]]></dc:creator>
            <pubDate>Mon, 25 Aug 2025 12:00:48 GMT</pubDate>
            <atom:updated>2025-08-25T12:00:48.744Z</atom:updated>
            <content:encoded><![CDATA[<p>In programming, we write instructions for a computer to follow. This sequence of instructions is the program’s normal flow. However, disruptions can occur. These are <em>“out of order”</em> events that break the normal flow. These events are called <strong>Exceptions.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XDSStGS2rciVf2PDC7vyew.png" /></figure><h3>Exceptions vs. Errors</h3><p>Disruptions in a program come in two main forms: Exceptions and Errors.</p><ul><li><strong>Exception </strong>is a recoverable problem. <br>Your program can anticipate these events and has a plan to handle them and return to a stable state.<br>e.g. — a file not being found or a network connection dropping.</li><li><strong>Error</strong> is an unrecoverable failure. <br>There is nothing your program can do to fix the situation.<br>e.g. — running out of memory (<em>OutOfMemoryError</em>) or a critical JVM failure.</li></ul><p>Because we cannot control errors, we don’t try to handle them. Our focus is on handling exceptions.</p><h3>The Unhandled Exception: A Chain Reaction</h3><p>When an exception occurs in a method and it is not handled, the program doesn’t stop immediately. Instead, the exception is <em>&quot;thrown&quot; </em>as an object, up to the method that called it. This is called <strong>exception propagation </strong>or <em>&quot;bubbling up.&quot;</em></p><p>Consider this call stack: <br>The main method calls <em>processOrder()</em>, which in turn calls<em> findItem().</em></p><pre>public class Store {<br>    public static void main(String[] args) {<br>        processOrder();<br>    }<br><br>    public static void processOrder() {<br>        System.out.println(&quot;Processing order...&quot;);<br>        findItem(&quot;item-123&quot;);<br>        System.out.println(&quot;Order processed.&quot;); // Never reached here.<br>    }<br><br>    public static void findItem(String itemId) {<br>        System.out.println(&quot;Finding item: &quot; + itemId);<br><br>        // Let&#39;s assume the item is not in the database.<br>        throw new RuntimeException(&quot;ItemNotFound&quot;);<br>    }<br>}</pre><ol><li><em>findItem()</em> throws a <strong>RuntimeException</strong>.</li><li>Since<em> findItem()</em> doesn’t handle it, the exception propagates to <em>processOrder()</em>.</li><li><em>processOrder()</em> doesn’t handle it either, so it propagates up to <em>main().</em></li><li><em>main()</em> is the top of the stack. Since it doesn’t handle the exception, the <strong>program crashes.</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sfx6JlK46ZSbufHHMwac_w.png" /></figure><p>To prevent a crash, we must <em>“catch”</em> the exception before it reaches the top.</p><h3>Handling Exceptions with try-catch</h3><p>We use a try-catch<em> </em>block to handle exceptions. You literally<em> &quot;try&quot; </em>a piece of code. If an exception is thrown, it is <em>&quot;caught&quot; </em>by the catch block, where you can write recovery logic.</p><blockquote>Exception handling is more than just catching the exception.</blockquote><pre>try {<br>    // some code that might fail<br>} catch (Exception e) {<br>    throw e;<br>}</pre><p>This is not handling. You caught the exception only to immediately throw it again. You haven’t done anything to recover.</p><pre>// WORSE: Swallowing the exception<br>try {<br>    // ...<br>} catch (Exception e) {<br>    // The problem is now hidden forever<br>}</pre><p>This is also bad practice. An empty catch block <em>“swallows” </em>the exception. The program continues as if nothing went wrong, but the problem is hidden, making debugging a nightmare.</p><blockquote>True handling means recovering from the problem.</blockquote><p>For example, if a <em>FileNotFoundException </em>occurs while trying to write to a log file, true handling would be to create the file and then continue writing.</p><pre>try {<br>    writeToFile(&quot;log.txt&quot;, &quot;User logged in.&quot;);<br>} catch (FileNotFoundException e) {<br>    // Recovery -- create the file and try again.<br>    createNewFile(&quot;log.txt&quot;);<br>    writeToFile(&quot;log.txt&quot;, &quot;User logged in.&quot;);<br>}</pre><h3>Checked vs. Unchecked Exceptions</h3><p>In Java, exceptions are categorized into two types:</p><ul><li><strong>Checked Exceptions:</strong> These are for predictable problems the compiler forces you to handle (e.g., <em>IOException</em>). You must either use a <strong>try-catch</strong> block or declare that your method <strong>throws </strong>it. The compiler enforces this because these exceptions often relate to external resources (files, networks)</li><li><strong>Unchecked (Runtime) Exceptions:</strong> The compiler does not force you to handle these. These are normally programming errors, like a <em>NullPointerException </em>or <em>ArrayIndexOutOfBoundsException</em>.</li></ul><pre>// Checked Exception <br>public void readFile() {<br>    try {<br>        FileReader reader = new FileReader(&quot;somefile.txt&quot;);<br>    } catch (FileNotFoundException e) { <br>        System.out.println(&quot;File could not be found.&quot;);<br>        // some kind of logging can be done here.<br>    }<br>}<br><br>// Unchecked Exception (optional handling)<br>public void printLength(String text) {<br>    if (text == null) {<br>        System.out.println(&quot;Input text is null.&quot;);<br>        return;<br>    }<br>    System.out.println(text.length());<br>}</pre><h3>Where to Handle: Let It Bubble Up</h3><p>It’s often best to handle exceptions based on the context. A low-level method (e.g., in a database layer) doesn’t know how to present an error to a user. The best practice is <strong>“Throw Early, Catch Late.”</strong></p><p>Let the exception propagate up to a layer that has the right context to handle it, like a Controller that can return a proper HTTP error response.</p><p>To pass a checked exception up the call stack, you use the <em>throws </em>keyword.</p><pre>//--------------REPOSITORY LAYER---------------<br>// this doesn&#39;t know how to handle this. It throws it.<br>public class UserRepository {<br>    public User findById(String id) throws DatabaseDownException {<br>        // Code to connect to database<br>        if (databaseIsDown) {<br>            throw new DatabaseDownException(&quot;Cannot connect to database&quot;);<br>        }<br>        // remaining code.....<br>    }<br>}<br><br><br>//-------------------SERVICE LAYER-----------------H<br>// also passes it up.<br>public class UserService {<br>    public User getUser(String id) throws DatabaseDownException {<br>        return userRepository.findById(id);<br>    }<br>}<br><br><br><br><br>//-----------------CONTROLLER LAYER---------------------- <br>// has the context to handle it.<br>public class UserController {<br>    public void displayUser(String id) {<br>        try {<br>            User user = userService.getUser(id);<br>            // display user<br>        } catch (DatabaseDownException e) {<br>            // Handle by showing an error message to the user.(CATCH LATE)<br>            showErrorPage(&quot;The database is currently unavailable.&quot;);<br>        }<br>    }<br>}</pre><h3>Re-throw &amp; Wrapping Exceptions</h3><p>Sometimes, when you catch an exception, you might want to throw a different, more meaningful one. This is called <strong>re-throwing</strong>. For example, you might catch a low-level exception and throw a high-level one that makes more sense for your program.</p><p>When doing this, it’s best to wrap the original exception (possibly in a custom exception) so the stack trace is preserved for debugging. A good practice is to handle exceptions where they are most relevant.</p><pre>catch (SQLException e) {<br>    // Pass the original cause &#39;e&#39; to the new exception.<br>    throw new DataAccessException(&quot;Failed to retrieve user data&quot;, e);<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iLq74ONqPjbFNtKKj_D31Q.png" /><figcaption>Rethrowing exception for better context handling</figcaption></figure><h3>The “finally” Block</h3><p>A finally block mostly executes after a try or try-catch block, regardless of whether an exception occurs, and is used for cleanup tasks like closing files or database connections. However it may not execute in rare cases like a JVM crash (e.g., <em>OutOfMemoryError</em>) or if the thread is terminated.</p><pre>Connection dbConnection = null;<br>try {<br>    dbConnection = openConnection();<br><br>    // ... data base code...<br><br>} catch (SQLException e) {<br>    // handle exception<br>} finally {<br>    if (dbConnection != null) {<br>        dbConnection.close(); <br>    }<br>}</pre><p><strong>Critical Rule:</strong> Do not throw an exception from a finally block. If the try block threw an exception, and the finally block also throws one, the original (and likely more important) exception will be “<em>suppressed” </em>and lost.</p><h3>Best Practices in Handling Exceptions.</h3><ol><li><strong>Don’t Catch If You Cannot Handle It:</strong> If you don’t have the context to recover, let the exception propagate up.</li><li><strong>Catch the Most Specific Exception:</strong> Instead of <em>catch(Exception e) </em>, catch <em>FileNotFoundException </em>. This makes your handling logic precise.</li><li><strong>Do Not Catch Errors:</strong> You can’t recover from an <em>OutOfMemoryError </em>, so don&#39;t even try.</li><li><strong>Do Not Swallow Exceptions:</strong> Never use an empty <em>catch </em>block.</li><li><strong>Throw Early, Catch Late:</strong> Throw exceptions at the source of the problem and catch them at a high level that can manage the recovery with context (e.g., user notification).</li><li><strong>Do Not Throw from a <em>finally </em>Block:</strong> This can suppress the original, more important exception.</li><li><strong>Preserve the Original Exception When Re-throwing:</strong> Use exception wrapping to maintain the full stack trace.</li><li><strong>Do Not Both Log and Throw everywhere:</strong> Let the exception propagate and log it once at the top level where it is finally handled to avoid cluttered, repetitive logs.</li></ol><p>Exception handling isn’t just about stopping crashes, It is also about creating reliable, easy-to-maintain, and debug-friendly software. By managing errors smoothly, you build apps that can handle unexpected problems and improve the experience for users and developers.</p><p><strong>References and Further Reading:</strong></p><ul><li><a href="https://www.geeksforgeeks.org/java/exceptions-in-java/">Java Exception Handling - GeeksforGeeks</a></li><li><a href="https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html">Lesson: Exceptions</a></li><li><a href="https://blog.jetbrains.com/idea/2024/04/easy-hacks-how-to-handle-exceptions-in-java/">Easy Hacks: How to Handle Exceptions in Java | The IntelliJ IDEA Blog</a></li></ul><p><a href="https://www.baeldung.com/java-exceptions">https://www.baeldung.com/java-exceptions</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0c0f9edb125" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Garbage Collection in Java]]></title>
            <link>https://medium.com/@bharishx/garbage-collection-in-java-5ff57b1d54b8?source=rss-0311e3ceba7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ff57b1d54b8</guid>
            <category><![CDATA[garbage-collection]]></category>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Harish Balakrishnar]]></dc:creator>
            <pubDate>Thu, 21 Aug 2025 10:50:49 GMT</pubDate>
            <atom:updated>2025-08-24T05:40:33.190Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the most powerful features in Java is automatic memory management. Unlike C / C++ where memory handling is done manually, Java does the memory cleanup for you. It runs in the background, reclaiming memory from unused objects to help reduce the risk of the Java application running out of memory. This cleanup process is called <strong><em>“Garbage collection”</em></strong>. But how does it actually work?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jZ4nDCMGMM75pETeQzuxaQ.png" /></figure><h3><strong>The Problem: <em>Heap of Garbage</em></strong></h3><p>Firstly, where does our program’s data live? <br>When you create an object in Java, it gets placed in a large memory area called the <strong>Heap</strong>.</p><pre>User user = new User(&quot;Bob&quot;);<br>// &quot;new User()&quot; creates the object in Heap.<br>// &quot;user&quot; variable is only a reference pointing to the REAL object in Heap.</pre><p>Now, what happens if we change the reference?</p><pre>user = null; // The reference is gone.</pre><p>The original <em>User(“Bob”) </em>object is now floating in the Heap, <strong>unreachable</strong>. No part of our active program can get to it. <strong>This is garbage.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/933/1*i7ZX7ramX5Jhdz3fICNkig.png" /></figure><p><strong>Note — </strong>objects that refer to each other but have no external references are also considered garbage (recursive references)</p><p>Without a cleanup process, the Heap would eventually fill with this garbage, and our application would crash. The Garbage Collector (GC) is the automated solution to this problem.</p><p>Before the garbage collector runs, it usually has to briefly pause the entire application in what’s known as a <em>Stop-The-World (STW) pause</em>, so the JVM can safely clean up memory without conflicts.</p><p>(Note — Some newer collectors like ZGC and Shenandoah reduce this pause to be almost unnoticeable.)</p><blockquote>The goal of every GC algorithm is to make STW pauses as short as possible.</blockquote><h3>The Big Idea: Most Objects Die Young</h3><p>Instead of constantly checking every single object in the entire Heap, Java engineers made an important observation through their development experience, called the <strong><em>Generational Hypothesis</em></strong>:</p><blockquote>Most objects become unreachable very soon after they are created.</blockquote><p>For example : In a web request, objects are created for the request, for processing, and for the response; after that, they’re all useless.</p><p>This insight led to splitting the Heap into two main areas:</p><ol><li><strong>The Young Generation</strong>: Where all new objects are born. This area is collected (and removed when unreachable) very frequently in a process called a <strong>Minor GC</strong>.</li><li><strong>The Old Generation</strong>: If an object survives enough Minor GCs, it’s considered long-lived and gets moved here. This area is collected much less frequently in a <strong>Major GC</strong>.</li></ol><p>The Young Generation itself is divided into three parts:</p><ul><li><strong>“Eden”</strong>: The birthplace for all new objects.</li><li>Two “<strong>Survivor Spaces” (S0 and S1)</strong>: Temporary holding areas for objects that survive a GC cycle.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*_uRSXjTUKo2T3mM_sJ4wUA.png" /></figure><p><strong>Note </strong>— Before Java 8, class info was kept in <strong>PermGen</strong>, a fixed-size heap area that could run out of space. Now it’s in <strong>Metaspace</strong>, which grows automatically, so we are not considering Metaspace.</p><p><strong>So, the lifecycle of a new object would be as follows:</strong></p><ol><li>A new object is created in Eden.</li><li>Eventually, Eden fills up, triggering a <strong>Minor GC</strong>.</li><li>The GC finds all live objects in Eden and <em>copies</em> them to one of the survivor spaces (S0). The entire Eden space is then wiped clean. This is very fast because most objects are expected to be garbage and are simply ignored.</li><li>The application continues, and Eden fills up again.</li><li>Another Minor GC occurs. This time, the GC finds all live objects in <em>both</em> Eden and S0 and copies them to the <em>other</em> survivor space (S1).</li><li>This process repeats, with live objects moving back and forth between S0 and S1. Each time an object survives, its internal age counter goes up.</li><li>Once an object’s age reaches a certain <strong>threshold</strong> (e.g., 15 cycles), it is <strong>promoted</strong>: instead of being copied to a survivor space, it’s copied to the Old Generation.</li></ol><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F_AfWx6GmK7w%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D_AfWx6GmK7w&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F_AfWx6GmK7w%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/87626e557f17a3f7096ea5630686bea9/href">https://medium.com/media/87626e557f17a3f7096ea5630686bea9/href</a></iframe><p>This generational approach is better for Garbage collection. The frequent <strong>Minor GCs</strong> are quick and clean up the vast majority of objects, leaving the expensive <strong>Major GC </strong>to run only when necessary.</p><h3><strong>The GC Toolkit: Mark, Sweep, and Copy</strong></h3><p>Before we go into specific algorithms, let’s understand how does a GC know an object is “unreachable” to start the cleanup?</p><p>It starts by identifying everything that is <strong>definitely <em>in use</em></strong> and assumes the rest is garbage.</p><h4><strong>GC Roots: The Starting Point</strong></h4><p>The GC can’t check every variable in your program. Instead, it starts from a set of known, active references called <strong>GC Roots</strong>. These are the anchors of your application’s object graph and include:</p><ul><li>Local variables in the currently executing methods (on the thread stacks).</li><li>Active threads themselves.</li><li>Static variables of classes.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*87b82jOGQ5FqGSROsvISmA.png" /></figure><p>Anything that can be reached by following a chain of references from a GC Root is considered a <strong>live object</strong>. Everything else is garbage.</p><h4>Strategy 1: <strong>The Mark-Sweep-Compact</strong></h4><p>This is a three-phase algorithm, primarily used in the Old Generation.</p><ol><li><strong>Mark</strong>: The collector starts at the GC Roots and traverses every connected object, marking each one it visits as “live”. We can think of it as painting every reachable object green.</li><li><strong>Sweep</strong>: The collector scans the entire heap from start to finish. If it finds an object that wasn’t marked (not green), it knows it’s garbage and reclaims its memory. (Frees up the memory)</li><li><strong>Compact</strong>: After sweeping, the memory can be fragmented, with gaps of free space between live objects. The collector moves all the live objects together into a single block. (Removes fragmentation)</li></ol><h4>Strategy 2: <strong>The Mark-Copy</strong></h4><p>This is a more efficient approach used in the Young Generation.</p><ol><li><strong>Mark and Copy</strong>: The collector divides the space into two halves (e.g., Eden and a Survivor space). It starts from the GC Roots, finds all live objects in the active half, and copies them to the other, empty half.</li><li><strong>Wipe</strong>: Once all live objects have been evacuated, the entire first half is considered empty and can be wiped clean in one go.</li></ol><p><strong><em>Why use this in the Young Generation?</em> </strong><br>Because most objects here are expected to be garbage. It’s much faster to copy the few survivors than to sweep over all the dead ones.</p><h3>The Algorithms: The Cleaning Crew</h3><p>Let’s look at how different garbage collectors use the above-mentioned strategies as building blocks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*tDzioG3Tv2KzAKWk5h5tTQ.gif" /><figcaption>Basic algorithms: [Green = application running, Red = GC pause]</figcaption></figure><h4><strong>1) The Serial GC</strong></h4><p>The simplest GC. It uses a single thread to do all the work and freezes the application while it runs.</p><p><strong>How It Works:</strong></p><p><strong>For the Young Generation (Minor GC):</strong></p><ol><li><strong>Trigger</strong>: Eden space becomes full.</li><li><strong>Stop-the-World Pause</strong>: The application is completely frozen to guarantee that the state of the heap doesn’t change while the GC is figuring out which objects are alive.</li><li><strong>Mark and Copy</strong>: The single GC thread uses the <strong>Mark-Copy</strong> strategy. It identifies live objects in the Young Generation and copies them to a survivor space (or promotes them).</li><li><strong>Resume</strong>: The pause ends, and the application continues.</li></ol><p><strong>For the Old Generation (Major GC):</strong></p><ol><li><strong>Trigger</strong>: The Old Generation is nearly full.</li><li><strong>Stop-the-World Pause</strong>: The application freezes again.</li><li><strong>Mark-Sweep-Compact</strong>: The single GC thread uses the full <strong>Mark-Sweep-Compact</strong> strategy on the Old Generation.</li><li><strong>Resume</strong>: The pause ends.</li></ol><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fo770WSEgmGY&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Do770WSEgmGY&amp;image=http%3A%2F%2Fi.ytimg.com%2Fvi%2Fo770WSEgmGY%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea55eb6d5964cddb70fd16237efa50da/href">https://medium.com/media/ea55eb6d5964cddb70fd16237efa50da/href</a></iframe><p><strong>Best for</strong>: Simple applications with small heaps or on machines with a single CPU core.</p><h4>2) <strong>The Parallel GC: The Team Effort</strong></h4><p>It works just like the Serial GC but uses multiple threads to speed up the collection process. It still freezes the application.</p><p><strong>How It Works:</strong></p><p>The steps are <strong>exactly the same</strong> as the Serial GC, with one crucial difference: the “Mark and Copy” and “Mark-Sweep-Compact” phases are performed by <strong>multiple GC threads working in parallel</strong>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F7L7rKuq2S00%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D7L7rKuq2S00&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F7L7rKuq2S00%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/732f672678cb1c3d03f401849ad0dca6/href">https://medium.com/media/732f672678cb1c3d03f401849ad0dca6/href</a></iframe><p><strong><em>Why does this help?</em> </strong><br>On a modern multi-core machine, using multi-threads reduces the total time the application is paused. This collector prioritizes <strong>throughput</strong> — it gets the cleanup done quickly so the application can un-pause.</p><p><strong>Best for</strong>: Backend applications focused on batch processing or intensive calculations, where overall performance is more important than avoiding short pauses. It was the default in Java 8.</p><h4>3) The Concurrent Mark Sweep (CMS) GC: The Concurrent Specialist</h4><p>A more advanced collector designed to minimize pause times by doing most of its expensive work on the Old Generation <em>at the same time</em> as the application is running.</p><p><strong>How It Works (Step-by-Step for the Old Generation):</strong></p><ol><li><strong>Initial Mark (STW Pause)</strong>: The application is paused, but only for a very short time. The GC threads quickly mark only the objects directly reachable from the GC Roots.</li><li><strong>Concurrent Mark</strong>: The application resumes. Now, the GC threads begin the main <strong>Mark</strong> phase, traversing the entire object graph. This happens <em>concurrently</em>.</li><li><strong>Remark (STW Pause)</strong>: The application is paused again for a second, slightly longer time. The GC threads revisit parts of the heap that were changed by the application during the <em>concurrent marking phase</em> to finalize the list of live objects. This step ensures correctness.</li><li><strong>Concurrent Sweep</strong>: The application resumes. The GC threads now begin the <strong>Sweep</strong> phase, reclaiming memory from garbage objects concurrently.</li></ol><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FvxipH3usPHs%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DvxipH3usPHs&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FvxipH3usPHs%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/13365c0f82dfe631527095cefb286495/href">https://medium.com/media/13365c0f82dfe631527095cefb286495/href</a></iframe><p><strong><em>What’s the issue with this algorithm?</em> </strong><br>CMS performs <strong>Mark</strong> and <strong>Sweep</strong> but skips the <strong>Compact</strong> phase to avoid a long pause. This leads to heap fragmentation over time, which can eventually force a very long stop-the-world pause.</p><p><strong>Best for</strong>: Latency-sensitive applications that can’t tolerate long pauses. (Note: CMS is deprecated from Java version 9 and has been replaced by G1).</p><h4>4) The G1 (Garbage-First) GC</h4><p>The modern, all-purpose default. It divides the heap into hundreds of small regions and intelligently collects the ones with the most garbage first, aiming for shorter pauses.</p><p><strong>How It Works:<br></strong>G1 partitions the entire heap into a grid of small, equal-sized regions. Each region can be Eden, a Survivor space, or an Old generation space.</p><p><strong>The Collection Cycle:</strong></p><ol><li><strong>Concurrent Marking Phase</strong>: G1, like CMS, runs a concurrent <strong>Marking</strong> process in the background to identify how much garbage is in <em>each individual region</em>.</li><li><strong>Choose the Collection Set</strong>: G1 creates a <strong>Collection Set</strong> of regions to clean. This set always includes Young Generation regions but can <em>also</em> include Old Generation regions that are mostly filled with garbage.</li><li><strong>Evacuation Pause (STW Pause)</strong>: The application is paused.</li><li><strong>Mark and Copy</strong>: Multiple GC threads work in parallel to perform a <strong>Mark-Copy</strong> process on the entire Collection Set. They find all live objects and copy them to brand new, empty regions.</li><li><strong>Resume</strong>: The pause ends.</li></ol><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fw7ehOdPwf7s%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dw7ehOdPwf7s&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fw7ehOdPwf7s%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b6833b602c8dd026a4725883175347b/href">https://medium.com/media/9b6833b602c8dd026a4725883175347b/href</a></iframe><p><strong><em>Why is this so effective?<br></em></strong>G1 compacts memory during evacuation and cleans the Old Generation incrementally, a few regions at a time to meet pause-time goals</p><p><strong>Best for</strong>: The default choice for virtually all modern, server-side Java applications, especially those with large heaps.</p><p>From the simple, single-threaded Serial Garbage Collector to the regional management of G1 and the low-pause designs of newer ones like <a href="https://docs.oracle.com/en/java/javase/20/gctuning/z-garbage-collector.html">Z Garbage Collector (ZGC)</a> and <a href="https://developers.redhat.com/articles/2024/05/28/beginners-guide-shenandoah-garbage-collector">Shenandoah</a>, Java’s garbage collectors have steadily improved memory management efficiency.</p><p><strong>References and Further Reading:</strong></p><p><a href="https://www.geeksforgeeks.org/java/garbage-collection-in-java/">https://www.geeksforgeeks.org/java/garbage-collection-in-java/</a></p><p><a href="https://www.baeldung.com/java-choosing-gc-algorithm">https://www.baeldung.com/java-choosing-gc-algorithm</a></p><p><a href="https://newrelic.com/blog/best-practices/java-garbage-collection">https://newrelic.com/blog/best-practices/java-garbage-collection</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ff57b1d54b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>