15 Ways to Optimize Spring Boot for the Cloud ☁️

Billy Korando - @BillyKorando Developer Advocate - IBM 17 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 ️ ️️ ️ ️ Optimization Factors

️ Observability Improvement ️ ️ Resiliency

EfficiencyEfficiency ️️ ️ ️️ Scalability Security ️ Sources/Additional Reading 1. You Don’t Have to Do Microservices to be on the Cloud

ORDERS ADDRESSES FULFILLMENT

ITEMS ORDER USER HISTORY

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 Understanding 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

org.springframework.cloud spring-cloud-starter-sleuth org.springframework.cloud spring-cloud-starter-zipkin 3. Spring Cloud Sleuth for Request Tracing and Correlation

Trace ID Span ID service1,2485ec27856c56f4,2485ec27856c56f4 service2,2485ec27856c56f4,9aa10ee6fbde75fa service3,2485ec27856c56f4,1210be13194bfe5 service2,2485ec27856c56f4,9aa10ee6fbde75fa service4,2485ec27856c56f4,1b1845262ffba49d service2,2485ec27856c56f4,9aa10ee6fbde75fa service1,2485ec27856c56f4,2485ec27856c56f4 3. Spring Cloud Sleuth for Request Tracing and Correlation

Time well spent Time poorly spent

Project Dapper 4. Use and Customize Spring Boot Actuator

org.springframework.boot spring-boot-starter-actuator /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

ITAddBooking ️

POST /bookings POST { http://dev/bookings customerId : 1 roomNumber : 100 } Person Client Person️ Service 8. Use Spring Cloud Contract to Validate Contracts Between Services

Bookings Contract 8. Use Spring Cloud Contract to Validate Contracts Between Services

ITAddBooking ️

POST /bookings POST { http://localhost/bookings customerId : 1 roomNumber : 100 } Person Client Person Service Contract 9. Use TestContainers for Other Services

ITAddPersonDB ️

INSERT INTO PERSONS PERSONS … TABLE ️ Person Client Dev Database 9. Use TestContainers for Other Services

ITAddPersonDB ️

INSERT INTO PERSONS PERSONS … TABLE

Person Client

Local Docker Database 10. Have a Continuous Delivery Pipeline to Production

️ ️ ️️ ☁️ Code Repo ️ ️ ️ ️ Build Server 11. Keep Dependencies Up-to-Date

The Code You Wrote Security patches The Code Performance enhancements Your Code Feature enhancements Runs On 12. Use OpenJ9

Hotspot GraalVM Corretto

OpenJ9 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/

13. Use Eclipse OpenJ9 class sharing

The read only part position independent

ROMClass So OpenJ9 splits the class into two parts

Classfile The stateful Not a great way part to store class information J9RAMClass 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 them! 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 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 [email protected]