What is GraalVM? What is Spring Native? And why do you need native Java?

Episode 248 · Fri, 02 Dec 2022 11:42:58 +0100 · 20:11 · SRT · archive.org · all Tanzu Talk

0:00

here's what i wanted to talk about ben today we might only have a little bit of time because you and i have just used a lot of time to catch up with each other it's always a pleasure this is the thing that that they you know you get about your uh your random coffee donut time what did they call that in zoom have uh remember in zoom in slack there was this movement among the the the nerdy people to have like you'd set up this slack bot and it would randomly assign someone to have a donut with or something wasn't because it was called the donut channel or something i don't know anyways it's good to have donuts with people uh and and to plan that out but i've been helping out you know we've got uh we we've moved our our SpringOne conference to a virtual conference so that more people can attend which i think will be great uh because it becomes very easy if you're interested in attending i'm not i don't even have a discount code because it's free now you can go to spring1.io now you could try to use the discount code i don't know where you would type it but if you want to you can use the code Coté free uh and i assume it will make it free since it already is free you see how that i have a philosophy degree so i know how to do uh you know if a then b and b then c then i can do logic i forget what the upside down es and a's mean uh but i could probably look at my notes from school anyhow i've been going through and helping out with some of the uh the kind of arranging the agenda and this thing keeps coming up over and over again called growl vm and i just can't help but say it that way and i know that's associated with spring native and this stuff has been going on for a while so i was hoping maybe you could educate me on what all of that stuff is because i know you keep doing these videos on python and dot net but i know you're a java person at heart right so you must be as you drift off to sleep at night dreaming of donuts you're probably like growl growl and just dreaming of this stuff because it sounds awesome so what's going on with this java native business yeah it's really quite cool it's um you know for folks who've been around java for a while they'll have heard the term java virtual machine for example and um they'll

2:04

uh you know most folks are used to running programs on their on their computer uh you know if you imagine you're in the command line uh in windows if you want to see all the files that are in a folder you type dir that's a program right and that program executes really quickly gives you a full list of everything that's uh on on that particular folder in mac and uh you know uh linux and it's ls but essentially you know it's a program right all those things are programs java virtual machine and java programs though they work differently they need a virtual machine in order to operate so um you need this like um middle layer that takes the java code and turns it into something that can be run on the machine that you're on it's very flexible because it means that you can uh you know have a java virtual machine for mac os and for windows and for linux and you can um run that even on different um architectures arm or uh or intel or whatever but there's uh there's a problem with having all that stuff in the middle you know it can make things a little bit heavy and uh it can like my middle am i right no no no no your middle is just fine oh thank you but what yeah so you end up with this sort of extra layer and that can there are some use cases where that's a disadvantage right not many because obviously java is huge and it's been very successful but there are some use cases where that's a disadvantage so having something that works more like a a native program like that dir or ls where you know it's instant and it's quick and it has a low memory footprint it's very high performance that sort of native use of a program is quite attractive for certain use cases a good example might be something like serverless where in order to be able to react quickly it's really helpful if

4:08

your workload can start very quickly so if you want really fast start times then starting a java virtual machine going through um the byte code and and compiling that so that it will run um on that machine takes a while um maybe you don't want that in that particular use case maybe you want something faster so that's what graal vm does so to to summarize it's basically i mean i feel like java has tried to do this over the years so many times but like it basically this is the wrong word but but well maybe it's not it basically it compiles down your java code into a single executable that doesn't have the java vm it actually makes it because the java vm takes your bytecode and does that for you kind of on the fly-ish sort of that's inaccurate but it does eventually you always get this this uh executable code that has to run but in this case it's like well why not just do it from the get-go and i you know it must have been very difficult to make that happen over the past 25 years is that how long it's been uh so and and the reason why that's valuable other than just being cool is like well sure it's good to have you know nice startup times but especially if you're looking to do like a cloud native style architecture especially if it's running in kubernetes where each component of your application has very little guarantee of living long i guess i guess you could always say you can tell kubernetes not to destroy it but the intention is that your application architectures will have a bunch of ephemeral components that come and go as makes sense to kubernetes so if if you're writing like a java application uh your little java bits are going to be coming and going all the time if you've architected it that way yeah and there's another there's another quite interesting use case there especially for folks running you know kubernetes at scale and that is that when you go through this process of creating a native image with graal vm

6:15

from your java code um because you're sort of losing some of that um chunky middle that we were talking about earlier uh it means that you can sometimes also benefit from other impacts of that like lower memory footprint so if you imagine you've got a thousand containers and you manage to lower the memory footprint on those containers a little then your cloud bill will reduce so it's also sort of a cost saving measure in some cases as well it's not perfect because you if an application needs a lot of memory anyway naturally because it has a lot of data to work with or whatever it's not going to be hugely lighter but for some applications by sort of stripping away some of that stuff that's in the middle uh the the just in time compiler and all the other stuff that's in there you can um see a reduction in the memory overhead as well okay so it does have definite um cost implications for those who are running at scale as well i think okay okay and so then so the second part is like so so thus far we've mostly talked about like the concept of it and not concept as in it doesn't happen but like generically what it is but what what's up with like graal vm as its own thing right like are there many different like java native things out there and this is just one of them or like is this the only way of doing it or like how does that as a as a i don't know if you would call it a brand but how does it as a brand as a project as a product fit into all of this i definitely say it's the most well known i understand that there were other projects in the java community to also do um a similar thing and um i'm not sure what the status is of those projects so i probably shouldn't really comment on them but um but yeah i doubt it would be alone because it's such a big community there's always room for alternative implementations of similar

8:20

ideas there's also um you know graal vm is is associated with oracle so so there's there's sort of a a paid version and a and a kind of a free tier if you like as well so there are different licensing implications sometimes with uh with with graal vm too so you need to sort of figure out if it's the right thing to do for your workloads probably it's the right thing to do for some workloads but perhaps not for all workloads there are other constraints as well right right and and then and then my understanding is they oracle contributed to the to like open jdk back in october now i i i know that i am not up to date enough on like community open java community open source stuff so i don't want to like step down that path maybe one day i'll go research that i remember way back when when i was still an analyst at red monk i i i knew all of that very well when sun was uh going to oracle and everything but nowadays i'm not really sure what the deal is but okay so so graal vm is basically oracle's uh implementation of doing um native java and and as i was just saying they've contributed to open jdk so if use open jdk i i assume that means it's in there which is great so essentially most any you know if you're doing java development you you have access to it now we and in the spring community they have spring native which which works with graal vm and um i mean i guess what i was wondering about that is like like what is what by the nature of doing native java would why does spring need to be involved in that like because it seems like it might be like you just do you just pass in this one it's always some hyphen uppercase x flag that you pass in or at least in my day it was but you just pass a flag

10:21

into the compiler and you're done but like so what why does spring have to be involved with it right so remember we were talking about the java virtual machine the way that it works it is very flexible you can do things like modify the class path for something um which would add additional features into your application for example so oh that was very very well explained of what a class path does you really uh you really removed all the jargon out of it and just explained the outcome well anyone who's run a minecraft server and adds a modification right a mod uh they will be adding extra programs i guess that's true right so so you know it's so anyway so putting that aside for a second there's a problem and and the problem is that um in order for things like graal vm to be able to create your native application it does static analysis on the code base to find out which avenues through the code are used and then compile those into native machine code that can run natively right and and this may not be extremely accurate so maybe it's more of a metaphor but to put it the other way is it cuts out all the code that's not used it does and it needs to know ahead of time all the code that is used and that's a problem because java has quite a lot of flexibility here there are things like reflection there are external files that you might need to know about there's all sorts of other stuff that also comes along with a java application that you might not know about um and that graal vm might not know about just by having a look at your main class and then figuring out you know what it thinks are the code paths so that causes a problem for lots of um frameworks uh including spring so there's been an awful lot of work done in spring to um bring in some new um innovations that allow that particular style of static code analysis to work

12:30

correctly with frameworks where where flexibility is the name of the game you know spring has been flexibility is one of its major benefits is um inversion of control and um ahead of time uh uh sorry um behind of time yeah yeah yeah there's probably some better phrase for it than that it's december i'm struggling i need to hibernate uh but yeah there's a whole bunch of stuff that you need to know about that that um that is done more um dynamically in in java and in frameworks that rely on java like like spring so there's been an awful lot of work done in spring to make it compatible with that style of static code analysis ahead of time analysis so that um that you can still write your code and use it in uh in your in your application but there are there are still some caveats there there are still some things that you um perhaps won't be able to do so it's not suitable for every application yet but the compatibility is growing and growing and growing and with spring boot three and spring framework six um the compatibility is at such a stage where it's generally available for everyone yeah and and i get you know i guess i guess to make try to make another example like uh if if you you know it's possible in java and definitely in spring to say like all right i have a a p i'm going to generate a pdf of this feature i want i want uh i want to generate uh ben's internet bill so he can file a reimbursement to get home internet reimbursement which i i probably need to go do before it gets too far behind so in in my program i need something that generates a pdf and you could at at building you know when you're writing the software you could decide that i want to use this pdf engine and so you just put it in there and then your java native stuff is just going to pick

14:30

out what it needs from the pdf engine or you could also say like i don't know i might want to swap in and out pdf engines depending on whatever and so even though you've compiled the code you can use some the the the dynamic loading that java has to like bring new pdf things in and not and like so you've got uh i don't know if that example fits because maybe that's taken care of by like graal vm stuff but the point being that you can like actually change if if you're going to be doing things where you're changing the actual code that's running somewhere things get a little dicey if you needed to pre-compile everything by yeah there's no for example those mods we were talking about for minecraft they just wouldn't work in this scenario because it's uh it's statically compiled you have to reboot the server basically well i guess you did not only reboot it you'd have to recompile it you'd have to include all of that stuff from the start you couldn't just patch it on later as an additional jar right so the class paths are fixed for example so if you have a situation where you can't have a fixed class path then probably this style of compilation is not not for that particular work though there you go that's that's a good a good uh shorthand way of thinking about it in the java world is that like if you're doing not a native java you can't ever mod you can't modify the class path like and you can't even restart it you just got to recompile everything which which uh you can't modify it dynamically as you yes yes exactly as you can with regular java yeah you could you know with regular java it's just part of the command that you'd use to start the jvm but with um a graal vm image it's more like you can't modify the dir command right because it's fixed it's it's code is it's code and it doesn't change at runtime whereas java is more flexible but that flexibility causes it challenges in other other ways but the graal is great as well i mean in terms of you know speed up um i've seen from my own sort of messing about with it a while ago that if you had an application

16:32

that maybe took three seconds to start you probably start it in 0.3 seconds so so like a at least a tenfold increase in start yeah that's crazy and that's conservative sometimes you know does does the uh does the compiling time take longer how does it affect that it does yeah yeah the compile time is longer and um one thing if you know for folks wanting to maybe scratch the surface a great way to do it is with spring boot because everything that you need is included in the uh tools that you use to build with spring boot so if you're used to maven for example and you normally use um you know spring boot colon run to run your applications you can use spring boot colon native image to build a native image and the documentation is fabulous the documentation for spring boot 3 i've just been looking through it this morning and it's really really good yeah so folks will find it very easy to get started if they want to give it a try well that's great i feel i feel as educated as i need to be which i think is uh that's not always like how you get an a plus but it uh it'll get you by in life i think but you know i i i don't know this for sure but no doubt at our SpringOne conference if you're interested in grawl and spring native i bet i bet hopefully maybe i should go check and suggest if we don't have this but we should have uh plenty of content going over this stuff uh which there is there is i've seen it already i think there is which uh yeah i'm being kind of facetious but uh hey are you doing are you doing a watch party do you know oh yes i don't know if that's if that's supposed to be some sort of like i don't know why it would oh is it super secret squirrel okay no no no way i mean i i if if it is i i should advise someone that like you know there's whatever uh yeah we have we'll have one in the uh i don't think there's a way for you to register for it or whatever no no no i'm supposed to be hosting one like in the amsterdam office uh to to watch the stuff which will be fun i'll have to think of some speeches to give uh maybe raise up i'll raise up a

18:35

glass of coffee well do share i'm doing the i'm doing the london one so oh that'll be fun do share yeah yeah yeah that's good well well speaking of that if you're interested you can you can attend SpringOne for free if you go to SpringOne.io and uh you can find out about all of that stuff uh you know and you can tend it i forget which dates it is in january but it's it's kind of in the third week of january maybe yeah 24th i think yeah but it just go to SpringOne.io i'll put a link in the uh the show notes here and you can check it out and with that we have a bit of a short one here you gotta wrap it up to go meet an old colleague downtown uh but yeah uh you know if you're interested in the the show notes for this you go to tanzutalk.com you can subscribe to the podcast you can check out videos that we do there or uh if you've watched this and uh you know you can also watch us do it live and youtube and twitch and linkedin twitter all over the place we're everywhere you want to be wait that was a slogan for some company back in the 80s was that at&t maybe uh don't know they don't operate in the uk so what uh let's just go with yes yeah what what was what was the name of of bt's like logo didn't they have like a little mascot and and it's got some name oh i see you know you're giving away my age now but i think it was buzzbee i think it was buzzbee or something like you know give us a buzz oh that's nice something like that okay all right well with that we'll see everyone next time goodbye bye bye Bye.

Tanzu Talk · 🍕 Get more pizza.