The presentation I presented with my collegue Wim, at Spring IO Barcelona 2019.
https://2019.springio.net/sessions/migrating-a-modern-spring-web-application-to-serverless
In this presentation we want to share how we transformed a typical spring realtime web into a serverless application and the challenges we encountered to make this a serverless application for easy scaling and resource optimization. We will share our experiences and give you tips and tricks on what we learned in the process, like:
- Spring resource optimization
- Server side rendering versus fat clients
- Session management
- Realtime UI updates in serverless environments
- Connecting functions
- Security
- Portability between cloud providers
- …
2. www.faros.be
Cloud Native Enterprise
Development
• Software development / Projects
• IT Consultancy
• Audit/training
Technology focus
• Spring Ecosystem
• Pivotal CloudFoundry
Migrating a modern spring web application to serverless
Serverless
Reactive
Leuven (Belgium)
https://www.meetup.com/Serverless-Belgium
3. www.faros.be
Migrating a modern spring web application to serverless
Migrating a modern spring web application to serverless
https://freepik.com
Why?
5. www.faros.be
S e r v e r l e s s a r c h i t e c t u r e s a r e a p p l i c a t i o n d e s i g n s t h a t i n c o r p o r a t e t h i r d - p a r t y
“ B a c k e n d a s a S e r v i c e ” ( B a a S ) s e r v i c e s a n d / o r c u s t o m c o d e r u n i n m a n a g e d ,
e p h e m e r a l c o n t a i n e r s o n a “ F u n c t i o n s a s a S e r v i c e ” ( F a a S ) p l a t f o r m
Migrating a modern spring web application to serverless
M i k e R o b e r t s
6. www.faros.be
Migrating a modern spring web application to serverless
• Goals
• ‘Traditional’ vs Serverless architecture
• Functionality
• Web / Websockets / Database / Security
• Serverless landscape (tooling / frameworks / platform maturity)
• Best practices (a practice? maturity?)
• Inter-platform compatibility
• Scalable
• Spring support (Cloud Function)
Migrating a modern spring web application to serverless
7. www.faros.beMigrating a modern spring web application to serverless
You have to let it all go.
Fear, doubt and disbelief.
Free your mind
12. www.faros.be
Server
Technical approach
Migrating a modern spring web application to serverless
S e r v e r l e s s a r c h i t e c t u r e – p a r t 1
Service
SpringDataJPA
SQL
db
MVC/Vaadin
internet
Infrastructure As Code
JSON Server
BaaS
13. www.faros.be
Technical approach
Migrating a modern spring web application to serverless
Facade Service Data
Interface Impl
deliverydayService.findByDateAndLocation(date, location);
slotService.findSlotsByDateAndLocation(deliveryday);getSlots(Date date, long locationId)
APIGateway
AWSstepfct
AWS Lambda LayerJARJAR JAR
14. www.faros.be
Technical approach
• Business logic
• Java
• Spring Cloud Function (Abstraction)
• Supportive
• Interpreted language (NodeJS)
• Mix/combine programming languages
Migrating a modern spring web application to serverless
P r o g r a m m i n g L a n g u a g e
15. www.faros.be
Technical approach
• Public
• AWS Lambda
• Azure Functions
• Google Cloud Functions
• On-Prem / OSS
• PFS
• OpenWhisk
• Fission
• Kubeless
• OpenFaaS
• Oracle fn
• ...
Migrating a modern spring web application to serverless
P l a t f o r m s
18. www.faros.be
Implementation
• Decouple development from any specific runtime
• Uniform programming model
• Run standalone (locally or in a PaaS)
• Enable Spring Boot features (auto-configuration, DI, metrics)
Migrating a modern spring web application to serverless
S p r i n g C l o u d F u n c t i o n
19. www.faros.be
Implementation
Migrating a modern spring web application to serverless
S p r i n g C l o u d F u n c t i o n
@Bean
Function f() {
...
}
AWS
Lambda
Apache
OpenWhisk
Azure
Spring
Boot App
REST
f() f() f()
f()
Spring
Boot App
Task
f()
Spring
Boot App
Stream
f()
20. www.faros.be
Implementation
Migrating a modern spring web application to serverless
S p r i n g C l o u d F u n c t i o n
f
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-aws</artifactId>
</dependency>
• Add mvn dependency
21. www.faros.be
Implementation
Migrating a modern spring web application to serverless
B u i l d
f
• Jar layout
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>1.0.21.RELEASE</version>
</dependency>
</dependencies>
</plugin>
...
</plugins>
22. www.faros.be
Implementation
Migrating a modern spring web application to serverless
B u i l d
f
• Jar layout
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<finalName>get-reservation</finalName>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</plugin>
</plugins>
23. www.faros.be
Implementation
• SAM for building serverless applications
• Package functions (ZIP)
Migrating a modern spring web application to serverless
B u i l d
+ set MAIN_CLASS environment variable
24. www.faros.be
Implementation
Migrating a modern spring web application to serverless
Facade Service Data
Interface Impl
APIGateway
AWSstepfct
AWS Lambda LayerJARJAR JAR
S t e p f u n c t i o n s
25. www.faros.be
Implementation
• Orchestration
• Statemachine
• Patterns
• Workflow logic
Tasks, choice, pass, fail, (parallel)
• Error handling
• Timeouts/retries
Migrating a modern spring web application to serverless
S t e p f u n c t i o n s
26. www.faros.be
Implementation
• Testing serverless (microservices) is hard
Migrating a modern spring web application to serverless
T e s t i n g
Test pyramid
https://medium.freecodecamp.org/the-best-ways-to-test-your-serverless-applications-40b88d6ee31e
Serverless Test pyramid
32. www.faros.be
terraform {
backend "s3" {
bucket = “myapp-terraform-state"
key = "tst_serverless_myapp.tfstate"
region = "eu-west-1"
profile = “..."
}
}
provider "aws" {
region = "${var.aws_region}"
profile = "${var.aws_profile}"
}
resource "aws_db_instance" "db_instance" {
name = “myapp-db"
engine = "postgres"
engine_version = "10.6“
...
}
Operations
• Terraform (HashiCorp)
• Open source
• Multi-platform support
• Independant state
• Integration
• Missing API Gateway websocket
Migrating a modern spring web application to serverless
I n f r a s t r u c t u r e A s C o d e
33. www.faros.be
Operations
• SAM (building serverless applications)
• SAM template (uses Cloudformation)
• Local Testing and Debugging
• Build automation
• Built-in best practices (ex. gradual deployments)
Migrating a modern spring web application to serverless
A W S S A M
34. www.faros.be
Operations
Migrating a modern spring web application to serverless
I n f r a s t r u c t u r e A s C o d e
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
GetReservationsFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: function/get-reservations
Handler: function.reservation.get.Handler
Runtime: java8
Events:
GetReservations:
Type: Api
Properties:
RestApiId: !Ref ReservationsApi
Path: /reservations
Method: get
....
SAM Translator
AWSTemplateFormatVersion: '2010-09-09'
Resources:
....
Cloudformation
templateSAM template
35. www.faros.beMigrating a modern spring web application to serverless
#1 REST API
#1 WS API
#2 db
#1 AWS Step Function
AWS Secrets Manager
#10 AWS access roles
#7 fct
#1 Template!
36. www.faros.be
Operations
• CI/CD
• AWS CodeBuild / AWS CodeDeploy / AWS CodePipeline
• Monitoring
• CloudWatch
• AWS X-Ray
Migrating a modern spring web application to serverless
O t h e r
38. www.faros.be
Conclusions
• Still quite new
• Little guidance / expertise / resources
• Trial & error
• Focus on 1 platform
• Distributed systems/patterns!
• Idempotent, stateless, transactions, ...
• Serverless != #NoOps
• Code
• Organize functions is important
• Java might not be your default language choice
• Mix/combine programming languages based on needs
Migrating a modern spring web application to serverless
39. www.faros.be
Conclusions
• No walk in the park
• Limited resources (doc)
• Little experience
• Operations (deploy with versioning, ...)
• Linking of small functions
• Design patterns (event-driven, SRP)
• Reusability
• State machine
Migrating a modern spring web application to serverless
41. www.faros.be
Contact
Gaston Geenslaan 11 B4
3000 Leuven
hello@faros.be
+32(0)16 240 840
Migrating a modern spring web application to serverless
@FarosBelgium farosbe