<?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 Trương Minh Triết on Medium]]></title>
        <description><![CDATA[Stories by Trương Minh Triết on Medium]]></description>
        <link>https://medium.com/@truongminhtriet96?source=rss-7dd71156ea56------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RCqEjBLF5h3j_9HezZ8Vqg.jpeg</url>
            <title>Stories by Trương Minh Triết on Medium</title>
            <link>https://medium.com/@truongminhtriet96?source=rss-7dd71156ea56------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 04:02:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@truongminhtriet96/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[Playing with Hystrix Thread Pool]]></title>
            <link>https://medium.com/@truongminhtriet96/playing-with-hystrix-thread-pool-c7eebb5b0ddc?source=rss-7dd71156ea56------2</link>
            <guid isPermaLink="false">https://medium.com/p/c7eebb5b0ddc</guid>
            <category><![CDATA[thread-pool]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[spring-cloud]]></category>
            <category><![CDATA[hystrix]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Trương Minh Triết]]></dc:creator>
            <pubDate>Sun, 21 Jun 2020 06:59:36 GMT</pubDate>
            <atom:updated>2020-06-21T07:12:43.324Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently, a professional project I joined implements the Microservice Architecture (MSA) using the Spring Cloud Netflix stack including Eureka, Spring Cloud Config Server, Feign Client with Hystrix support and more. Playing with the Hystrix, a circuit breaker, I get confused with the <a href="https://github.com/Netflix/Hystrix/wiki/Configuration#ThreadPool">thread pool configuration</a> and <a href="https://github.com/Netflix/Hystrix/wiki/How-it-Works#Threads">how it works</a>:</p><pre>hystrix:<br>  command:<br>    default:<br>      circuitBreaker:<br>        #sleepWindowInMilliseconds: 10000<br>        #<em>requestVolumeThreshold: 6<br>        </em>enabled: false<br>      execution:<br>        isolation:<em><br>          </em>strategy: THREAD<em><br>          </em>thread:<br>            timeoutInMilliseconds: 3000<br><em><br>  </em>threadpool:<br>    default:<em><br>      </em>coreSize: 2<br>      <em>#keepAliveTimeMinutes:1<br>      </em>maximumSize: 5<br>      allowMaximumSizeToDivergeFromCoreSize: true<br><em><br>      </em>maxQueueSize: 3<em><br>      </em>queueSizeRejectionThreshold: 3</pre><p>That leads me to research about thread pool, concurrency concept. After searching a dozen of articles, I found a thing behind the thread pool: <a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html"><strong>ThreadPoolExecutor</strong></a>. The API documentation is comprehensive and detailed but difficult to imagine the working flow since there&#39;s no diagram or illustration, only descriptive text. But another clue that enlightens me to understand about it is the <strong>producer/consumer pattern.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*byKFSu0JWWwlzM9MFO01_w.png" /><figcaption><strong>Producer consumer pattern</strong></figcaption></figure><p>Simply put, tasks are sent from <strong>multiple</strong> <strong>producers</strong> into a waiting <strong>queue,</strong> waiting to be handled by <strong>multiple</strong> <strong>consumers.</strong></p><p>Internally, Hystrix Thread Pool is a Java&#39;s ThreadPoolExecutor which implements this pattern.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/0*28dPElpGax1Bi1KP.jpeg" /><figcaption><a href="https://medium.com/@vipulgupta_19290/threadpool-or-executor-framework-7007844cac52">https://medium.com/@vipulgupta_19290/threadpool-or-executor-framework-7007844cac52</a></figcaption></figure><p>In the microservice environment, I use FeignClient as a Rest client to communicate with other business services. Along with it is <a href="https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html#spring-cloud-feign-hystrix">Hystrix support</a> - a circuit breaker mechanism. Every Feign client call is wrapped with a Hystrix command for fault tolerance or latency tolerance.</p><pre>@FeignClient(value = &quot;system-service&quot;, fallback = SystemFallback.class)<br>public interface <strong>SystemClient</strong> {</pre><pre><em>    </em>@GetMapping(value = &quot;/user&quot;)<br>    JsonResponse&lt;List&lt;UserDto&gt;&gt; <strong>sendGetAllUsers</strong>();<br><br>}</pre><p>In this example, current service can make a REST call to system-service to get a list of users. Associate with it is a fallback function to return an empty list if the call fails.</p><pre>@Component<br>public class <strong>SystemFallback</strong> implements SystemClient {<br>   <br>   @Override<br>   public JsonResponse&lt;List&lt;UserDto&gt;&gt; <strong>sendGetAllUsers</strong>() {<br>      return new JsonResponse&lt;&gt;(Collections.<em>emptyList</em>());<br>   }<br><br>}</pre><p>So far, our discussion mainly focuses on how Hystrix ThreadPool works, so I will <strong>disable</strong> the Circuit breaker mechanism, which means the circuit is in always in CLOSED state. All calls can go past the circuit breaker and reach the ThreadPoolExecutor for execution.</p><pre>hystrix:<br>  command:<br>    default:<br>      circuitBreaker:<br>        #sleepWindowInMilliseconds: 10000<br>        #<em>requestVolumeThreshold: 6<br>        </em><strong>enabled: false</strong><br>      execution:<br>        isolation:<em><br>          </em>strategy: THREAD<em><br>          </em>thread:<br>            timeoutInMilliseconds: 3000</pre><p>Now, let&#39;s take a look back at the Hystrix Thread Pool configuration:</p><pre>hystrix:<em><br>  </em>threadpool:<br>    default:<em><br>      </em><strong>coreSize</strong>: 2<br>      <em>#keepAliveTimeMinutes:1<br>      </em><strong>maximumSize</strong>: 5<br>      <strong>allowMaximumSizeToDivergeFromCoreSize</strong>: true<br><em><br>      </em><strong>maxQueueSize</strong>: 3<em><br>      </em><strong>queueSizeRejectionThreshold</strong>: 3</pre><p>First group is the Thread Pool configuration:</p><p><strong>· coreSize</strong>: number of always-available threads in Thread pool.</p><p><strong>· maximumSize</strong>: maximum capacity of the Thread Pool. By default, it’s equal to coreSize and only takes effect when allowMaximumSizeToDivergeFromCoreSize=true. Ideally, coreSize &lt; maximumSize.</p><p><strong>· allowMaximumSizeToDivergeFromCoreSize</strong> (default <strong>false</strong>): when switching to true, allow maximumSize to have different value from coreSize.</p><p>In the sample configuration, I set the <em>coreSize</em>=2, <em>maximumSize</em>=5 and <em>allowMaximumSizeToDivergeFromCoreSize</em>=true. This means there are always 2 available threads ready to execute tasks. When necessary, 3 more threads can be created and ready to serve. They will get disposed if idle for more than <em>keepAliveInMinutes </em>period (default 1).<br>(See <strong>Core and maximum pool sizes</strong> and <strong>Keep-alive times </strong>section <a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html">here</a>).</p><p>The latter group is the queue configuration:</p><p><strong>· maxQueueSize</strong> (default: -1): maximum size of the <a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingQueue.html">LinkedBlockingQueue</a>, fixed after initialization. When value is -1, a <a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/SynchronousQueue.html">SynchronousQueue</a> is used instead, a simple hand-off queue with no buffer.</p><p><strong>· queueSizeRejectionThreshold</strong>: dynamically changes the queue limit, never exceeds the <em>maxQueueSize</em>, only takes effect when <em>maxQueueSize</em> value is positive.</p><p>(More info can found in this git issue <a href="https://github.com/Netflix/Hystrix/issues/1554">discussion</a> or <a href="#ThreadPool">official Netflix document</a>).</p><p>Here’s the illustration when I send 10 concurrent <em>requests</em> (illustrated as <em>tasks</em>):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*4K-ZkOSPtu_z2qpWWdgYfQ.gif" /></figure><p>Initially, there are<em> 2 available threads</em> in the thread pool. The 2 first tasks go through the queue and get redirected to idle <em>Thread 1 and 2 </em>for execution.</p><p>When task 3 is enqueued, as there’s no available thread, ThreadFactory inside the ThreadPoolExecutor creates a new <em>Thread 3</em> to handle the task. The same thing happens to task 4 and 5 with the creation of <em>Thread 4 and 5. </em>The maximum thread pool size is now reached, no more thread can be created.</p><p><em>Task 6, 7, 8</em> are enqueued and wait until there’s an available Thread worker to handle.</p><p>When the queue is full, it <strong>rejects</strong> any incoming tasks, so <em>task 9 and 10 </em>get blocked and redirected to a fallback function.</p><p>Notes that if Thread 3, 4 or 5 is idle longer than <em>keepAliveInMinutes </em>period<em>, </em>it will get disposed. The thread pool shrinks its size to 2 (<em>coreSize</em>).</p><p>In summary, there are 8 tasks get executed and 2 get rejected.</p><p>That’s it! I hope this article is informative and useful for you. Feel free have any question or concern on the comment section below.</p><p>Thank you for your time!</p><p><strong>References</strong>:</p><ul><li><a href="https://github.com/Netflix/Hystrix/wiki/Configuration#threadpool-properties">Netflix/Hystrix</a></li><li><a href="https://github.com/Netflix/Hystrix/issues/1554">What&#39;s the relationship between thread pool properties and queue properties? · Issue #1554 · Netflix/Hystrix</a></li><li><a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html">ThreadPoolExecutor (Java Platform SE 7 )</a></li><li><a href="https://www.logicbig.com/tutorials/spring-framework/spring-cloud/circuit-breaker-hystrix-thread-pool.html">Spring Cloud - Circuit Breaker Hystrix, concurrent requests and default thread pool size</a></li></ul><p><strong>Local sources (in Vietnamese):</strong></p><ul><li><a href="https://viblo.asia/p/tim-hieu-ve-thread-pool-trong-java-OeVKBDQMlkW">Tìm hiểu về Thread Pool trong java</a></li><li><a href="https://techblog.vn/queue-blockingqueue-synchronousqueue-trong-java-va-ung-dung">Queue, BlockingQueue, SynchronousQueue trong java và ứng dụng</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7eebb5b0ddc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>