Configuring Undertow in Spring Boot Applications

Undertow is a lightweight, high-performance web server that you can use as Web Server in Spring Boot applications. This tutorial will guide you through the steps to configure Undertow and explain specific properties to optimize its performance.

Undertow Configuration

Firstly, before you can use Undertow as Web Server, you have to disable the default Tomcat Web Server and add Undertow dependency to it. The following article will guide you through the process: Configure Spring Boot to use Undertow Web server

Next, you can configure undertow in the application.properties or application.yml file. For example:

server.undertow.buffer-size=16384
server.undertow.buffers-per-region=20
server.undertow.io-threads=64
server.undertow.worker-threads=512
server.undertow.direct-buffers=true

Here is an explanation to the above settings:

  1. server.undertow.buffer-size=16384: Defines the size of each buffer in bytes. Larger buffer sizes can improve performance for large payloads.
  2. server.undertow.buffers-per-region=20: Specifies the number of buffers per region. This setting helps manage memory allocation for buffers.
  3. server.undertow.io-threads=64: Sets the number of I/O threads to handle non-blocking tasks. More threads can improve concurrency and throughput.
  4. server.undertow.worker-threads=512: Determines the number of worker threads for handling requests. Increasing this value can enhance the server’s ability to process multiple requests simultaneously.
  5. server.undertow.direct-buffers=true: Enables the use of direct buffers, which can improve I/O performance by reducing the overhead of garbage collection.

Configuring Undertow Programmatically

Besides the application property files, you can also configure Undertow programmatically. For example here is how to use the UndertowServletWebServerFactory to configure Undertow’s pool size in Spring Boot:

@Bean public UndertowServletWebServerFactory embeddedServletContainerFactory() {
  UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
  int ioThreads = 5;
  int taskThreads = 100;
  factory.setIoThreads(ioThreads);
  factory.setWorkerThreads(taskThreads);
  return factory;
}

Configuring Undertow Properties at Spring Boot startup

Finally, you can also fine tune your Undertow configuration when you boot your Spring Boot application. For example:

java -Xms256m -Xmx512m \
    -jar application-0.0.1-SNAPSHOT.jar \
    --server.port=8080 \
    --server.undertow.buffer-size=16384 \
    --server.undertow.buffers-per-region=20 \
    --server.undertow.io-threads=64 \
    --server.undertow.worker-threads=512 \
    --server.undertow.direct-buffers=true

Summary

By following these steps, you can configure Undertow as the web server for your Spring Boot application and optimize its performance using the specified properties. This setup ensures that your application can handle high loads efficiently and provides a robust foundation for your web services.

If you want to learn more about Undertow configuration, you can also visit this article, which discusses how to configure Undertow Pool in WildFly Server.

Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon