Iván López @ilopmar
Iván López (@ilopmar)
Iván López @ilopmar
Serverless
Iván López @ilopmar
- Framework para microservicios en la JVM
- Ultra-ligero & reactive (basado en Netty)
- Java, Groovy & Kotlin
- Ahead of Time compilation (AoT)
- Sin reflection ni runtime proxies
- Arranque muy rápido
- Consumo de memoria bajo
- Natively Cloud Native
- Soporte para Serverless: AWS, Azure, Google Cloud, Oracle Cloud
- Soporte para GraalVM (mejorado en Micronaut 2.0)
Micronaut
Iván López @ilopmar
Serverless en Micronaut
Tipo aplicación Triggers
Application o
Serverless function
HTTP request a un único
endpoint
Application HTTP requests a múltiples
endpoints
Serverless function Eventos: S3, queue,
scheduled
Iván López @ilopmar
AWS Lambda
Iván López @ilopmar
Problema “cold start”
- AWS Lambda creas instancias bajo demanada
- Inicialización en primera petición
- En siguientes peticiones se reutiliza
- No sólo problema en Servless,
también con contenedores
Iván López @ilopmar
Inicialización
- AWS Lambda arranca JVM
- Java runtime carga e inicializa handler class
- Lambda llama al método handler
Iván López @ilopmar
Soluciones
- Aumentar la memoria de la Lambda
- Más memoria –> Más CPU
- Más memoria –> Más coste
- Usar static initizaliers para cargar clases
- Evitar reflection
- Evitar classpath scanning
- Eliminar dependencies no utilizadas
- Usar AWS SDK v2 (más modular)
- Evitar AWS auto-discovery
Iván López @ilopmar
- Substrate VM: compilar aplicaciones Java a binarios nativos
- https://www.graalvm.org
GraalVM
Iván López @ilopmar
Demo
- Misma app de ejemplo
- ICNDB API (Internet Chuck Norris DB)
- https://github.com/ilopmar/codemotion-madrid-2020-micronaut-aws
- joke-function-jvm
- joke-function-graalvm
- joke-app-jvm
- joke-app-graalvm
Show me
the code
Iván López @ilopmar
Joke-app JVM – Cold start
Iván López @ilopmar
Joke-app JVM – Cold start
RequestId: 3576dc46-98bf-453e-b9d8-5ea6ecb54789
Duration: 4308.04 ms
Billed Duration: 4400 ms
Memory Size: 512 MB Max
Memory Used: 226 MB
Init Duration: 4434.35 ms
Iván López @ilopmar
Joke-app JVM – Warm request
Iván López @ilopmar
Joke-app JVM – Warm request
RequestId: f6f6b28e-6cd6-4818-9fa8-88aabc6e8170
Duration: 542.91 ms
Billed Duration: 600 ms
Memory Size: 512 MB
Max Memory Used: 271 MB
Iván López @ilopmar
Joke-app JVM
$ time curl $API_ENDPOINT/jokes/category/nerdy
# Cold start
real 0m9,000s
# Warm
real 0m0,823s
real 0m0,785s
real 0m0,726s
real 0m0,341s
Iván López @ilopmar
Joke-app GraalVM – Cold start
Iván López @ilopmar
Joke-app GraalVM – Cold start
RequestId: b0a719a7-e570-41a2-add6-422e3c0c599e
Duration: 774.41 ms
Billed Duration: 1300 ms
Memory Size: 512 MB
Max Memory Used: 201 MB
Init Duration: 479.62 ms
Iván López @ilopmar
Joke-app GraalVM – Warm request
Iván López @ilopmar
Joke-app GraalVM – Warm request
RequestId: dff7db10-46b7-4825-a0ad-96abca5066f7
Duration: 463.04 ms
Billed Duration: 500 ms
Memory Size: 512 MB Max
Memory Used: 273 MB
Iván López @ilopmar
Joke-app GraalVM
$ time curl $API_ENDPOINT/jokes/category/nerdy
# Cold start
real 0m1,656s
# Warm
real 0m0,383s
real 0m0,460s
real 0m0,419s
real 0m0,261s
Iván López @ilopmar
Comparativa
Runtime Cold start Warm request
9 seg ~ 0,6 seg
1,6 seg ~ 0,3 seg
Iván López @ilopmar
Resumen
- Uso de Java y JVM
- Triggers
- Serverless function vs Application
- Micronaut hace muy sencilla la integración
- GraalVM mejora mucho el cold start
Iván López @ilopmar
Recursos
- @micronautfw
- https://gitter.im/micronautfw
- https://docs.micronaut.io
- https://guides.micronaut.io (*AWS guides)
- https://micronaut.io/launch
- https://github.com/micronaut-projects/micronaut-core
- https://github.com/micronaut-projects/micronaut-aws
¡Gracias!
@ilopmar
lopez.ivan@gmail.com
https://github.com/ilopmar
Iván López
https://bit.ly/codemotion-mn-aws
¿Preguntas?

Codemotion Madrid 2020 - Serverless con Micronaut