Going serverless with Java and Quarkus
Get a working understanding of what it means to be serverless
Serverless, a popular word. No need for keeping resources alive where there is no need to. Don’t worry about managing your infrastructure. Just code and add business value. Focus on the things you’re good at and keep track of what is important for you — your core business.
Can you do this using a 25-year-old language? Is Java suitable? Yes, it is, according to IBM Developer Advocate Edward Ciggaar, based in Amsterdam. During the upcoming June 12, 2020 Code @ Think Digital Summit, Edward will dive into the workings and host a workshop.
“To me, serverless is the real promise of the cloud,” says Edward. “When you don’t run anything, you don’t pay. It’s event-driven programming, one process per request, and you can scale immediately when you need to. From zero to enough, without you having to worry about it. When it’s finished, it scales back to zero, just like that.”
“Function as a service, or FaaS, is the most widely used form of serverless, so we will concentrate on FaaS for going serverless with Java,” says Edward. For a wider interpretation of serverless, explore the Serverless Computing page in the IBM Cloud Learn Hub.
“When you use IBM Cloud Functions, you only need to concentrate on your code, your business logic, and to working event-driven. Cloud Functions is based on the open source serverless platform Apache OpenWhisk. You can write your functions in any language, but we have performance-optimized runtime support for the most popular languages, like Python, Node.js, Go, Ruby, and Java.”
Edward explains how to build a Hello World function, which you can use as a developer to get acquainted with the technique. “Take a look at the out-of-the-box echo function in OpenWhisk. This is a Node.js-based function that activates in about 30 milliseconds. That’s really fast — from nothing to up and running.”
Java, on the other hand, is not natively optimized for running serverless: “When you look at Java, which exists since 1995, it is one of the most trusted languages out there. Many people know all of its ins and outs, so this is definitely a language you want to use.” Edward pauses a moment and continues, “When you activate a Java serverless function for the first time, you’ll see that it takes about 300 milliseconds to start. This so-called cold-start is relatively fast, but not fast enough, in my opinion. Also, Java is more memory-intensive and when you do cloud-native development, the less memory you consume, the better.”
To fix this, many vendors put a lot of effort in optimizing runtimes for containerized environments and cloud-native development. So did Red Hat by launching Quarkus last year. Quarkus is a Kubernetes-native Java framework, also known as “supersonic subatomic Java,” that allows developers to build Java applications that are meant to run in, and are optimized for, these type of environments.
“Developers can write Java the way they are used to, applying the Twelve-Factor development practices, and use all their tools they know so well. When they’re done, due to the container-first philosophy of Quarkus, they can easily deploy the application to a container platform like OpenShift or Kubernetes, or as a serverless function.”
Most Java applications that run inside a container functionally don’t change after they’ve been started. Therefore, a lot of the heavy lifting to resolve all the dynamics taking place at runtime could be moved to the build phase. This results in a clean and fully optimized JAR file, where mainly direct invocations are used. Combine this with an optimized JVM like OpenJ9, and you can already benefit from faster startup times and a lower memory footprint. This is the JVM option that Quarkus offers. The other option is the native one.
“If you decide to build your code as native executable using ahead-of-time (AOT) compilation, you end up with a super fast Java application that runs with GraalVM. I was pretty impressed when I saw this for the first time,” says Edward.
The bottom line is that when leveraging frameworks like Quarkus, Java can function very well in serverless environments.
Edward continues on Quarkus and hails the clean result of the builds. “When the build results in such an optimized JAR file (or native executable) that, let’s say, has a 50 percent smaller memory footprint compared to the same code running inside a regular JVM, then that is a big deal. Imagine you have 1000 instances running, knowing you pay for memory usage per second.”
After his brief introduction on going serverless with Java, it’s time for the hands-on workshop. “Some knowledge of Java is needed, so when you need to search for what ‘Maven’ means, it’s probably not the workshop for you. The only thing you need to be able to join is an IBM Cloud account.”
“We’ll be working in the IBM Cloud Shell. This shell has all prerequisites in place, like Maven, Git, and Java. If you want to do it on your own laptop, instead of the browser, then you’ll need Java JDK 1.8 or higher, Maven, Git, and preferably Docker.” Edward will only show Docker and not elaborate on it because it’s not in the hands-on portion itself. “But if you want to continue toying around with Quarkus, then you want to have Docker set up.”
“Furthermore, you’ll see that serverless functions truly scale to zero, and we briefly talk about the disadvantages and current limitations of serverless. Last but not least, you experience yourself how Quarkus does speed up your Java code.”
Join Edward on June 12
Are you curious and want to get hands-on yourself? Or, do you have questions about working serverless with Java? Then join the virtual talk and workshop on Friday, June 12, 2020 at Code @ Think Digital Summit.