5 Tips for Optimizing Java Performance on Kubernetes

Transcript

Do your instances devour cloud resources or CPU time? Do containers restart frequently for unknown reasons? Are response times suboptimal? Is it challenging to meet service level agreement?

If the answer is yes to any of these questions, these five tips may help you boost the performance of your Java apps in the cloud!

Tip 1. Set CPU and RAM limits properly

Set CPU and RAM limits based on the results of load testing, stress testing, Native Memory tracking data, and other relevant metrics such as memory usage inside the pod. In addition, you should take Kubernetes overhead into account. For instance, AWS Fargate adds 256 MB to each Pod's memory reservation for the required Kubernetes components. 

Tip 2. Configure Kubernetes probes correctly

Probes are essential for monitoring the health of your pod. But improper prob configuration may lead to unnecessary container restarts, unrequired vertical scaling, or other issues. 

Tip 3. Choose a small base image

Choose a small base image to reduce container image size. For instance, Liberica Runtime Container can help you reduce RAM consumption by up to 30%.

Tip 4. Select the right Garbage collector

Select a Garbage Collector tailored to your needs. HotSpot JVM offers a selection of garbage collectors for various purposes, such as ParallelGC for high throughput or ZGC for low latency. 

Tip 5. Reduce application startup and warmup time

Reduce startup and warmup time of your applications. Java services may take dozens of minutes to reach stable peak performance. During that period, they process way fewer requests. You can alleviate the situation by using AppCDS, GraalVM Native Image, or Coordinated Restore at Checkpoint.

Summary

If your Java apps in the cloud struggle with high resource consumption, frequent container restarts, or slow response times, these five tips can help enhance their performance. First, set CPU and RAM limits properly based on load testing and account for Kubernetes overhead. Additionally, configure Kubernetes probes accurately, use a small base image to optimize container size, choose a suitable garbage collector, and reduce application startup and warmup time to ensure faster performance.

About Catherine

Java developer passionate about Spring Boot. Writer. Developer Advocate at BellSoft

Social Media

Videos
card image
Dec 17, 2024
Master Java Profiling: Tools, Techniques, and Real-World Tips

Java profiling allows to rapidly identify and fix performance bottlenecks in your program. In this video we explain what is profiling, introduce popular profiling tools, list their pros and cons, and provide useful tips and code examples.

Videos
card image
Dec 9, 2024
How to use CRaC with Spring Boot in a Docker Container

CRaC (Coordinated Restore at Checkpoint) is an OpenJDK project designed to significantly reduce startup and warmup times of Java applications to milliseconds. This tutorial demonstrates using CRaC with a Spring Boot application running in a Docker container, specifically the Spring Boot Petclinic app (version 3.2 or later).

Further watching

Videos
card image
Jan 22, 2025
JEP 483: Ahead-of-Time Class Loading & Linking. Project Leyden in JDK 24

JEP 483 introduces Ahead-of-Time (AOT) Class Loading and Linking in JDK 24, which enhances Java application startup times by loading and linking classes ahead of time and storing them in a reusable AOT cache. This feature, part of Project Leyden, reduces the JVM's workload during startup without requiring changes to application code, though a training run mimicking production is needed to create an efficient cache. Early tests with a Spring Boot app showed significant improvements, cutting startup time from two seconds to just one second.

Videos
card image
Jan 14, 2025
How to use AppCDS with Spring Boot

This tutorial demonstrates how to use Application Class Data Sharing (AppCDS) and Ahead-of-Time (AOT) processing with Spring Boot applications to reduce startup time by 40–50%. AppCDS creates an archive of parsed classes for faster loading, requiring no code changes, and works both locally and in containers. The tutorial covers building optimized Docker images using Dockerfiles or Buildpacks for efficient deployment and improved performance.

Videos
card image
Dec 28, 2024
JDK 24: The New Features in Java 24

JDK 24 is in Rampdown Phase One, which means, we know all the JEPs targeted to this release. And there are a lot of them, so it is time to discuss this new Java release!