<?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 Vijay Kambala on Medium]]></title>
        <description><![CDATA[Stories by Vijay Kambala on Medium]]></description>
        <link>https://medium.com/@vijaykambala?source=rss-a9551000a54c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RUrh2d9tiExOuHw9NUejvQ.jpeg</url>
            <title>Stories by Vijay Kambala on Medium</title>
            <link>https://medium.com/@vijaykambala?source=rss-a9551000a54c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 03:57:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vijaykambala/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[Efficient Async Processing in Spring Boot APIs with Multiple Thread Executors and CompletableFuture]]></title>
            <link>https://vijaykambala.medium.com/efficient-async-processing-in-spring-boot-apis-with-multiple-thread-executors-and-completablefuture-21a62d9df337?source=rss-a9551000a54c------2</link>
            <guid isPermaLink="false">https://medium.com/p/21a62d9df337</guid>
            <dc:creator><![CDATA[Vijay Kambala]]></dc:creator>
            <pubDate>Tue, 29 Oct 2024 14:37:38 GMT</pubDate>
            <atom:updated>2024-10-29T14:39:44.545Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In modern microservices, performance and scalability are paramount. A typical REST API request often involves multiple steps: parsing requests, processing complex computations, querying databases, and even integrating with third-party services. In high-load systems, executing all tasks sequentially can slow down response times. In this article, we’ll explore a high-performance design pattern for Spring Boot APIs using CompletableFuture and custom executors to handle different tasks in parallel.</p><h3>Why CompletableFuture and Custom Executors?</h3><p>CompletableFuture in Java provides a non-blocking, asynchronous way to execute tasks, letting us handle tasks concurrently and offload resource-intensive tasks to separate thread pools. Spring Boot makes it easy to integrate CompletableFuture with multiple executors, allowing us to design APIs that handle thousands of concurrent requests efficiently.</p><h3>Outline of Our Design</h3><p>In this example, our API will:</p><ol><li>Accept HTTP requests asynchronously.</li><li>Use a <strong>Core Executor</strong> for computational tasks.</li><li>Use a <strong>Database Executor</strong> for querying databases.</li><li>Use a <strong>Http Executor</strong> for making to external apis.</li></ol><p>By using separate executors, we can prevent a high workload in one area from affecting others, improving performance and resilience.</p><h3>Step 1: Setting Up the Executors</h3><p>To begin, define custom thread pools in a Spring configuration file to assign tasks to specific executors.</p><pre>@Configuration<br>public class ExecutorConfig {<br><br>    @Bean<br>    public Executor httpExecutor() {<br>        return Executors.newFixedThreadPool(20);<br>    }<br><br>    @Bean<br>    public Executor coreExecutor() {<br>        return Executors.newFixedThreadPool(50);<br>    }<br><br>    @Bean<br>    public Executor dbExecutor() {<br>        return Executors.newFixedThreadPool(30);<br>    }<br>}</pre><p>This configuration defines three executors: one for handling HTTP requests, one for core computations, and another for database queries. Adjust the thread pool sizes based on your expected load and task complexity.</p><h3>Step 2: Leveraging CompletableFuture for Asynchronous Processing</h3><p>In a typical Spring Boot controller, synchronous calls may look like this:</p><pre>@GetMapping(&quot;/process&quot;)<br>public ResponseEntity&lt;String&gt; process() {<br>    // Synchronous processing<br>    return ResponseEntity.ok(&quot;Processed&quot;);<br>}</pre><p>To enhance this with CompletableFuture, we can modify our controller method to use asynchronous processing, returning CompletableFuture&lt;ResponseEntity&lt;String&gt;&gt; instead. Here’s how to apply different executors for distinct parts of the processing logic.</p><h3>Step 3: Writing the Asynchronous Controller Logic</h3><pre>@RestController<br>@RequestMapping(&quot;/api&quot;)<br>public class MyController {<br><br>    private final Executor coreExecutor;<br>    private final Executor dbExecutor;<br><br>    public MyController(@Qualifier(&quot;coreExecutor&quot;) Executor coreExecutor,<br>                        @Qualifier(&quot;dbExecutor&quot;) Executor dbExecutor) {<br>        this.coreExecutor = coreExecutor;<br>        this.dbExecutor = dbExecutor;<br>    }<br><br>    @GetMapping(&quot;/process&quot;)<br>    public CompletableFuture&lt;ResponseEntity&lt;String&gt;&gt; process() {<br>        return CompletableFuture.supplyAsync(this::complexCalculation, coreExecutor)<br>            .thenApplyAsync(this::fetchFromDatabase, dbExecutor)<br>            .thenApply(ResponseEntity::ok);<br>    }<br><br>    private String complexCalculation() {<br>        // Simulate a complex computation<br>        return &quot;Result of Complex Calculation&quot;;<br>    }<br><br>    private String fetchFromDatabase(String result) {<br>        // Simulate a database query that depends on the computation result<br>        return result + &quot; + Fetched Data from DB&quot;;<br>    }<br>}</pre><h3>Step 4: Understanding the Flow</h3><ul><li><strong>Complex Calculation</strong>: A computationally intensive task is offloaded to the core executor pool, allowing it to run independently of HTTP and database operations.</li><li><strong>Database Fetch</strong>: Once the computation is complete, the result is passed to another CompletableFuture stage running in the DB executor, which retrieves data based on the computation’s outcome.</li></ul><h3>Step 5: Optimizing and Handling Errors</h3><p>Handling errors in an asynchronous setup is crucial for a smooth user experience. CompletableFuture provides .exceptionally() and .handle() methods to manage exceptions gracefully.</p><pre>    @GetMapping(&quot;/process&quot;)<br>    public CompletableFuture&lt;ResponseEntity&lt;String&gt;&gt; process() {<br>        return CompletableFuture.supplyAsync(this::complexCalculation, coreExecutor)<br>            .thenApplyAsync(this::fetchFromDatabase, dbExecutor)<br>            .thenApply(ResponseEntity::ok)<br>            .exceptionally(ex -&gt; ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)<br>                                               .body(&quot;Error occurred: &quot; + ex.getMessage()));<br>    }</pre><h3>Benefits of This Approach</h3><ul><li><strong>Improved Scalability</strong>: Requests are no longer bound to a single thread, allowing for more concurrent processing.</li><li><strong>Resilience</strong>: By isolating tasks in specific executors, resource-heavy operations don’t block other parts of the system.</li><li><strong>Responsiveness</strong>: The asynchronous model allows quicker feedback for clients, especially when combined with non-blocking APIs.</li></ul><h3>Best Practices</h3><ol><li><strong>Monitor Thread Pool Sizes</strong>: Thread pools should be sized based on system resources and anticipated load.</li><li><strong>Use Qualifiers</strong>: With multiple executors, use @Qualifier to avoid injection conflicts.</li><li><strong>Graceful Shutdowns</strong>: Ensure executors are properly shut down to avoid resource leaks.</li><li><strong>Apply Circuit Breakers</strong>: For high-throughput applications, consider using patterns like circuit breakers to prevent overloading specific executors.</li></ol><h3>Conclusion</h3><p>Using CompletableFuture with custom executors in Spring Boot allows for highly responsive, concurrent APIs that can handle complex workflows. This pattern is valuable for applications needing to separate distinct workloads while maintaining code readability and performance. With this approach, you can design APIs that meet high scalability and responsiveness requirements without sacrificing maintainability.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=21a62d9df337" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Max heap myths around finding the kth largest element]]></title>
            <link>https://vijaykambala.medium.com/max-heap-myths-around-finding-the-kth-largest-element-fd58a940a5d5?source=rss-a9551000a54c------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd58a940a5d5</guid>
            <category><![CDATA[heap]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[kth]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[sort]]></category>
            <dc:creator><![CDATA[Vijay Kambala]]></dc:creator>
            <pubDate>Tue, 17 Nov 2020 15:45:57 GMT</pubDate>
            <atom:updated>2021-07-03T10:32:40.955Z</atom:updated>
            <content:encoded><![CDATA[<h3>Max heap myths around finding the kth largest element from an array</h3><p>You might have heard about this problem already either in an interview or in a casual discussion with colleagues or friends. Let’s go into the topic.</p><p><strong>Input:</strong> nums = [3,2,1,5,6,4] and k = 2<br><strong>Output:</strong> 5</p><p>Consider we have an array of integers. Whenever you are asked to write a program to find the kth largest element from an array of integers.</p><p>We immediately think of sorting the array and returning the element at index <strong>length - k</strong>. It may be a Brute-force algorithm but park it aside for some time. See the below code for reference.</p><pre>Arrays.<em>sort</em>(nums);<br>max = nums[nums.length - k];</pre><p>Generally the above is not recommended since <strong>sorting</strong> in the worst case takes quadratic time(O(n²)).</p><p>So, then how should we minimize the complexity. Programmers tend to think using heap data structure(<strong>max-heap</strong>) which keeps the track of maximum numbers at any given point of time. As soon as we build the <strong>max-heap</strong> with all the given numbers, we go on to remove the first <strong>k</strong> numbers from the heap therefore we get the <strong>kth </strong>maximum but remember this is not in place(we are using additional memory for heap here) and it takes about O(n log n) to construct the <strong>max-heap</strong> and to get the <strong>kth</strong> maximum O(k log n). See the below code for reference.</p><pre><em>// construction of max heap</em><br>PriorityQueue&lt;Integer&gt; maxHeap = new PriorityQueue(Collections.<em>reverseOrder</em>());<br><br>for (int i = 0; i &lt; nums.length; i++){<br>    maxHeap.add(nums[i]);<br>}<br><br>int max;<br><em>// Remove the top k-1 roots of max heap<br></em>for (int i = 0; i &lt; k -1; i++){<br>    maxHeap.remove();<br>}<br><em>// k th element of heap<br></em>max = maxHeap.remove();</pre><p>I have run a performance run about 10 times to see which one is faster on both these algorithms with about 100 million integers in an array to find the <strong>kth</strong> maximum. For the first algorithm, it just took about 12 milliseconds on average and for the latter(<strong>max-heap</strong>) it took about 3500 milliseconds.</p><p>But still, ironically interviewers get convinced only when you talk about the latter. It happened to me in a couple of interviews.</p><p>I hope that you will find this article helpful. Feel free to comment, suggest or share your opinion.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd58a940a5d5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>