Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How Quarkus brings imperative and reactive programming together

#1
How Quarkus brings imperative and reactive programming together

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/01/how-quarkus-brings-imperative-and-reactive-programming-together.png" width="300" height="108" title="" alt="" /></div><div><p>The supersonic subatomic Java singularity has expanded!</p>
<p>42 releases, 8 months of community participation, and 177 amazing contributors led up to the release of <a href="https://developers.redhat.com/topics/quarkus/">Quarkus 1.0</a>.  This release is a significant milestone with a lot of cool features behind it. You can read more in the <a href="https://quarkus.io/blog/announcing-quarkus-1-0/">release announcement</a>.</p>
<p>Building on that awesome news, we want to delve into how Quarkus unifies both imperative and reactive programming models and its reactive core. We’ll start with a brief history and then take a deep dive into what makes up this dual-faceted reactive core and how <a href="https://developers.redhat.com/topics/enterprise-java/">Java</a> developers can take advantage of it.<span id="more-653567"></span></p>
<p><a href="https://developers.redhat.com/topics/microservices/">Microservices</a>, <a href="https://developers.redhat.com/topics/event-driven/">event-driven architectures</a>, and <a href="https://developers.redhat.com/topics/serverless-architecture/">serverless</a> functions are on the rise. Creating a cloud-native architecture has become more accessible in the recent past; however, challenges remain, especially for Java developers. Serverless functions and microservices need faster startup times, consume less memory, and above all offer developer joy. Java, in that regard, has just in recent years done some improvements (e.g., ergonomics enhancements for containers, etc.). However, to have a performing container-native Java, it hasn’t been easy. Let’s first take a look at some of the inherent issues for developing container-native Java applications.</p>
<p>Let’s start with a bit of history.</p>
<p><img loading="lazy" class=" wp-image-653597 " data-add-featherlight="https://developers.redhat.com/blog/wp-content/uploads/2019/11/Screenshot-2019-11-05-at-01.04.16-1024x367.png" src="https://developers.redhat.com/blog/wp-content/uploads/2019/11/Screenshot-2019-11-05-at-01.04.16-300x108.png" alt="Threads CPUs Java and Containers" width="892" height="321" srcset="https://developers.redhat.com/blog/wp-content/uploads/2019/11/Screenshot-2019-11-05-at-01.04.16-300x108.png 300w, https://developers.redhat.com/blog/wp-co...68x275.png 768w, https://developers.redhat.com/blog/wp-co...24x367.png 1024w" sizes="(max-width: 892px) 100vw, 892px" /></p>
<h2><b>Threads and containers</b></h2>
<p>As of version 8u131, Java is more container-aware, due to the ergonomics enhancements. So now, the JVM knows the number of cores it’s running on and can customize thread pools accordingly — typically the fork/join pool. That’s all great, but let’s say we have a traditional web application that uses HTTP servlets or similar on Tomcat, Jetty, or the like. In effect, this application gives a thread to each request allowing it to block this thread when waiting for IO to occur, such as accessing databases, files, or other services. The sizing for such an application depends on the number of concurrent requests rather than the number of available cores; this also means quota or limits in Kubernetes on the number of cores will not be of great help and eventually will result in throttling.</p>
<h2><b>Memory exhaustion</b></h2>
<p>Threads also cost memory. Memory constraints inside a container do not necessarily help. Spreading that over multiple applications and threading to a large extent will cause more switching and, in some cases, performance degradation. Also, if an application uses traditional microservices frameworks, creates database connections, uses caching, and perhaps needs some more memory, then straightaway one would also need to look into the JVM memory management so that it’s not getting killed (e.g., XX:+UseCGroupMemoryLimitForHeap). Even though JVM can understand cgroups as of Java 9 and adapt memory accordingly, it can still get quite complex to manage and size the memory.</p>
<h2><b>Quotas and limits</b></h2>
<p>With Java 11, we now have the support for CPU quotas (e.g., PreferContainerQuotaForCPUCount). Kubernetes also provides support for limits and quotas. This could make sense; however, if the application uses more than the quota again, we end up with sizing based on cores, which in the case of traditional Java applications, using one thread per request, is not helpful at all.</p>
<p>Also, if we were to use quotas and limits or the scale-out feature of the underlying Kubernetes platform, the problem wouldn’t solve itself; we would be throwing more capacity at the underlying issue or end up over-committing resources. And if we were running this on a high load in a public cloud, certainly we would end up using more resources than necessary.</p>
<h2><b>What can solve this?</b></h2>
<p>A straightforward solution to these problems would be to use asynchronous and non-blocking IO libraries and frameworks like Netty, <a href="https://developers.redhat.com/blog/2019/10/21/eclipse-vert-x-3-8-1-update-for-red-hat-runtimes/">Vert.x,</a> or Akka. They are more useful in containers due to their reactive nature. By embracing non-blocking IO, the same thread can handle multiple concurrent requests. While a request processing is waiting for some IO, the thread is released and so can be used to handle another request. When the IO response required by the first request is finally received, processing of the first request can continue. Interleaving request processing using the same thread reduces the number of threads drastically and also resources to handle the load.</p>
<p>With non-blocking IO, the number of cores becomes the essential setting as it defines the number of IO threads you can run in parallel. Used properly, it efficient dispatches the load on the different cores, handling more with fewer resources.</p>
<h2>Is that all?</h2>
<p>And, there’s more. Reactive programming improves resource usage but does not come for free. It requires that the application code embrace non-blocking and avoid blocking the IO thread. This is a different development and execution model. Although there are many libraries to help you do this, it’s still a mind-shift.</p>
<p>First, you need to learn how to write code executed asynchronously because, as soon as you start using non-blocking IOs, you need to express what is going to happen once the response is received. You cannot wait and block anymore. To do this, you can pass callbacks, use reactive programming, or continuation. But, that’s not all, you need to use non-blocking IOs and so have access to non-blocking servers and clients for everything you need. HTTP is the simple case, but think about database access, file systems, and so on.</p>
<p>Although end-to-end reactive provides the best efficiency, the shift can be hard to comprehend. Having the ability to mix both reactive and imperative code is becoming essential to:</p>
<ol>
<li>Use efficiently the resources on hot paths, and</li>
<li>Provide a simpler code style for the rest of the application.</li>
</ol>
<h2>Enter Quarkus</h2>
<p>This is what Quarkus is all about: unifying reactive and imperative in a single runtime.</p>
<p>Quarkus uses Vert.x and Netty at its core. And, it uses a bunch of reactive frameworks and extensions on top to help developers. Quarkus is not just for HTTP microservices, but also for event-driven architecture. Its reactive nature makes it very efficient when dealing with messages (e.g., Apache Kafka or AMQP).</p>
<p>The secret behind this is to use a single reactive engine for both imperative and reactive code.</p>
<p><img loading="lazy" class=" wp-image-653607 " data-add-featherlight="https://developers.redhat.com/blog/wp-content/uploads/2019/11/reactive-quarkus-1024x489.png" src="https://developers.redhat.com/blog/wp-content/uploads/2019/11/reactive-quarkus-300x143.png" alt="" width="913" height="435" srcset="https://developers.redhat.com/blog/wp-content/uploads/2019/11/reactive-quarkus-300x143.png 300w, https://developers.redhat.com/blog/wp-co...68x367.png 768w, https://developers.redhat.com/blog/wp-co...24x489.png 1024w" sizes="(max-width: 913px) 100vw, 913px" /></p>
<p>Quarkus does this quite brilliantly. Between imperative and reactive, the obvious choice is to have a reactive core. What that helps with is a fast non-blocking code that handles almost everything going via the event-loop thread (IO thread). But, if you were creating a typical REST application or a client-side application, Quarkus also gives you the imperative programming model. For example, Quarkus HTTP support is based on a non-blocking and reactive engine (Eclipse Vert.x and Netty). All the HTTP requests your application receive are handled by <i>event loops</i> (IO Thread) and then are routed towards the code that manages the request. Depending on the destination, it can invoke the code managing the request on a worker thread (servlet, Jax-RS) or use the IO was thread (reactive route).</p>
<p id="ObTUmeC"><a href="https://developers.redhat.com/blog/wp-content/uploads/2019/11/img_5dcca0ed0a68d.png"><img class=" wp-image-653617 " data-add-featherlight="https://developers.redhat.com/blog/wp-content/uploads/2019/11/img_5dcca0ed0a68d.png" src="https://developers.redhat.com/blog/wp-content/uploads/2019/11/img_5dcca0ed0a68d.png" alt="" srcset="https://developers.redhat.com/blog/wp-content/uploads/2019/11/img_5dcca0ed0a68d.png 771w, https://developers.redhat.com/blog/wp-co...00x137.png 300w, https://developers.redhat.com/blog/wp-co...68x352.png 768w" sizes="(max-width: 771px) 100vw, 771px" /></a></p>
<p>For messaging connectors, non-blocking clients are used and run on top of the Vert.x engine. So, you can efficiently send, receive, and process messages from various messaging middleware.</p>
<p>To help you get started with reactive on Quarkus, there are some well-articulated guides on <a href="http://www.quarkus.io">Quarkus.io:</a></p>
<ul>
<li><a href="https://quarkus.io/guides/reactive-routes-guide">Using Reactive Routes</a></li>
<li><a href="https://quarkus.io/guides/reactive-sql-clients">Reactive SQL Clients</a></li>
<li><a href="https://quarkus.io/guides/kafka-guide">Using Apache Kafka with Reactive Messaging</a></li>
<li><a href="https://quarkus.io/guides/amqp-guide">Using AMQP with Reactive Messaging</a></li>
<li><a href="https://quarkus.io/guides/using-vertx">Using Vertx API</a></li>
</ul>
<p>There are also reactive demo scenarios that you can try online; you don’t need a computer or an IDE, just give it a go in your browser. You can try them out <a href="https://learn.openshift.com/middleware/courses/middleware-quarkus/">here.</a></p>
<h3>Additional resources</h3>
<ul>
<li><a href="https://www.redhat.com/cms/managed-files/cl-4-reasons-try-quarkus-checklist-f19180cs-201909-en.pdf">Four reasons to try Quarkus</a></li>
<li>Quarkus website:<a href="http://quarkus.io"> http://quarkus.io</a></li>
<li>Quarkus GitHub project:<a href="https://github.com/quarkusio/quarkus"> https://github.com/quarkusio/quarkus</a></li>
<li>Quarkus Twitter:<a href="https://twitter.com/QuarkusIO"> https://twitter.com/QuarkusIO</a></li>
<li>Quarkus chat:<a href="https://quarkusio.zulipchat.com/"> https://quarkusio.zulipchat.com/</a></li>
<li>Quarkus mailing list:<a href="https://groups.google.com/forum/#!forum/quarkus-dev"> https://groups.google.com/forum/#!forum/...ev</a></li>
</ul>
<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2019%2F11%2F18%2Fhow-quarkus-brings-imperative-and-reactive-programming-together%2F&amp;linkname=How%20Quarkus%20brings%20imperative%20and%20reactive%20programming%20together" title="Facebook" rel="nofollow noopener noreferrer" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2019%2F11%2F18%2Fhow-quarkus-brings-imperative-and-reactive-programming-together%2F&amp;linkname=How%20Quarkus%20brings%20imperative%20and%20reactive%20programming%20together" title="Twitter" rel="nofollow noopener noreferrer" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2019%2F11%2F18%2Fhow-quarkus-brings-imperative-and-reactive-programming-together%2F&amp;linkname=How%20Quarkus%20brings%20imperative%20and%20reactive%20programming%20together" title="LinkedIn" rel="nofollow noopener noreferrer" target="_blank"></a><a class="a2a_button_email" href="https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2019%2F11%2F18%2Fhow-quarkus-brings-imperative-and-reactive-programming-together%2F&amp;linkname=How%20Quarkus%20brings%20imperative%20and%20reactive%20programming%20together" title="Email" rel="nofollow noopener noreferrer" target="_blank"></a><a class="a2a_button_print" href="https://www.addtoany.com/add_to/print?linkurl=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2019%2F11%2F18%2Fhow-quarkus-brings-imperative-and-reactive-programming-together%2F&amp;linkname=How%20Quarkus%20brings%20imperative%20and%20reactive%20programming%20together" title="Print" rel="nofollow noopener noreferrer" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2019%2F11%2F18%2Fhow-quarkus-brings-imperative-and-reactive-programming-together%2F&amp;linkname=How%20Quarkus%20brings%20imperative%20and%20reactive%20programming%20together" title="Reddit" rel="nofollow noopener noreferrer" target="_blank"></a><a class="a2a_button_flipboard" href="https://www.addtoany.com/add_to/flipboard?linkurl=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2019%2F11%2F18%2Fhow-quarkus-brings-imperative-and-reactive-programming-together%2F&amp;linkname=How%20Quarkus%20brings%20imperative%20and%20reactive%20programming%20together" title="Flipboard" rel="nofollow noopener noreferrer" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2019%2F11%2F18%2Fhow-quarkus-brings-imperative-and-reactive-programming-together%2F&title=How%20Quarkus%20brings%20imperative%20and%20reactive%20programming%20together" data-a2a-url="https://developers.redhat.com/blog/2019/11/18/how-quarkus-brings-imperative-and-reactive-programming-together/" data-a2a-title="How Quarkus brings imperative and reactive programming together"><img src="https://static.addtoany.com/buttons/favicon.png" alt="Share"></a></p>
<p>The post <a rel="nofollow" href="https://developers.redhat.com/blog/2019/11/18/how-quarkus-brings-imperative-and-reactive-programming-together/">How Quarkus brings imperative and reactive programming together</a> appeared first on <a rel="nofollow" href="https://developers.redhat.com/blog">Red Hat Developer</a>.</p>
</div>


https://www.sickgaming.net/blog/2019/11/...-together/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016