SPRING
SERVERLESS
@smaldini
AGENDA
▸ Serverless and functions
▸ Serverless platforms
▸ Spring Cloud Functions
http://github.com/spring-cloud/spring-cloud-function
http://projectreactor.io/
SERVERLESS
▸ Event Driven
▸ Dynamic resource utilization, “scale to zero”
▸ Billing per Message
▸ Focus on Business Logic
▸ Easy Integration with Platform Services
CNCF https://github.com/cncf/wg-serverless/tree/master/whitepaper
AMAZON LAMBDA
GOOGLE CLOUD FUNCTION
MICROSOFT AZURE FUNCTIONS
SERVERLESS PROVIDERS
▸ (J) Amazon Lambda
▸ Google Cloud Functions
▸ (J) Azure Functions
▸ (J) Riff https://github.com/projectriff
▸ (J) IBM OpenWhisk
▸ (J) Oracle Fn
▸ OpenFaas
▸ Fission
▸ Kubeless
▸ …
(J) = native Java support
Others can run Java, e.g. via custom container or node JRE launcher.
JAVA UTIL FUNCTIONS
public interface Function<T, R> {
R apply(T t);
}
public interface Consumer<T> {
void accept(T t);
}
public interface Supplier<T> {
T get();
}
SPRING CLOUD FUNCTIONS
@SpringBootApplication
public class Application {
@Bean
public Function<String, String> uppercase() {
return value -> value.toUpperCase();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
PLAIN OLD FUNCTIONS
package functions;
public class Uppercase implements Function<String, String> {
String apply(String input) {
return input.toUpperCase();
}
}
AWS COLD START
• Lambda throttles CPU resources when memory is constrained
• … also billing is proportional to memory usage
• … it’s not that simple
SPRING CLOUD FUNCTION
All the benefits of serverless, but with full access to Spring (dependency injection, integrations,
autoconfiguration) and build tools (testing, continuous delivery, run locally)
For Spring devs: a smaller, more familiar step than using FaaS APIs and UIs natively
For Functionistas: no need to know anything about Spring
Decouple lifecycle of business logic from runtime platform. Run the same code as a web endpoint,
a stream processor, or a task
Uniform programming model across serverless providers, and also able to run standalone

(locally or in a PaaS)
PROJECT REACTOR
public abstract class Flux<T> implements Publisher<T> {
...
}
public abstract class Mono<T> implements Publisher<T> {
...
}
SPRING CLOUD FUNCTION
@SpringBootApplication
public class Application {
@Bean
public Function<Flux<String>, Flux<String>> uppercase() {
return flux -> flux
.filter(this::isNotRude)
.map(String::toUpperCase);
}
boolean isNotRude(String word) {
...
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
SPRING CLOUD FUNCTION
SERVERLESS PROVIDER
ADAPTER
USER FUNCTION
SPRING BOOT
HTTP MESSAGE
SPRING CLOUD FUNCTION
1.Programming model: @Beans of type Function, Consumer and Supplier,
with Flux, Mono, Publisher
2.Component scan for functions (e.g. execute jar with no dependency on Spring at all)
3.Compile strings which are Java function bodies
4.Bind and invoke from an isolated classloader (JVM packing, platform)
5.Adapters for Spring MVC, Spring Cloud Stream, AWS Lambda, Azure, Riff and other
"serverless" service providers
LEARNINGS
1.Spring is not slow or "heavy" on its own, classes loaded is important
2.AWS Lambda containers are slow
3.Warm JVM starts app much faster (10x) → class loader isolation
4.Functions are glue code anyway - not latency sensitive
5.Scale to Zero
6.Platform services and integration spaghetti
LINKS
• Spring Cloud Function: https://github.com/spring-cloud/spring-cloud-function
• Riff: https://github.com/projectriff/riff
• Spring Boot Thin Launcher: https://github.com/dsyer/spring-boot-thin-launcher
• Spring Initializr: http://start.spring.io
• Spring Cloud: http://cloud.spring.io
• Reactor: http://projectreactor.io
THANKS!

Serverless Spring by Stephane Maldini

  • 1.
  • 2.
    AGENDA ▸ Serverless andfunctions ▸ Serverless platforms ▸ Spring Cloud Functions http://github.com/spring-cloud/spring-cloud-function http://projectreactor.io/
  • 3.
    SERVERLESS ▸ Event Driven ▸Dynamic resource utilization, “scale to zero” ▸ Billing per Message ▸ Focus on Business Logic ▸ Easy Integration with Platform Services CNCF https://github.com/cncf/wg-serverless/tree/master/whitepaper
  • 4.
  • 5.
  • 6.
  • 7.
    SERVERLESS PROVIDERS ▸ (J)Amazon Lambda ▸ Google Cloud Functions ▸ (J) Azure Functions ▸ (J) Riff https://github.com/projectriff ▸ (J) IBM OpenWhisk ▸ (J) Oracle Fn ▸ OpenFaas ▸ Fission ▸ Kubeless ▸ … (J) = native Java support Others can run Java, e.g. via custom container or node JRE launcher.
  • 8.
    JAVA UTIL FUNCTIONS publicinterface Function<T, R> { R apply(T t); } public interface Consumer<T> { void accept(T t); } public interface Supplier<T> { T get(); }
  • 9.
    SPRING CLOUD FUNCTIONS @SpringBootApplication publicclass Application { @Bean public Function<String, String> uppercase() { return value -> value.toUpperCase(); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
  • 10.
    PLAIN OLD FUNCTIONS packagefunctions; public class Uppercase implements Function<String, String> { String apply(String input) { return input.toUpperCase(); } }
  • 11.
    AWS COLD START •Lambda throttles CPU resources when memory is constrained • … also billing is proportional to memory usage • … it’s not that simple
  • 12.
    SPRING CLOUD FUNCTION Allthe benefits of serverless, but with full access to Spring (dependency injection, integrations, autoconfiguration) and build tools (testing, continuous delivery, run locally) For Spring devs: a smaller, more familiar step than using FaaS APIs and UIs natively For Functionistas: no need to know anything about Spring Decouple lifecycle of business logic from runtime platform. Run the same code as a web endpoint, a stream processor, or a task Uniform programming model across serverless providers, and also able to run standalone
 (locally or in a PaaS)
  • 13.
    PROJECT REACTOR public abstractclass Flux<T> implements Publisher<T> { ... } public abstract class Mono<T> implements Publisher<T> { ... }
  • 14.
    SPRING CLOUD FUNCTION @SpringBootApplication publicclass Application { @Bean public Function<Flux<String>, Flux<String>> uppercase() { return flux -> flux .filter(this::isNotRude) .map(String::toUpperCase); } boolean isNotRude(String word) { ... } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
  • 15.
    SPRING CLOUD FUNCTION SERVERLESSPROVIDER ADAPTER USER FUNCTION SPRING BOOT HTTP MESSAGE
  • 16.
    SPRING CLOUD FUNCTION 1.Programmingmodel: @Beans of type Function, Consumer and Supplier, with Flux, Mono, Publisher 2.Component scan for functions (e.g. execute jar with no dependency on Spring at all) 3.Compile strings which are Java function bodies 4.Bind and invoke from an isolated classloader (JVM packing, platform) 5.Adapters for Spring MVC, Spring Cloud Stream, AWS Lambda, Azure, Riff and other "serverless" service providers
  • 17.
    LEARNINGS 1.Spring is notslow or "heavy" on its own, classes loaded is important 2.AWS Lambda containers are slow 3.Warm JVM starts app much faster (10x) → class loader isolation 4.Functions are glue code anyway - not latency sensitive 5.Scale to Zero 6.Platform services and integration spaghetti
  • 18.
    LINKS • Spring CloudFunction: https://github.com/spring-cloud/spring-cloud-function • Riff: https://github.com/projectriff/riff • Spring Boot Thin Launcher: https://github.com/dsyer/spring-boot-thin-launcher • Spring Initializr: http://start.spring.io • Spring Cloud: http://cloud.spring.io • Reactor: http://projectreactor.io
  • 19.