Serverless Spring

@david_syer, 2019


  • Serverless and functions
  • Servless platforms, including Riff
  • Spring Cloud Function


  • Event driven
  • Dynamic resource utilization, "scale to zero"
  • Billing per message
  • Focus on business logic
  • Easy integration with platform services
  • Nothing to install or maintain

Platform Abstractions


Credit: Olivier Tardieu, and John McKim

Platform Abstractions


Credit: Olivier Tardieu, and John McKim

Serverless Use Cases

  • Web hooks
  • Background jobs
  • Glue code (hands-free integration)

No Code is an Island


Amazon Lambda


Google Cloud Function


Microsoft Azure Functions


Alibaba Function Compute




Serverless Providers

  • (J) Amazon Lambda
  • Google Cloud Functions
  • (J) Azure Function
  • (J) Alibaba
  • (J) Riff and PFS
  • (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 Function

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 Function

public class Application {

  public Function<String, String> uppercase() {
    return value -> value.toUpperCase();

  public static void main(String[] args) {, args);

Plain Old Functions

package functions;

public class Uppercase implements Function<String, String> {

  String apply(String input) {
    return input.toUpperCase();


AWS Cold Starts

  • Lambda throttles CPU resources when memory is constrained
  • …​ also billing is proportional to memory usage
  • …​ it’s not that simple
Memory Cost

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

public class Application {

  public Function<Flux<String>, Flux<String>> uppercase() {
    return flux -> flux

  boolean isNotRude(String word) {

  public static void main(String[] args) {, args);

Spring Cloud Function Adapter


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