Safe Harbor
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
Java In Serverless Land
Serverless Java
Serverless Java
Java is...
  • the most popular language [TIOBE,]
  • Java is 3rd in serverless []
Serverless Java
Serverless Java in history
AWS Lambda launched November 2014, with Java support added June 2015. So why are Python & Node more popular?
  • FaaS as a scripting language for the web?
  • Early adopters had simple use-cases?
  • Doesn't fit normal Java development patterns?
  • JVM not suitable for short-lived apps?
Serverless Java
Serverless Java
Mature Java
What does it look like?
  • Plain Java
  • Wide choice of good tooling
  • Huge number of available libraries
  • Complex applications
  • Using the JVM effectively
... on FaaS?
Fn Project
What the Fn?
  • Open-source Serverless Compute Platform
  • Run anywhere: Datacenter / laptop / rPi
  • Functions are containers
  • Fully open (Apache 2.0) with commercial backing from Oracle
  • Fn Java
Demo time!
Fn Flow
fn flow
Fn Flow
What is Flow for?
  • Fn Flow lets you build reliable and scalable FaaS applications as functions
  • Flow functions only consume resources when they have work to do
  • Flow functions are written purely in code
  • Flow allows you to build distributed applications
  • Flow supports rich concurrency primitives including fork-join, chaining, delays and error handling
fn flow
Fn Flow: Vista
We want code to:
  • Hit the Flickr API for "license plate car usa"
  • In each image, look for specific license plates
  • If we find a plate we're looking for, draw a box around the license plate
  • Drop a message into Slack about it, and post to twitter
fn flow
Fn Flow: Vista
fn flow
Fn Flow: Vista
fn flow
Fn Flow: Vista
fn flow
Fn Flow: Vista
fn flow
Fn Flow: Vista
fn flow
Fn Flow
How does it even work?
  • Inspiration from features of Java 8
    • CompletableFuture
    • Lambdas
Under the Hood of Fn Flow
fn flow
CompletableFuture is a representation of a single asynchronous action. It is also a framework for composing these actions to define complex asynchronous behaviours.
  • Since JDK8
  • Part of java.util.concurrent
  • Uses ForkJoinPool
Fn Flow is a framework for composing distributed asynchronous actions, based on CompletableFuture.
fn flow
fn flow
fn flow
fn flow
fn flow
fn flow
Everyone knows Java 8 lambdas, right?
fn flow
Everyone knows Java 8 lambdas, right?
Did you know they can be serializable?
This is a cast expression - f implements Function and Serializable.
fn flow
Serializing Lambdas gives us a way to take compiled, type-safe, pure Java code and send it to the Fn Flow server to be run as a separate function:
  • independent concurrency and resource limits
  • billable on compute-time only
  • still testable
  • still debuggable and inspectable
Java In Container Land
Java in Container Land
Java in containers
What do we want containerized JVMs to do?
  • Start fast
  • Run in small images
  • Respect resource constraints
Java in Container Land
Fast JVM startup
Moving startup costs to build-time also allows us to share pre-built assets between JVMs. How can we take advantage of container deployment patterns?
  • libc, can be shared (assuming same layer/file/inode)
  • What else can we share?
    • Class Data Sharing (CDS)
    • Application CDS
    • AOT compilation
Java in Container Land
Fast JVM startup - CDS
  • Like an shared object file but for Java Class Data
  • Memory-mapped, ReadOnly + COW
  • Class data read from memory without reading/parsing/verifying classes from Jar files
  • Class data can be shared between containers
Java in Container Land
Fast JVM startup - AppCDS
Application CDS is like "regular" CDS, but for application classes.
  • Cache size & speedup depends on how big your application's classes are
  • Memory saving depends on number of containers sharing the same cache
  • (eg) Several seconds and hundreds of MB saved with AppCDS using WebLogic
Java in Container Land
Fast JVM startup - AOT
Ahead-of-time compilation for Java classes. Does some of the work that HotSpot would do in compiling bytecode to native, at build-time.
  • Shared memory
  • Reduced footprint
  • Quicker startup
  • Optionally include profiling paths
Java in Container Land
Smaller images
Smaller JVM container images mean faster startup:
  • Transferring images to the host
  • COW caches
  • Overlay FS
  • jlink - remove unused core libraries
  • Smaller base images
  • Substrate VM - Compile Java to a static binary
Java in Container Land
Smaller images - jlink
Remove parts of Java standard library that you don't use:
  • Only includes the jdk9 modules you use
  • Make even smaller with --compress
  • Also: --vm minimal includes only Serial GC and client compiler - size reduced to ~15MB
Java in Container Land
Smaller images - Alpine & Musl
Ubuntu / Debian / Oracle Linux base images can be 100s of MB, even the "slim" versions. We don't care much about the base image so long as it can run Java. Project Portola makes the JVM run on Alpine Linux/Musl.

  • Alpine Linux is a security-oriented, lightweight Linux distribution based on musl libc and busybox. Alpine base image is 4mb (!)

  • Musl is lightweight, fast, simple, free, and strives to be correct in the sense of standards-conformance and safety.
Java in Container Land
Smaller images - Substrate VM
Substrate VM from Oracle Labs compiles Java source to a single binary, like C or Golang.
  • Part of Graal project
  • Tiny image sizes (>10mb)
  • Small VM overhead
  • Experimental, in active development
  • No support for dynamic code generation, serialization
  • Limited support for reflection
Java in Container Land
Respecting Resource Constraints
The JVM self-tunes to the underlying system (aka Ergonomics).
  • Memory
    • Heap size
    • GC region sizes
    • JIT code cache sizes
  • CPU
    • ThreadPool sizes
    • Runtime.availableProcessors()
This is not transparent - requires JVM to support cgroups resource limitations. Much good stuff landed recently for JDK10. See JDK-8146115 for more details.
Java in Container Land
Being Container Friendly
Other work:
  • JDK-8179498: attach in linux should be relative to /proc/pid/root and namespace aware (JDK 10)
  • JDK-8193710: jcmd -l and jps commands do not list Java processes running in Docker containers (JDK 11)
  • And more... JDK-8182070: Container aware Java
Thank you for listening
Java In Serverless Land :: @MaximumGilliard :: DevNexus, Atlanta, 22 Feb 2018