Home Java javaTutorial Improving the performance of Spring Boot applications - Part I

Improving the performance of Spring Boot applications - Part I

Aug 22, 2024 pm 12:33 PM

Melhorando o desempenho de aplicações Spring Boot - Parte I

When starting Spring Boot applications, we typically use the default settings provided by starters, which is sufficient for most cases. However, if we are in need of performance, there are specific adjustments that can be made, as will be demonstrated in the first part of this article.

Replacing Tomcat with another servlet container

Applications web, RESTFul, which use Spring MVC, generally add the spring-boot-starter-web dependency, which by default uses Tomcat as a web server. However, there are more interesting alternatives, such as Undertow, which is a high-performance web server, with an asynchronous and non-blocking architecture, which allows you to handle a large number of simultaneous connections. efficiently, making it suitable for high-performance applications. We're not saying Tomcat is bad, but we can give Undertow a chance.

Adding Undertow in Spring

In order for us to use Undertow as a web server, we need to ignore the spring-boot-starter-tomcat dependency that spring-boot-starter-web already adds and then add the spring-boot-starter-undertow.

Using pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusions>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>
</dependencies>
Copy after login

Using build.gradle:

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    }
    implementation 'org.springframework.boot:spring-boot-starter-undertow'
}

Copy after login

Configuring undertow

Through application.properties or application.yml, we can configure how many IO threads and how many worker threads we want the server to use.

Using application.yml

server:
  undertow:
    threads:
      io: 4
      worker: 64
Copy after login

Using application.properties

server.undertow.threads.io=4
server.undertow.threads.worker=64
Copy after login

I/O Threads perform non-blocking operations and should never perform blocking operations, as they are responsible for listening to connections arriving in the application, and then sending them to a processing queue. A common value is two I/O Threads per CPU core.

The worker threads execute blocking operations, such as Servlet requests that were sent to the processing queue by the I/O Threads. The ideal value depends on the workload, but it is generally recommended to configure around 10 threads per CPU core.

For more detailed information and more options that can be explored, just go to the Undertow documentation.

Compressing HTTP responses

Data compression is a feature that aims to reduce the body size of HTTP responses, which in turn can improve the performance of our application by reducing the amount of data transmitted over the network.

Configuring data compression in Spring Boot is a trivial task, as it supports this functionality.

Using application.yml

server:
  compression:
    enabled: true
    mime-types: text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json
    min-response-size: 1024
Copy after login

Using application.properties

server.compression.enabled=true
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json
server.compression.min-response-size=1024
Copy after login

server.compression.enabled: Enables/disables compression.
server.compression.mime-types: List of MIME types that should be compressed.
server.compression.min-response-size: Minimum size of "Content-Length" that is necessary for compression to be performed.

With this, we close the first part of the article. In the next part, we will learn more about Hikari, JPA and Hibernate and learn how to configure them, in order to further improve the performance of Spring Boot applications.

The above is the detailed content of Improving the performance of Spring Boot applications - Part I. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

See all articles