preface
Within the SpringBoot framework, we use Tomcat the most, which is the default container technology for SpringBoot and is embedded with Tomcat.
At the same time, SpringBoot also supports Undertow container, we can easily replace Tomcat with Undertow, and Undertow is better than Tomcat in terms of performance and memory usage, so how can we use Undertow technology? This article will explain it in detail.
Tomcat container in SpringBoot
SpringBoot is arguably the most popular Java Web framework out there. It saves developers from heavy XML and allows them to create a complete Web service in a matter of minutes, greatly increasing developer productivity. Web container technology is an essential part of Web projects because any Web project needs container technology to run.
Within the SpringBoot framework, we use Tomcat the most, which is the default container technology for SpringBoot and is embedded with Tomcat.
SpringBoot set Undertow
Java programmers should be familiar with Tomcat technology, which is the most commonly used container technology for Web applications. Most of our earliest projects were deployed under Tomcat, so what container technologies can we use in SpringBoot besides Tomcat containers?
The Undertow container technology. SrpingBoot has fully inherited the Undertow technology, so we just need to introduce the Undertow dependency, as shown in the figure below.
Once configured, we start the application and find that the container has been replaced with Undertow.
So why do we need to replace Tomcat with Undertow technology?
The pros and cons of Tomcat and Undertow
Tomcat is a lightweight Servlet container under the Apache Foundation that supports servlets and JSPS. Tomcat has the unique functions of Web servers, including Tomcat management and control platform, security management, and Tomcat valve. Tomcat itself includes an HTTP server, so it can also be considered a separate Web server.
However, Tomcat is not the same thing as the ApacheHTTP server, which is an HTTP Web server implemented in the C language. Tomcat is completely free and popular with developers. (2020’s most frequently asked Business interview questions and answers)
Undertow is an open source product from Red Hat. Developed entirely in Java, Undertow is a flexible, high-performance Web server that supports blocking and non-blocking IO. Because Undertow is developed in the Java language, it can be embedded directly into Java projects. At the same time, Undertow fully supports servlets and Web sockets and performs well in high concurrency situations.
We pressed Tomcat and Undertow under the same machine configuration and got the test results as follows:
QPS test results comparison:
Tomcat
Undertow
Memory usage comparison:
Tomcat
Undertow
The tests found that Tomcat was relatively weak in high-concurrency systems. Simulating the same number of requests for the same machine configuration, Undertow is optimal for both performance and memory usage. And the new version of Undertow uses persistent connections by default, which will further improve its concurrent throughput. Therefore, Undertow is the best choice for high-concurrency business systems.
The last
In SpingBoot, we can either use Tomcat as Http service or Undertow instead. Undertow performs better than Tomcat in high-concurrency service scenarios. Therefore, if we have a system with high concurrent requests, we might as well use Undertow and you will find that your system performance will be greatly improved.