medium.com 4 min read Original Source

How many requests can Spring boot handle simultaneously?

As an essential framework for Java development, Spring Boot provides developers with efficient and easy-to-use development tools, so interview questions related to it are naturally very important. Let’s take a look at this classic interview question today: How many requests can Spring Boot handle at the same time?

To be precise, how many requests Spring Boot can handle at the same time does not depend on the Spring Boot framework itself, but on its built-in Web container (because the behavior of the Web container determines the behavior of Spring Boot, so let’s assume that the answers to the two questions are the same).

#1. Three major Web containers

Web containers are currently divided into three parts. The three most common web containers on the market are Tomcat, Undertow and Jetty. Tomcat is the default web container for the Spring Boot framework.

The differences between the three are as follows:

  • Tomcat is an open source project under the Apache Software Foundation. It is one of the most widely used Servlet containers and fully implements the Java Servlet and JavaServer Pages (JSP) specifications. It is not only a Servlet container, but also a lightweight application server, although Tomcat is considered to be slightly heavier than other lightweight servers. Tomcat supports many enterprise-level features, such as SSL, connection pooling, etc., and is suitable for running large and complex enterprise-level applications. Its stability and maturity have been verified by many years of enterprise-level applications, so it is the preferred Web container in many enterprises.
  • Undertow is a flexible, high-performance web server and reverse proxy server developed by Red Hat. It is the default web container for the WildFly application server. Undertow is designed with low memory usage and high concurrency, and is particularly good at handling a large number of short-connection scenarios, such as RESTful API services. Undertow supports Servlet 3.1, WebSocket, and non-blocking IO (NIO), and is one of the modern servers that supports the HTTP/2 protocol. Its design concept is to provide a modular, embeddable solution that is easy to integrate into existing systems and is also suitable for microservice architectures.
  • Jetty is an open source, lightweight web server and Servlet container maintained by the Eclipse Foundation. It is known for its embeddability and high configurability, and is often used in scenarios that require fast startup and lightweight deployment, such as development stages, test environments, or lightweight applications. Jetty also supports the Servlet specification and WebSocket, and is also based on NIO, which makes it perform well when handling a large number of concurrent connections. Jetty is designed with flexibility and scalability in mind, and is easy to customize through APIs to meet specific needs. Therefore, it is very popular in cloud environments, continuous integration, DevOps and other fields.

In general, Tomcat is suitable for large applications due to its maturity, stability and enterprise-level features; Undertow is known for its high performance and low memory usage, and is particularly suitable for handling high-concurrency short-connection scenarios; and Jetty is lightweight, flexible, and easy to embed, making it suitable for rapid development and lightweight deployment.

#2. Maximum number of connections and maximum number of waits

Taking the default web container Tomcat of the Spring Boot framework as an example, how many requests it can handle simultaneously is actually configured in the spring-configuration-metadata.json file in the Spring Boot framework, as shown in the following figure:

Open this file and search for “server.tomcat.max-connections” (Tomcat maximum number of connections) to get the following results:

That is to say, by default, the maximum number of connections allowed by Tomcat is 8192 (=8*1024).

Well, at this point someone might think that by default Spring Boot can handle 8192 requests at the same time. If you think so, you are wrong . Why?

Because, although Tomcat can allow a maximum number of connections of 8192, Tomcat also has a maximum waiting number, that is, if it reaches 8192, there is still a waiting queue to store the requested connections. Therefore, the number of connections that Spring Boot can handle at the same time is equal to the maximum number of connections of Tomcat plus the maximum number of waiting of Tomcat .

So, what is the maximum number of waits?

We continue to search for “server.tomcat.accept-count” (Tomcat maximum wait count) in the spring-configuration-metadata.json file. The search results are shown in the following figure:

That is, by default, the maximum number of waits for Tomcat is 100.

#3. Number of requests processed simultaneously

So we can conclude that by default, the number of requests that Spring Boot can handle simultaneously = the maximum number of connections (8192) + the maximum number of waits (100), which is 8292.

Of course, these two values ​​can be modified in the Spring Boot configuration file, as shown in the following configuration:

server:
  tomcat:
    max-connections: 2000
    accept-count: 200
server:
  tomcat:
    max-connections: 2000
    accept-count: 200

#4. Extended knowledge: Setting up the Web container

How does the Spring Boot framework set the web container to Jetty or Undertow? Next, let’s take a look.

#4.1 Set the container to Jetty

To set the Spring Boot framework’s web container to Jetty, just modify the pom.xml file as shown below:

<button data-code="

org.springframework.boot
spring-boot-starter-web

org.springframework.boot
spring-boot-starter-tomcat

org.springframework.boot
spring-boot-starter-jetty

” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-3 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>

<dependencies>
    <!-- Spring Boot Starter Web Tomcat -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- Tomcat -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Jetty -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
</dependencies>
<dependencies>
    <!-- Spring Boot Starter Web Tomcat -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- Tomcat -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Jetty -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
</dependencies>

In other words, you only need to exclude the default tomcat and add the jetty dependency.

#4.2 Set the container to Undertow

The idea of ​​setting the Spring Boot framework’s web container to Undertow is the same as the implementation of Jetty above. You only need to modify the pom.xml file as shown in the following configuration:

<button data-code="

org.springframework.boot
spring-boot-starter-web

org.springframework.boot
spring-boot-starter-tomcat

org.springframework.boot
spring-boot-starter-undertow

” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-3 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>

<dependencies>
    <!-- Spring Boot Starter Web Tomcat -->
    <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>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Undertow -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>
</dependencies>
<dependencies>
    <!-- Spring Boot Starter Web Tomcat -->
    <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>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Undertow -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>
</dependencies>

Plesae provide your comments to enrich this post.