Analysis and Optimization of Connection Limits in Spring Boot Microservices

Dec 02, 2025 · Programming · 9 views · 7.8

Keywords: Spring Boot | Connection Limits | Thread Pool Configuration | Tomcat | Performance Optimization | Microservices

Abstract: This article provides an in-depth analysis of connection limit issues encountered during performance testing of Spring Boot microservices. By examining the thread pool configuration mechanisms of embedded containers (such as Tomcat, Jetty, and Undertow), it explains default connection settings, configuration adjustment methods, and special limitations under HTTP/2 protocol. The article offers comprehensive troubleshooting steps and configuration optimization solutions to help developers understand and resolve concurrency processing limitations in microservices.

Problem Context and Phenomenon Analysis

During performance testing of microservices developed with Spring Boot, developers frequently observe upper limits on concurrent connections. Specifically, even when making numerous requests to the service, the actual number of threads created remains stable at a fixed value (e.g., 20), preventing full utilization of system resources for higher concurrency. This phenomenon typically originates from default configuration limits of Spring Boot's embedded containers, rather than functional limitations of the Spring Boot framework itself.

Embedded Container Thread Pool Mechanism

Spring Boot uses embedded Servlet containers (such as Tomcat, Jetty, or Undertow) by default to run web applications. These containers process incoming HTTP requests through thread pools, with their concurrency handling capacity directly controlled by thread pool configuration parameters. Taking the most commonly used Tomcat as an example, key configuration properties include:

According to Tomcat's official documentation, when server.tomcat.max-threads is not explicitly configured, the default value is 200. However, the observed limit of 20 threads may involve more complex factors.

Configuration Adjustment and Optimization Solutions

To resolve connection limit issues, it's first necessary to adjust configurations according to the actual container type being used. Make the following settings in application.properties or application.yml files:

# Tomcat configuration example
server.tomcat.max-threads=400
server.tomcat.min-spare-threads=50
server.tomcat.max-connections=10000
server.tomcat.accept-count=100

# Undertow configuration example
server.undertow.worker-threads=400

# Jetty configuration example
server.jetty.acceptors=4
server.jetty.selectors=8

After configuration adjustments, the application needs to be restarted for the settings to take effect. It's recommended to verify thread count changes through monitoring tools to ensure configuration adjustments achieve the desired effect.

Special Considerations for HTTP/2 Protocol

When microservices enable the HTTP/2 protocol, additional concurrency limitations may be encountered. Tomcat's HTTP/2 implementation includes the maxConcurrentStreamExecution parameter, which controls the number of streams that can be processed concurrently on a single connection. In some versions, this parameter may default to 20, explaining why concurrency remains limited around 20 even after adjusting thread pool configurations.

To adjust HTTP/2 concurrent stream limits, implement a custom TomcatConnectorCustomizer:

@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
    return factory -> factory.addConnectorCustomizers(connector -> {
        if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) {
            AbstractHttp11Protocol<?> protocol = 
                (AbstractHttp11Protocol<?>) connector.getProtocolHandler();
            protocol.setMaxConcurrentStreamExecution(200);
        }
    });
}

Performance Testing and Verification Methods

After configuration adjustments, systematic performance testing is needed to verify optimization effectiveness:

  1. Use load testing tools (such as JMeter or Gatling) to simulate high-concurrency scenarios
  2. Monitor application thread count changes to confirm maximum threads reach configured values
  3. Observe system resource usage to avoid resource exhaustion from over-configuration
  4. Test performance under different configuration combinations to find optimal parameters

A gradual adjustment strategy is recommended, incrementally increasing concurrency while observing system response, avoiding stability issues that may arise from drastic one-time changes.

Best Practices and Considerations

When adjusting connection count configurations, consider the following practical principles:

By understanding the connection handling mechanisms of Spring Boot's embedded containers and making targeted adjustments to relevant configurations, developers can effectively enhance the concurrency processing capabilities of microservices to meet the demands of high-performance application scenarios.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.