"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Improving the performance of Spring Boot applications - Part I

Improving the performance of Spring Boot applications - Part I

Published on 2024-09-13
Browse:608

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 server web. 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 it 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 to 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:

org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-tomcatorg.springframework.bootspring-boot-starter-undertow

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'
}

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

Using application.properties

server.undertow.threads.io=4
server.undertow.threads.worker=64

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

worker threads execute blocking operations, such as Servlet requests that were sent to the processing queue by 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

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

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.

Release Statement This article is reproduced at: https://dev.to/mathstylish/melhorando-a-performance-de-aplicacoes-spring-boot-parte-i-58jl?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3