<?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 Ghanshyam Verma on Medium]]></title>
        <description><![CDATA[Stories by Ghanshyam Verma on Medium]]></description>
        <link>https://medium.com/@ghanshyam.v16?source=rss-8adfc71628b9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*c_5SWuJq7BlkRAXX</url>
            <title>Stories by Ghanshyam Verma on Medium</title>
            <link>https://medium.com/@ghanshyam.v16?source=rss-8adfc71628b9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 05:50:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ghanshyam.v16/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[Virtual Threads and MySQL: Unlocking Performance Gains with mysql-connector-J/9.0.0]]></title>
            <link>https://medium.com/naukri-engineering/virtual-threads-and-mysql-unlocking-performance-gains-with-mysql-connector-j-9-0-0-6754abc85f61?source=rss-8adfc71628b9------2</link>
            <guid isPermaLink="false">https://medium.com/p/6754abc85f61</guid>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[scalability]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[virtual-threads]]></category>
            <dc:creator><![CDATA[Ghanshyam Verma]]></dc:creator>
            <pubDate>Mon, 05 Aug 2024 04:22:12 GMT</pubDate>
            <atom:updated>2024-08-05T04:22:12.688Z</atom:updated>
            <content:encoded><![CDATA[<p>In my previous blog post on <a href="https://medium.com/naukri-engineering/virtual-thread-performance-gain-for-microservices-760a08f0b8f3">Virtual Thread Performance Gain for Microservices</a>, we deep dive into the remarkable capabilities of virtual thread. How virtual threads break Little’s law, and how virtual threads are special as they internally provide non-blocking IO with the existing blocking programming paradigm. <br>But we also experience the blocking behavior of virtual threads with MySQL. Let’s reiterate the use case:</p><p><strong>API Performance testing exposed over JPA</strong></p><pre>@GetMapping<br>@RequestMapping(value = &quot;/slowDBcall&quot; , method = RequestMethod.GET)<br>public ResponseEntity&lt;String&gt; dbCall() throws InterruptedException, URISyntaxException {<br>    userRepository.executeSleep();<br>    return ResponseEntity.ok(&quot;success&quot;);<br>}</pre><pre>@Repository<br>public interface UserRepository extends CrudRepository&lt;User, Long&gt; {<br>    <br>    @Query(value = &quot;select sleep(1)&quot;, nativeQuery = true)<br>    public int executeSleep();<br>}</pre><p>We mimic the slowness of the database with a sleep query.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HUiUJoF6TTTUdhQWhx43rQ.png" /><figcaption>Figure1. Concurrency and tps for database exposed API.</figcaption></figure><p>In the above figure, the default Tomcat configuration with 200 traditional platform worker threads throttles at that limit. The virtual thread pool as a worker pool throttles at 16 (although it should handle large concurrency without impacting response time). As there were 16 platform threads and the TPS throttled at that number, it clearly indicates that the blocking of the carrier thread caused this throttling. This observation was validated via the thread dump.</p><p>This behavior can be validated official MySQL bug -&gt; <a href="https://bugs.mysql.com/bug.php?id=109346">https://bugs.mysql.com/bug.php?id=109346</a></p><pre>A few lines from bug: <br>There is a limition when code is executed in a synchronized block. In a synchronized block the carrier thread will be pinned and won&#39;t be available to perform other work.<br>These synchronized blocks where IO is being done, should be changed and use a ReentrantLock.</pre><p>With mysql-connector-j/9.0.0 release it is fixed. Release notes: <a href="https://dev.mysql.com/doc/relnotes/connector-j/en/news-9-0-0.html">https://dev.mysql.com/doc/relnotes/connector-j/en/news-9-0-0.html</a></p><p>Synchronized blocks in the Connector/J code were replaced with ReentrantLocks. This allows carrier threads to unmount virtual threads when they are waiting on IO operations, making Connector/J virtual-thread friendly. <br>Github commit changes of same: <a href="https://github.com/mysql/mysql-connector-j/commit/00d43c5e8b24f1d516f93eea900b3487c15a489c">https://github.com/mysql/mysql-connector-j/commit/00d43c5e8b24f1d516f93eea900b3487c15a489c</a></p><p><strong>Now, it’s time to validate the same and do performance testing:</strong></p><p><strong>Changes: </strong>Upgrading mysql-connector-java to mysql-connector-j.</p><p>From:</p><pre> &lt;dependency&gt;<br>   &lt;groupId&gt;mysql&lt;/groupId&gt;<br>   &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;<br>   &lt;version&gt;8.0.31&lt;/version&gt;<br>  &lt;/dependency&gt;</pre><p>To:</p><pre>&lt;dependency&gt;<br>   &lt;groupId&gt;com.mysql&lt;/groupId&gt;<br>    &lt;artifactId&gt;mysql-connector-j&lt;/artifactId&gt;<br>   &lt;version&gt;9.0.0&lt;/version&gt;<br>  &lt;/dependency&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h8WbhbYICOsgCe6XEW0vhQ.png" /><figcaption>Concurrency and tps with mysql-connector-j/9.0.0</figcaption></figure><p>With upgraded mysql-connector-j/9.0.0 we were able to get a tps of 1000 but throttled on it, and that is because of Hikari max-connection pool (spring.datasource.hikari.maximum-pool-size=1000). And we anyways needed this to protect downstream databases from infinite API bombarding after virtual thread. So Hikari connection pool acts like a protecting bulkhead for MySQL.</p><p>We also validate that the connection pool is not blocking, i.e if the max connection is 1000, and if concurrency &gt; 1000, it should not pin the thread and should not impact other API.</p><p><strong>Conclusion:<br></strong>Before MySQL-connector-J/9.0.0 change, we can opt virtual thread only for Rest aggregator application with zero database interaction. However, this upgrade has been a game changer, addressing these issues by replacing synchronized blocks with ReentrantLocks, thereby making the connector more compatible with virtual threads.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6754abc85f61" width="1" height="1" alt=""><hr><p><a href="https://medium.com/naukri-engineering/virtual-threads-and-mysql-unlocking-performance-gains-with-mysql-connector-j-9-0-0-6754abc85f61">Virtual Threads and MySQL: Unlocking Performance Gains with mysql-connector-J/9.0.0</a> was originally published in <a href="https://medium.com/naukri-engineering">Naukri Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Virtual thread: performance gain for microservices]]></title>
            <link>https://medium.com/naukri-engineering/virtual-thread-performance-gain-for-microservices-760a08f0b8f3?source=rss-8adfc71628b9------2</link>
            <guid isPermaLink="false">https://medium.com/p/760a08f0b8f3</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[virtual-threads]]></category>
            <category><![CDATA[multithreading]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[jdk21]]></category>
            <dc:creator><![CDATA[Ghanshyam Verma]]></dc:creator>
            <pubDate>Fri, 23 Dec 2022 01:12:56 GMT</pubDate>
            <atom:updated>2022-12-23T04:40:15.072Z</atom:updated>
            <content:encoded><![CDATA[<p>The most awaited project of JDK is finally released as a preview feature with JDK-19. For detail about the preview feature browse <a href="https://openjdk.org/jeps/425">JEPS425</a>, it’s a fine detailed documentation, strongly recommend going through the official JEPs.</p><p>The blog content is organized as:</p><ol><li><strong>Why virtual threads are special.</strong></li><li><strong>Integration with the spring-boot embedded tomcat-based application.</strong></li><li><strong>Performance gains in microservices.</strong></li><li><strong>Debugging with virtual threads.</strong></li><li><strong>Possible pitfall of virtual thread</strong></li></ol><p><strong>Why virtual threads are special.</strong></p><ol><li>Little’s law: Concurrency for an application is equal to the throughput multiplied by the average response time. Till now, concurrency was constant, take the example of tomcat based application where the worker thread is equal to 200. Although this is a tunable parameter but limited by OS memory and CPU, so the throughput is limited by the number of worker threads in the webserver. But with the virtual thread, which is a lightweight thread then the concurrency can scale to a very large number.</li><li>No longer 1:1 mapping with kernel threads: With early JDK, when most computers had single core only, JVM had green threads which were many to one relationship between JVM threads to a kernel thread. But eventually multi-threaded outperformed the green threads. Till now JVM has a one-to-one mapping between kernel threads and JVM threads, and JVM threads are a wrapper over functionalities over kernel thread so gain around a few functionalities like execution thread stacks which we easily get in thread dump. With virtual thread, JDK again going with M:N relationship, but here M can be very large in numbers. So, basically, virtual threads are user-spaced threads that get scheduled over a fork-join carrier thread pool. And this carrier fork-join pool is default equal to the number of processes.</li><li>Network blocking IO no longer blocking on virtual thread: Java has modified the java.io.socket in such a way that if blocking IO is executed over virtual thread, it&#39;s no longer blocking. And with this modification, all blocking calls whether it’s aerospike, MySQL, httpclient automatically convert to non-blocking as under the hood all use java.io.Socket only.</li><li>For non-blocking application development, it’s a major gain, as till now we have only the reactive paradigm as an option for non-blocking application. The general challenge around the reactive paradigm is to get reactive equivalent libraries for all remote calls (databases, cache, HTTP etc), a lot of learning curve for these paradigms,s and lastly the debugging is tremendously hard.</li></ol><p><strong>Integration with the spring-boot embedded tomcat-based application.</strong></p><p>To try out in microservices with the Jeps425 preview feature, the question was do we need to wait for embedded tomcat to adopt it? <br>The answer is no, spring boot application provides a way to pass a custom thread pool as a worker pool. We need the below change to test out springboot-tomcat-based application with virtual threads:</p><pre>public class VirtualThreadApplication {<br><br> public static void main(String[] args) {<br>  SpringApplication.run(VirtualThreadApplication.class, args);<br> }<br><br> @Bean(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME)<br> public AsyncTaskExecutor asyncTaskExecutor() {<br>  return new TaskExecutorAdapter(Executors.newVirtualThreadPerTaskExecutor());<br> }<br><br> @Bean<br> public TomcatProtocolHandlerCustomizer&lt;?&gt; protocolHandlerVirtualThreadExecutorCustomizer() {<br>  return protocolHandler -&gt; {<br>   protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());<br>  };<br> }<br>}</pre><p><strong>Performance gains in microservices.</strong></p><p>We have tested three scenarios for performance testing.</p><p><strong>NOTE</strong>: All observation in this blog is performed with the jdk19 virtual thread preview feature.</p><ol><li><strong>API exposed with constant one-second thread sleep time.</strong></li></ol><pre>@GetMapping<br>@RequestMapping(value = &quot;/slowAPI&quot; , method = RequestMethod.GET)<br>public ResponseEntity&lt;String&gt; slowResponseTIme(@RequestParam(&quot;timeslowness&quot;) int slowness) throws InterruptedException, URISyntaxException {<br>    Thread.currentThread().sleep(slowness);<br>    return ResponseEntity.ok(&quot;success&quot;);<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*6v3tQmGcU49Q70xXfW48xA.png" /></figure><p>With this performance test, it is clear that via traditional threadpool, we can’t get more than 200 tps as after it the request queue up, and post 200 concurrency response time increase w.r.t increase in concurrency. But with virtual thread pool as tomcat worker thread, the tps increase linearly w.rt to concurrency.</p><p><strong>2. API: Integrating with slow third-party API.</strong></p><pre>@GetMapping<br>public ResponseEntity&lt;String&gt; test() throws InterruptedException, URISyntaxException {<br>    HttpHeaders headers = new HttpHeaders();<br>    headers.set(&quot;content-type&quot;,&quot;application/json&quot;);<br>    HttpEntity requestEntity = new HttpEntity&lt;&gt;(null, headers);<br>    URI uri = new URI(&quot;http://localhost:9098/sample/slowAPI?timeslowness=1000&quot;);<br>    ResponseEntity&lt;String&gt; response = restTemplate.exchange(uri, HttpMethod.GET,requestEntity,String.class);<br>    return ResponseEntity.ok(&quot;success&quot;);<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/1*FO_D1nTnOXZ-ONZFBNCFUA.png" /></figure><p>For this use-case, we want to test the Socket behavior, does it block the thread on blocking remote call. With a traditional thread pool, the tps throttled at 200 TPS as the default worker pool size with tomcat is 200. And post 200 concurrencies, the request starts to pile up. But with a Virtual thread pool as tomcat worker thread, we were able to achieve almost 2000 tps with 2000 concurrency. This is a major gain. <strong><em>Although we can gain similar throughput in the traditional way too but for which we need to increase the worker thread and post a particular point we can’t increase it as the traditional thread is resource intensive and we will run out of memory.</em></strong></p><p><strong>3. API exposed over JPA with select sleep(1) query. (Sleep at DB level.)</strong></p><pre>@GetMapping<br>@RequestMapping(value = &quot;/slowDBcall&quot; , method = RequestMethod.GET)<br>public ResponseEntity&lt;String&gt; dbCall() throws InterruptedException, URISyntaxException {<br>    userRepository.executeSleep();<br>        //Internal code of userRepository <br>        // {<br>        //    @Query(value = &quot;select sleep(1)&quot;, nativeQuery = true)<br>        //    public int executeSleep(); <br>        // }<br>    return ResponseEntity.ok(&quot;success&quot;);<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*eFlsLjgzQhBAhwmFeZbtBQ.png" /></figure><p>This particular use case is interesting and the observation is just contradictory to the expectation. The virtual thread pool as a worker pool throttled at 16. As there were 16th platform threads and the tps throttled at that number clearly stated that the blocking of carrier thread created this throttling. And via the thread dump validated the same observation.</p><p><strong>Debugging in virtual thread based application:</strong></p><p>Virtual thread is the most awaited feature as it provides reactive paradigm seamlessly with traditional programming favor, no another paradigm learning curve, just use virtual thread and the code automatically react like reactive. <br>Along with this, debugging reactive code is a lot cumbersome. But with virtual thread, what exactly is happening on a request can still be got via thread-dump. Although as the virtual thread can grow to a very large number previous thread dump utils don’t give virtual thread stack. For this you need to use the new jcmd command which writes the thread stacks in a file.</p><p>jcmd PID Thread.dump_to_file -format=json /path/to/the/file</p><p><strong>Possible Pitfall of virtual threads:</strong></p><ol><li>Blocking calls: Unknowingly if any remote call or any piece of code which can create a blocking call at the carrier thread level will bring the throughput to number of carrier thread * response time (little’s law). Similar behavior we saw in the third use-cases of performance testing.</li><li>Default bulkhead is lost: We can think previous 200 worker thread as a safeguard for the flood of requests from the client application. It was acting as a bulkhead that the calling service can’t call more than 200 concurrencies from a single service instance. But with virtual thread, this safety measure is lost, which is positive and negative. Now application needs to either build a bulkhead on the service level or application must need to build a rate limit so it could not be flooded with a very high number of concurrency.</li></ol><p><strong>Conclusion:</strong></p><p>Virtual threads are a very handy way of reactive programming. And as the hype, it can solve reactive programming in a very unique way with minimal code changes applications. But still, whether it is able to handle all network socket call as non-blocking under the hood is the question. It’s still in preview feature, so hoping that it handles all network blocking call.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=760a08f0b8f3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/naukri-engineering/virtual-thread-performance-gain-for-microservices-760a08f0b8f3">Virtual thread: performance gain for microservices</a> was originally published in <a href="https://medium.com/naukri-engineering">Naukri Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ripple effect in Aerospike cluster: Production issue debugging]]></title>
            <link>https://medium.com/naukri-engineering/ripple-effect-in-aerospike-cluster-production-issue-debugging-66908d11fa9b?source=rss-8adfc71628b9------2</link>
            <guid isPermaLink="false">https://medium.com/p/66908d11fa9b</guid>
            <category><![CDATA[aerospike]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[nosql]]></category>
            <dc:creator><![CDATA[Ghanshyam Verma]]></dc:creator>
            <pubDate>Tue, 30 Nov 2021 01:33:13 GMT</pubDate>
            <atom:updated>2021-11-30T01:33:13.202Z</atom:updated>
            <content:encoded><![CDATA[<p>In Infoedge, we use Aeropsike for many distributed caching use-cases. We have multiple clusters in production. An incident happened on one of the clusters that we are going to discuss in this post.</p><blockquote>A dependent application was giving timeout errors indicating a kind of slowness in Aerospike. On further debugging, we found out that it was a ripple effect of ssd malfunctioning on a particular node. Let’s deep dive into it.</blockquote><p>In Naukri, Aerospike is a critical component that provides fast access for read heavy traffic. We are heavily using Aerospike and its features like multi-bin, secondary indexes to solve software business problems at scale. The reason behind choosing Aerospike is: It’s a multi-threaded distributed caching solution that claims to provide 1 millions TPS from a single node.</p><p>As it’s a cluster solution, it provides automatic data replication, which provides fault tolerance and scale both. The number of replications can be configured and controlled at namespace level via replication factor. And the cluster can operate as <em>Available and Partition-tolerant</em> (AP) or <em>Consistent and Partition-tolerant</em> (CP), and that depends on the configuration. By default Aerospike operates as AP,</p><ol><li>Read transaction, fetches from master</li><li>Write transaction, write locally and synchronously writes to all replicas and then return successful.</li></ol><h4><strong>Incident:</strong></h4><p>In Naukri, we are on SAO architecture and we have more than 200 microservices. Architecture at this level mandates monitoring and observability principles. We use ELK &amp; Solarwinds APM tool for it.</p><p>We got timeouts from one of services, and although we have solarwinds for APM but the slowness was not detected in it. So, we used the old day practices and put profile logging via spring AOP. On analyzing it, we found this was because of slowness from Aerospike. And that’s why solarwinds was unable to detect it as it doesn’t instrument the Aerospike codebase.</p><p>So, we were getting slowness from Aerospike cluster from all the nodes. We wanted to be sure that it’s not because of the application to cluster network slowness, and all slowness is on Aerospike cluster itself. We analyzed the latencies via Aerospike <a href="https://docs.aerospike.com/docs/tools/asloglatency/">asloglatency</a> utility.</p><figure><img alt="Fig. Aerospike 3 node cluster" src="https://cdn-images-1.medium.com/max/581/1*VREbIU3vnOhu_tf5xhuA2g.png" /><figcaption>Incident - Aerospike 3 node cluster</figcaption></figure><h4><strong>Useful commands:</strong></h4><p>Asloglatency: Aerospike by default logs histograms around reads, writes and queries. Via asloglatency we can read the histograms into latency measurements.</p><pre><em>Command: asloglatency -l AEROSPIKE-LOG-FILENAME -h write -N NAMESPACE -f ‘Oct 14 2021 10:10:00’ -d 180 -n 12 -e 1</em></pre><p>With default logging asloglateny gives high level information about latency of read and writes. All the 3 nodes showed higher latency on writes. To get deeper details about latency, aerospike provides the option of micro-benchmarking.</p><p><a href="https://docs.aerospike.com/docs/operations/monitor/latency/index.html#write-transaction-analysis">Aerospike micro-benchmarking</a>: On enabling it, aerospike dumps end to end measurements around slices of enabled transactions. E.g. on enabling on write, it gives latency for wait time for incoming queue, de-dup resolution time, write on disk time, write on replica time (write-replication) and response return time. Via this, it’s easy to drill out the slowness root-cause.</p><p>This can be enabled via these commands:</p><pre><em>asinfo -v ‘set-config:context=namespace;id=NAMESPACE;enable-benchmarks-write=false’<br>asinfo -v ‘set-config:context=namespace;id=NAMESPACE;enable-benchmarks-read=false’<br>asinfo -v ‘set-config:context=namespace;id=NAMESPACE;enable-benchmarks-storage=false’</em></pre><p><strong>Observations:<br>Node1</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A9Tjmeo2aiOsSBa1L-uF4Q.png" /><figcaption>Fig2: Node 1 asloglatency output for read , write-master, write-replication</figcaption></figure><p><strong>Node2:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7kK7aXpWVLwUGHKOrDEVPw.png" /><figcaption>Fig3: Node2 asloglatency output for read, write-master, write-replica</figcaption></figure><ol><li>The cluster is of 3 nodes. And service was getting high write latency from all nodes.</li><li>Read latency: At 17:08:32, around 4.01% requests took more than 512ms on Node1. On Node 2, all read requests were served under 64ms.</li><li>Write-master latency: At 17:08:42, around 4.17% request took more than 512ms. On Node2, all write-master transactions were done under 32ms.</li><li>Write-replica latency (write-repl-write): At 17:08:52, around 2.74% requests took more than 512ms. On Node2, only 0.62 requests took more than 512ms.</li><li>IO wait on all nodes are in same ranges, and less than 1% only.</li></ol><h4><strong>Analysis:</strong></h4><p>SSD responding slow on node 1 because of which reads &amp; writes slowed down on node1. Writes on node2 &amp; node3 slowed as of replication sync writing on node1, and that’s why write-replication higher side on these two nodes.</p><h4><strong>Conclusion:</strong></h4><p>One of the node SSD malfunctioned and gave latency on writes to SSD, and cluster was responding with higher latencies on writes as it synchronously writes to replicas, and this lead to ripple effect to sibling nodes on writes.<br>We verified it by removing the faulty node from cluster, and all write latency got resolved after it. Later, we replaced the faulty node with new node.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=66908d11fa9b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/naukri-engineering/ripple-effect-in-aerospike-cluster-production-issue-debugging-66908d11fa9b">Ripple effect in Aerospike cluster: Production issue debugging</a> was originally published in <a href="https://medium.com/naukri-engineering">Naukri Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>