Containers for Java: Optimizing your applications Mofe Salami - - PowerPoint PPT Presentation

containers for java optimizing your applications
SMART_READER_LITE
LIVE PREVIEW

Containers for Java: Optimizing your applications Mofe Salami - - PowerPoint PPT Presentation

Containers for Java: Optimizing your applications Mofe Salami Developer Advocate Containers are great! App 1 App 2 App 3 Bins/Libs Bins/Libs Bins/Libs App 1 App 2 App 3 Guest OS Guest OS Guest OS Bins/Libs Bins/Libs Bins/Libs


slide-1
SLIDE 1

Containers for Java: Optimizing your applications —

Mofe Salami Developer Advocate

slide-2
SLIDE 2

Containers are great!

Physical Server Host Operating System Container Engine Bins/Libs Bins/Libs Bins/Libs App 1 App 2 App 3 Physical Server Host Operating System Hypervisor Guest OS Guest OS Guest OS Bins/Libs Bins/Libs Bins/Libs App 1 App 2 App 3

2

slide-3
SLIDE 3

Java and containers, not so great

Java is nice but:

  • Large application size
  • Long start-up times
  • Not ideal for FaaS

3

slide-4
SLIDE 4

How can we address these issues?

Takeaways:

  • 1. How to reduce application size
  • 2. Hotspot features that improve start-up times
  • 3. Other optimizations

4

slide-5
SLIDE 5

What this talk will not cover

5

Important, but not in scope:

  • Security
  • JVM vs. resource limits
  • Running multiple JVMs
slide-6
SLIDE 6
  • 1. How to reduce

application size

6

slide-7
SLIDE 7

Defining “application size”

7

  • Containers are isolated environments
  • Dependencies are packaged with the application
  • Java code depends on a JVM

Application size = Linux distribution + JVM + Application Code

slide-8
SLIDE 8

Choosing the “right” JDK distribution

8

Points for consideration:

  • Vendor
  • Support and Updates
  • Support periods
  • Commercial vs. “Free”
  • TCK’d binaries

Read more:

Life Beyond Java 8 – Trisha Gee Panel: Java Is Still Free?

Oracle OpenJDK Build Release date Free updates superseded / ended (by Oracle) 8 (LTS) March 2014 At least through January 2020 (Personal desktop use) Commercial use ended in January 2019 9 Sept 2017 Superseded by Oracle OpenJDK build 10 10 March 2018 Superseded by Oracle OpenJDK build 11 in Sept 2018 11 (LTS) Sept 2018 To be superseded by Oracle OpenJDK build 12 in March 2019 (may be extended) 12 March 2019 To be superseded by Oracle OpenJDK build 13 13 Sept 2019 To be superseded by Oracle OpenJDK build 14

slide-9
SLIDE 9

Choosing AdoptOpenJDK

9

  • Free as in beer and as in speech
  • Built from OpenJDK source repo
  • Backporting patches are community-led
  • Maintained by the OpenJDK community
  • Docker images:
  • latest (Full JDK)
  • slim (Stripped down JDK)
  • alpine (Full JDK)
  • alpine-slim (Stripped down JDK)

Adopt OpenJDK Build Release date Update/Support period 8 (LTS) March 2014 At least September 2023 9 Sept 2017

  • 10

March 2018

  • 11 (LTS)

Sept 2018 At least September 2022

slide-10
SLIDE 10

Choosing the “right” Linux distribution

10

docker pull adoptopenjdk/${VERSION}:${VARIANT}

slide-11
SLIDE 11

Building the application

11

Simple Spring Boot Application Dockerfile

slide-12
SLIDE 12

Building the application

12

docker build -t java-containers:adoptopenjdk-${VERSION}:${VARIANT} Dockerfile

slide-13
SLIDE 13

Demo #1

13

  • Verify the start of java application:

docker run -p 8080:8080 --rm java-containers:adoptopenjdk-8-alpine-slim

  • Verify we can reach the /ping endpoint

Go to: localhost:8080/ping

slide-14
SLIDE 14
  • 2. HotSpot features

that improve start-up times

14

slide-15
SLIDE 15

JVM Start-up times

15

Total start-up time = JVM start-up time + application start-up time

  • What JDK will you use?
  • What type of application are you running?

Let’s experiment with:

  • AdoptOpenJDK 11
  • Our sample rest application
slide-16
SLIDE 16

Class Data Sharing (CDS)

16

  • Oracle JVM feature
  • Reduces the start-up time
  • Creates a cache of Java Class Data
  • Does not factor in classes within custom JARs
  • Available since JDK 5

Generate cache with:

  • java -Xshare:dump

Use with:

  • Xshare:on

Classes from system jar file Private internal representation Dump to shared archive file

slide-17
SLIDE 17

Benchmarking our application

17

  • Test start-up times on average:
  • Add config to exit application after starting up:
slide-18
SLIDE 18

Benchmark Results: CDS (Off vs. On)

18

slide-19
SLIDE 19

Application Class Data Sharing (AppCDS)

19

  • CDS for external libraries
  • Enables pre-processing of classes to be loaded
  • Speeds up start-up time
  • Was commercial only until JDK 10

Steps to use:

  • Identify a list of classes to cache
  • Generate the cache
  • Run application with AppCDS cache
slide-20
SLIDE 20

AppCDS: Generating shared cache file

20

  • Create classes.lst which stores list of classes to cache:
  • Create application cache from classes.lst at app-cds.jsa
slide-21
SLIDE 21

AppCDS: Run application with AppCDS cache

21

  • Run application with AppCDS cache:
  • Note the cache size:
slide-22
SLIDE 22

Benchmark Results: CDS vs AppCDS

22

slide-23
SLIDE 23

Ahead-of-time Compilation (AOT)

23

  • Compile java classes to native code
  • Skips JVM JIT compilation at start-up time
  • Restricted to Linux x64 systems running 64-bit Java
  • Available since JDK 9

Steps to use:

  • Identify a list of classes to cache
  • Generate the the AOT cache
  • Run application with AOT cache
slide-24
SLIDE 24

AOT: Generating shared cache file

24

  • Reuse classes.lst from AppCDS use case
  • Create AOT cache from classes.lst at lib.so
slide-25
SLIDE 25

AOT: Run application with CDS + AppCDS + AOT

25

  • Run application with AOT cache:
  • Note the cache size:
slide-26
SLIDE 26

Benchmark Results: CDS vs CDS + AppCDS + AOT

26

slide-27
SLIDE 27

Things to note

27

  • Down to experimentation
  • Optimisations depend on application mark-up
  • AOT Cache size trade-off
  • Bigger caches can increase start-up times!
  • Exclude some classes from AOT cache
  • Classpath on launch and cache must match
  • More benefits for multiple JVMs using caches
  • Cache is OS-specific!
slide-28
SLIDE 28

But wait… this is also a container talk…

28

  • Let’s make a new Dockerfile:
  • Java 11 for features
  • Use alpine-slim to reduce image size
  • Copy in the caches
  • We add the appropriate flags
slide-29
SLIDE 29

But wait… this is also a container talk…

29

  • Let’s build the image:

docker build -t java-container-opt:adoptopenjdk-11-alpine-slim .

  • Check the output:
slide-30
SLIDE 30
  • 3. Other optimizations

30

slide-31
SLIDE 31

An alternative JVM: OpenJ9

31

  • Contributed by IBM to the Eclipse Foundation in 2017
  • Boasts “Low memory footprint” & “Fast start-up time”
  • CDS and AOT implementations also included
  • Builds by AdoptOpenJDK available for Java 8+

Stephen Hellberg – Open J9: Compelling Java for Cloud Workloads

slide-32
SLIDE 32

OpenJ9: Putting it to the test (AdoptOpenJDK 11)

32

slide-33
SLIDE 33

OpenJ9: Putting it to the test (AdoptOpenJDK 8)

33

slide-34
SLIDE 34

They have images for it too!

34

slide-35
SLIDE 35

Demo #2

35

AdoptOpenJDK 11 (CDS, AppCDs, AOT) Vs. AdoptOpenJDK 8 OpenJ9 (CDS, AOT)

slide-36
SLIDE 36

Honorable mention 1: Graal and Subtrate VM

36

  • Takes AOT to a new level
  • Compiles Java code into self-contained executables
  • Does not run on the Java HotSpot VM
  • Runs in SubstrateVM in the executable
  • Boasts faster start-up times & lower runtime memory overhead

Duncan McGregor - Graal: Not Just a New JIT for the JVM

slide-37
SLIDE 37

Honorable mention 2: Using modules & jLink

37

  • Java modules and jLink introduced in JDK9
  • jLink identifies the modules required in the JRE
  • Strip the JRE of unused classes
  • Reduce the application size and improve performance
  • Requires coding efforts to embrace modules
slide-38
SLIDE 38

Try it out!

Workshop: https://github.com/IBMCodeLondon/java-containers101 Our London Meetup: https://www.meetup.com/IBM-Code-London

38

slide-39
SLIDE 39

39