15 Ways to Optimize Spring
Boot for the Cloud ☁️
Billy Korando - @BillyKorando
Developer Advocate - IBM
18 Ways to Optimize Spring
Boot for the Cloud ☁️
Billy Korando - @BillyKorando
Developer Advocate - IBM
Keep Current
http://ibm.biz/javanewsletter
https://cloud.ibm.com/docs/java
🥅⚽️
🥅
🥅
Improvement 🥅
🔎Observability
Efficiency ♻️
💣Resiliency
⚖️Scalability
Security 🔒
Optimization Factors
Efficiency 💰
Sources/Additional Reading
1. You Don’t Have to Do Microservices to be on the Cloud
ORDERS
ITEMS ORDER
HISTORY
ADDRESSES
USER
FULFILLMENT
The Distributed Monolith
1. You Don’t Have to Do Microservices to be on the Cloud
Challenges with Microservices:
 Distributed Systems Have Emerging Problems
 Require Organizational Change
 Deep Understand of DDD and Your Domain
1. You Don’t Have to Do Microservices to be on the Cloud
With Monoliths You Can:
 Use Spring Boot
 Can be CI/CD
2. Use the Services Provided by Your Cloud Platform
☁️
2. Use the Services Provided by Your Cloud Platform
☁️
3. Spring Cloud Sleuth for Request Tracing and Correlation
3. Spring Cloud Sleuth for Request Tracing and Correlation
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
🔥
3. Spring Cloud Sleuth for Request Tracing and Correlation
service1,2485ec27856c56f4,2485ec27856c56f4
service2,2485ec27856c56f4,9aa10ee6fbde75fa
service3,2485ec27856c56f4,1210be13194bfe5
service2,2485ec27856c56f4,9aa10ee6fbde75fa
service4,2485ec27856c56f4,1b1845262ffba49d
service2,2485ec27856c56f4,9aa10ee6fbde75fa
service1,2485ec27856c56f4,2485ec27856c56f4
Trace ID Span ID
3. Spring Cloud Sleuth for Request Tracing and Correlation
Project Dapper
Time well spent
Time poorly spent
4. Use and Customize Spring Boot Actuator
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
/health
/info
/metrics /prometheus
/configprops
And many more!
https://docs.spring.io/spring-
boot/docs/current/reference/html/production-ready-endpoints.html
4. Use and Customize Spring Boot Actuator
Health
Default, scans for various datasources and caches
Can also be customized:
5. Use Pools to Meter Resource Usage
🔥🔥
5. Use Pools to Meter Resource Usage
⏱
6. Use Circuit Breakers to Limit Cascading Failures
🔥🔥
6. Use Circuit Breakers to Limit Cascading Failures
🔥
@HystrixCommand(doFallback)
V
7. Be Stateless When Possible
🔥
8. Use Spring Cloud Contract to Validate Contracts Between Services
POST /bookings
{
customerId : 1
roomNumber : 100
}
Person Client Person Service
POST
http://dev/bookings
ITAddPerson ✅🔴
🔥
8. Use Spring Cloud Contract to Validate Contracts Between Services
Bookings Contract
8. Use Spring Cloud Contract to Validate Contracts Between Services
POST /bookings
{
customerId : 1
roomNumber : 100
}
Person Client Person Service
Contract
POST
http://localhost/bookings
ITAddPerson ✅
9. Use TestContainers for Other Services
INSERT INTO
PERSONS …
Person Client Dev Database
ITAddPersonDB ✅
PERSONS
TABLE
🔥
🔴
9. Use TestContainers for Other Services
INSERT INTO
PERSONS …
Person Client
Local Docker Database
ITAddPersonDB ✅
PERSONS
TABLE
10. Have a Continuous Delivery Pipeline to Production
👩💻👨💻
Code Repo
🔨
Build Server
☁️
✅
✅
✅
11. Keep Dependencies Up-to-Date
The Code
Your Code
Runs On
The Code
YouWrote
Security patches
Performance
enhancements
Feature
enhancements
12. Use Eclipse OpenJ9
OpenJ9
Hotspot
GraalVM
Corretto
12. Use Eclipse OpenJ9
Memory savings of ~40%
Near same throughput
Totally Free to Use
Eclipse Public License 2.0
Apache License 2.0
Download at: https://adoptopenjdk.net/
More info: https://www.eclipse.org/openj9/
So OpenJ9 splits the
class into two parts
Classfile
ROMClass
J9RAMClass
Not a great way
to store class
information
The read only part
position independent
The stateful
part
13. Use Eclipse OpenJ9 class sharing
JVM 1 JVM 2 JVM 3
13. Use Eclipse OpenJ9 class sharing
JVM 1 JVM 2 JVM 3
All these classes are identical
13. Use Eclipse OpenJ9 class sharing
JVM 1 JVM 2 JVM 3
So let’s share them!
13. Use Eclipse OpenJ9 class sharing
13. Use Eclipse OpenJ9 class sharing
-Xshareclasses
Enables class sharing
-Xscmx=Xm
Sets the size of the cache, default 300 MB
https://developer.ibm.com/articles/eclipse-openj9-class-sharing-in-docker-
containers/
https://developer.ibm.com/tutorials/j-class-sharing-openj9/
14. Use Cloud Configuration for Zero-Downtime Config Changes
👩💻👨💻
Config Server Config Client
15. Tune Your Spring Boot Application
● Explode your jar
● Review which features you’re actually using
○ Actuator
○ spring.jmx.enabled=false
● Exclude unneeded dependencies
● Be on the latest version of Spring (Boot)
● Tune with JVM Args, e.g. –noverify
● Lazy load beans
15. Tune Your Spring Boot Application
Spring Boot isn’t Memory Hungry
15. Tune Your Spring Boot Application
https://spring.io/blog/2018/12/12/how-fast-is-spring
https://www.youtube.com/watch?v=97UTDmonq7w
16. Use A/B Testing to Tune Performance
{
…
}
{
…
}
17. Introduce Chaos into Production
🕥
10:30AM
🔥
🔥
Review
1. You Don’t Have to do Microservices to be in the Cloud
2. Use the Services Provide by Cloud Platform for Monitoring
3. Use Spring Cloud Sleuth for Request Tracing and Correlation
4. Use and Customize Spring Boot Actuator
5. Use Pools to Meter Resource Usage
6. Use Circuit Breakers to Limit Cascading Failures
7. Be Stateless When Possible
8. Use Spring Cloud Contract to Verify Integration Points Between Services
9. Use TestContainers to Verify Integration Points Between Systems
Review
10.Have a Continuous Delivery Pipeline to Production
11.Keep Dependencies Up-to-Date
12.Switch to Eclipse OpenJ9
13.Enable OpenJ9 Class Sharing
14.Use Cloud Configuration for Zero-Downtime Config Changes
15.Tune Your Spring Boot Application
16.Use A/B Testing to Measure Optimizations
17.Introduce Chaos into Production
15 Ways for to Optimize Spring Boot for the Cloud
Q&A
@BillyKorando
Feedback: schedule.devnexus.com

15-ways-to-optimize-spring-boot-for-the-cloud

  • 1.
    15 Ways toOptimize Spring Boot for the Cloud ☁️ Billy Korando - @BillyKorando Developer Advocate - IBM
  • 2.
    18 Ways toOptimize Spring Boot for the Cloud ☁️ Billy Korando - @BillyKorando Developer Advocate - IBM
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
    1. You Don’tHave to Do Microservices to be on the Cloud ORDERS ITEMS ORDER HISTORY ADDRESSES USER FULFILLMENT The Distributed Monolith
  • 8.
    1. You Don’tHave to Do Microservices to be on the Cloud Challenges with Microservices:  Distributed Systems Have Emerging Problems  Require Organizational Change  Deep Understand of DDD and Your Domain
  • 9.
    1. You Don’tHave to Do Microservices to be on the Cloud With Monoliths You Can:  Use Spring Boot  Can be CI/CD
  • 10.
    2. Use theServices Provided by Your Cloud Platform ☁️
  • 11.
    2. Use theServices Provided by Your Cloud Platform ☁️
  • 12.
    3. Spring CloudSleuth for Request Tracing and Correlation
  • 13.
    3. Spring CloudSleuth for Request Tracing and Correlation <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> 🔥
  • 14.
    3. Spring CloudSleuth for Request Tracing and Correlation service1,2485ec27856c56f4,2485ec27856c56f4 service2,2485ec27856c56f4,9aa10ee6fbde75fa service3,2485ec27856c56f4,1210be13194bfe5 service2,2485ec27856c56f4,9aa10ee6fbde75fa service4,2485ec27856c56f4,1b1845262ffba49d service2,2485ec27856c56f4,9aa10ee6fbde75fa service1,2485ec27856c56f4,2485ec27856c56f4 Trace ID Span ID
  • 15.
    3. Spring CloudSleuth for Request Tracing and Correlation Project Dapper Time well spent Time poorly spent
  • 16.
    4. Use andCustomize Spring Boot Actuator <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> /health /info /metrics /prometheus /configprops And many more! https://docs.spring.io/spring- boot/docs/current/reference/html/production-ready-endpoints.html
  • 17.
    4. Use andCustomize Spring Boot Actuator Health Default, scans for various datasources and caches Can also be customized:
  • 18.
    5. Use Poolsto Meter Resource Usage 🔥🔥
  • 19.
    5. Use Poolsto Meter Resource Usage ⏱
  • 20.
    6. Use CircuitBreakers to Limit Cascading Failures 🔥🔥
  • 21.
    6. Use CircuitBreakers to Limit Cascading Failures 🔥 @HystrixCommand(doFallback) V
  • 22.
    7. Be StatelessWhen Possible 🔥
  • 23.
    8. Use SpringCloud Contract to Validate Contracts Between Services POST /bookings { customerId : 1 roomNumber : 100 } Person Client Person Service POST http://dev/bookings ITAddPerson ✅🔴 🔥
  • 24.
    8. Use SpringCloud Contract to Validate Contracts Between Services Bookings Contract
  • 25.
    8. Use SpringCloud Contract to Validate Contracts Between Services POST /bookings { customerId : 1 roomNumber : 100 } Person Client Person Service Contract POST http://localhost/bookings ITAddPerson ✅
  • 26.
    9. Use TestContainersfor Other Services INSERT INTO PERSONS … Person Client Dev Database ITAddPersonDB ✅ PERSONS TABLE 🔥 🔴
  • 27.
    9. Use TestContainersfor Other Services INSERT INTO PERSONS … Person Client Local Docker Database ITAddPersonDB ✅ PERSONS TABLE
  • 28.
    10. Have aContinuous Delivery Pipeline to Production 👩💻👨💻 Code Repo 🔨 Build Server ☁️ ✅ ✅ ✅
  • 29.
    11. Keep DependenciesUp-to-Date The Code Your Code Runs On The Code YouWrote Security patches Performance enhancements Feature enhancements
  • 30.
    12. Use EclipseOpenJ9 OpenJ9 Hotspot GraalVM Corretto
  • 31.
    12. Use EclipseOpenJ9 Memory savings of ~40% Near same throughput Totally Free to Use Eclipse Public License 2.0 Apache License 2.0 Download at: https://adoptopenjdk.net/ More info: https://www.eclipse.org/openj9/
  • 32.
    So OpenJ9 splitsthe class into two parts Classfile ROMClass J9RAMClass Not a great way to store class information The read only part position independent The stateful part 13. Use Eclipse OpenJ9 class sharing
  • 33.
    JVM 1 JVM2 JVM 3 13. Use Eclipse OpenJ9 class sharing
  • 34.
    JVM 1 JVM2 JVM 3 All these classes are identical 13. Use Eclipse OpenJ9 class sharing
  • 35.
    JVM 1 JVM2 JVM 3 So let’s share them! 13. Use Eclipse OpenJ9 class sharing
  • 36.
    13. Use EclipseOpenJ9 class sharing -Xshareclasses Enables class sharing -Xscmx=Xm Sets the size of the cache, default 300 MB https://developer.ibm.com/articles/eclipse-openj9-class-sharing-in-docker- containers/ https://developer.ibm.com/tutorials/j-class-sharing-openj9/
  • 37.
    14. Use CloudConfiguration for Zero-Downtime Config Changes 👩💻👨💻 Config Server Config Client
  • 38.
    15. Tune YourSpring Boot Application ● Explode your jar ● Review which features you’re actually using ○ Actuator ○ spring.jmx.enabled=false ● Exclude unneeded dependencies ● Be on the latest version of Spring (Boot) ● Tune with JVM Args, e.g. –noverify ● Lazy load beans
  • 39.
    15. Tune YourSpring Boot Application Spring Boot isn’t Memory Hungry
  • 40.
    15. Tune YourSpring Boot Application https://spring.io/blog/2018/12/12/how-fast-is-spring https://www.youtube.com/watch?v=97UTDmonq7w
  • 41.
    16. Use A/BTesting to Tune Performance { … } { … }
  • 42.
    17. Introduce Chaosinto Production 🕥 10:30AM 🔥 🔥
  • 43.
    Review 1. You Don’tHave to do Microservices to be in the Cloud 2. Use the Services Provide by Cloud Platform for Monitoring 3. Use Spring Cloud Sleuth for Request Tracing and Correlation 4. Use and Customize Spring Boot Actuator 5. Use Pools to Meter Resource Usage 6. Use Circuit Breakers to Limit Cascading Failures 7. Be Stateless When Possible 8. Use Spring Cloud Contract to Verify Integration Points Between Services 9. Use TestContainers to Verify Integration Points Between Systems
  • 44.
    Review 10.Have a ContinuousDelivery Pipeline to Production 11.Keep Dependencies Up-to-Date 12.Switch to Eclipse OpenJ9 13.Enable OpenJ9 Class Sharing 14.Use Cloud Configuration for Zero-Downtime Config Changes 15.Tune Your Spring Boot Application 16.Use A/B Testing to Measure Optimizations 17.Introduce Chaos into Production
  • 45.
    15 Ways forto Optimize Spring Boot for the Cloud Q&A @BillyKorando Feedback: schedule.devnexus.com

Editor's Notes

  • #2 Poll: how many are on the Cloud in PROD? Poll: how many in the process or planning on migrating to the Cloud? Submitted the talk, worked a lot with Spring Boot Felt 15 good tips for optimizing
  • #3 However ended up with 18 So you are getting 20% free!
  • #4 Benefits of Java newsletter Just started, focus on what is happening in the java ecosystem Cloud ibm java, great documentation for running applications on the Cloud for both Java/JakartaEE and Spring
  • #5 Optimization isn’t a single goal Optimization isn’t a single state either Applications will have to be continually changed and re-optimized in response to changing business needs and demands To that end, this presentation is more aimed towards developers/orgs new to the cloud, going to the cloud, rather than very mature cloud orgs
  • #6 Observability – we have to know what is happening within our applications so then we can optimize them Resiliency – problems will eventually occur, how do we contain failures, limit the apparent problems to customers/clients, and recover Scalability – the only constant with demand is it will change. How can our applications and system at large adapt from zero requests to thousands and back down to zero Security – Security is critical, clients/customers give us sensitive info, we need to hold it in confidence Improvement – How can we setup a system so they we can continually improve our applications over time Efficiency – how to make the best use of resources available, ultimately this is going to be about saving our organizations money
  • #7 Domain driven design, important for understanding how to break apart services, concepts like bounded context aggregates Continuous delivery, importance of automation, auditability, repeatability, addresses a lot of the underlying issues developers face Release It! A lot of important principles on how to build reliable and scalable services in production (new version out) Cloud Native Java, takes elements from previous books, with a heavy spring focus
  • #8 Not a hot take that microservices are bad Been a part of a lot of microservice migrations that were linked with cloud migrations We have some different tables/models When breaking up the monolith we put these models into different services We though communication was going to go like this But ended up being more like this
  • #9 A lot to consider when weighing monoliths versus microservices Increase network I/O Organizational changes Need deep understanding at least senior technical levels of underlying concepts DDD A number of benefits commonly associated with microservices are problems that have to be solved to enable microservices, Not characteristics of microsrvices CI/CD Spring Boot To some degree move away from monolith, but those decisions need to be based on real world
  • #10 A lot to consider when weighing monoliths versus microservices Increase network I/O Organizational changes Need deep understanding at least senior technical levels of underlying concepts DDD A number of benefits commonly associated with microservices are problems that have to be solved to enable microservices, Not characteristics of microsrvices CI/CD Spring Boot To some degree move away from monolith, but those decisions need to be based on real world
  • #11 So what do we do with all this data? Cloud providers also provide services for monitoring your applications Graphs Log aggregation And others Optimization isn’t just about code, but also optimizing your developer time, Don’t spend time reinventing solved problems like aggregation, tracing ,graphing, etc, use what’s already available
  • #13 Start understanding flow and usage of app with spring sleuth
  • #14 Just need these dependencies
  • #15 Add trace and span ids
  • #16 Can also trace requests to see how much time is being spent where in the app Read Project Dapper, fantastic read on how google used tracing and correlation to figure out how their applications where actually interacting And to drive decision making into where to spend time optimizing
  • #17 Like sleuth, can be activated by bringing in a dependency
  • #18 Default checks for a number of data sources and caches Full list can be seen here But can also have custom health checks as well
  • #19 With your applications being less of a blackbox We can begin to understand where optimizations should start to take place Meter resource usage with pools
  • #21 Services will begin to fail at some point Circuit breakers can also be implemented to limit cascading failures