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

The supersonic subatomic Java singularity has expanded!

42 releases, 8 months of community participation, and 177 amazing contributors led up to the release of Quarkus 1.0.  This release is a significant milestone with a lot of cool features behind it. You can read more in the release announcement.

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 Java developers can take advantage of it.

Microservices, event-driven architectures, and serverless 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.

Let’s start with a bit of history.

Threads CPUs Java and Containershttps://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" />

Threads and containers


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.

Memory exhaustion


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.

Quotas and limits


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.

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.

What can solve this?


A straightforward solution to these problems would be to use asynchronous and non-blocking IO libraries and frameworks like Netty, Vert.x, 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.

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.

Is that all?


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.

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.

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:

  1. Use efficiently the resources on hot paths, and
  2. Provide a simpler code style for the rest of the application.

Enter Quarkus


This is what Quarkus is all about: unifying reactive and imperative in a single runtime.

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).

The secret behind this is to use a single reactive engine for both imperative and reactive code.

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" />

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 event loops (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).

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.

To help you get started with reactive on Quarkus, there are some well-articulated guides on Quarkus.io:

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 here.

Additional resources


Share

The post How Quarkus brings imperative and reactive programming together appeared first on Red Hat Developer.



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



Possibly Related Threads…
Thread Author Replies Views Last Post
  Quarkus: Modernize “helloworld” JBoss EAP quickstart, Part 1 xSicKxBot 0 52 09-09-2020, 03:48 AM
Last Post: xSicKxBot
  Using Kubernetes ConfigMaps to define your Quarkus application’s properties xSicKxBot 0 169 06-09-2020, 06:30 PM
Last Post: xSicKxBot
  How Quarkus brings imperative and reactive programming together xSicKxBot 0 127 06-08-2020, 01:10 AM
Last Post: xSicKxBot
  Quarkus: Modernize “helloworld” JBoss EAP quickstart, Part 1 xSicKxBot 0 133 06-05-2020, 01:48 PM
Last Post: xSicKxBot
  Bring joy to development with Quarkus, the cloud-native Java framework xSicKxBot 0 144 06-03-2020, 07:45 PM
Last Post: xSicKxBot
  Bring joy to development with Quarkus, the cloud-native Java framework xSicKxBot 0 288 01-27-2020, 05:17 AM
Last Post: xSicKxBot
  Quarkus: Modernize “helloworld” JBoss EAP quickstart, Part 1 xSicKxBot 0 250 01-25-2020, 11:50 AM
Last Post: xSicKxBot
  Using Kubernetes ConfigMaps to define your Quarkus application’s properties xSicKxBot 0 237 01-24-2020, 10:39 AM
Last Post: xSicKxBot
  How Quarkus brings imperative and reactive programming together xSicKxBot 0 218 01-21-2020, 11:38 PM
Last Post: xSicKxBot

Forum Jump:

Become a Patron!
[-]
Upcoming Events

[-]
Latest Threads
[1.06] BO4 Zombie Trainer (Call of duty ...
Last Post: Tom_Pdv
Today 09:03 PM
» Replies: 12
» Views: 440
[Tut] How to Build Your Brand as a Freel...
Last Post: xSicKxBot
Today 08:58 PM
» Replies: 0
» Views: 1
(Indie Deal) FREE Ghost Sweeper, ? NBA 2...
Last Post: xSicKxBot
Today 08:58 PM
» Replies: 0
» Views: 2
Drag[en]gine Hands-On
Last Post: xSicKxBot
Today 08:58 PM
» Replies: 0
» Views: 2
AppleInsider - Apple TV Remote app influ...
Last Post: xSicKxBot
Today 08:58 PM
» Replies: 0
» Views: 1
News - Nightdive Studios Delays Its Enha...
Last Post: xSicKxBot
Today 08:58 PM
» Replies: 0
» Views: 1
News - PS5 Third-Party Face Plates Are A...
Last Post: xSicKxBot
Today 08:58 PM
» Replies: 0
» Views: 2
Xbox Wire - Xbox Insider Release Notes –...
Last Post: xSicKxBot
Today 02:31 PM
» Replies: 0
» Views: 4
News - Video: The lighting technology of...
Last Post: xSicKxBot
Today 02:30 PM
» Replies: 0
» Views: 5
News - Talking Point: What Are You Playi...
Last Post: xSicKxBot
Today 02:22 PM
» Replies: 0
» Views: 4

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020