[ Spring Docs ] Tomcat 관련 설정, UnderTow 설정
[ Blog ] Spring Thread Pool (tomcat)
[ Baeldung ] Configuring Thread Pools for Java Web Servers
[ Blog ] UnderTow와 Tomcat의 간단한 비교(성능 분석 포함)
Tomcat vs Undertow
[ Stackshare ] Apache Tomcat vs Undertow
- Apache Tomcat은 기능이 많고 넓게 채택되는 application server로 Java EE 환경을 완벽히 제공해줌
- Undertow는 고성능, 경량 웹서버로 마이크로 서비스 아키텍쳐나 embedding에 적합함
- Undertow의 I/O 부분은 JBoss 프로젝트 중 하나인 XNIO를 이용하여 처리.
- asynchronous, non blocking 형태로 구성되어 있어, 많은 수의 동시 connection을 효율적으로 처리 가능함 (반면 Tomcat은 비동기가 아닌 동기 형태 → concurrency가 떨어짐)
ㅤ | Tomcat | Undertow |
Pros(장점) | Easy
Java
Popular | Performance
Lower footprint(메모리 덜 사용) |
Cons | Blocking - each http request block a thread
| Smaller Community
Less known |
Jmeter 이용한 부하테스트 시, 성능 비교(Scouter로 확인)
Undertow
ThreadList :모든 Thread가 RUNNABLE, 실행 중이고, TIMED_WAITING, 대기하고 있는 스레드는 거의 없음. Thread 갯수 64개. Thread당 CPU 사용량이 더 큼

mpstat : CPU 상태 확인

Api response Time

Tomcat
ThreadList : thread 갯수가 200개. RUNNABLE이 많지만, TIMED_WAIT도 4-5개씩 발생

mpstat: 위의 UnderTow랑 큰차이 없음

결론
- Tomcat이냐, Undertow냐 의 문제가 아니었음. Thread 갯수의 문제. Tomcat 은 스레드 갯수가 200개였고, Undertow는 Thread 갯수가 64개였어서, Tomcat 의 스레드 풀 갯수를 64개로 줄였더니, api 응답시간이 Undertow와 성능 비슷함
- Thread 갯수가 200개 일때 Profile 상황

Profile 상황을 보면 REDIS 명령어와 HikariDataSource#getConnection 명령어 사이에서 7초나 기다리고 있음. 각각의 명령어 자체는 오래 걸리지가 않는데 이렇게 오래 걸리는 이유는 결국, Thread의 갯수가 너무 많아서 Context Switching의 Overhead 때문이였음
- Context Switching의 오버헤드인지, 무엇인지는 잘 모르겠지만 Thread Pool의 갯수를 줄이니 확실히 응답속도가 짧아지는 것을 확인함.
Thread 갯수별 GC, Heap Usage 비교


- 당연히 돌고 있는 Thread가 더 많다 보니 메모리를 더 사용함!
- Heap 도 많이 사용
- Memory도 조금 더 사용
XLog에 문제가 있었음..! api 응답시간 계속 줄어들었던건 아님 (Thread 8, 16, 24 개는 거의 비슷하고. 32개부터는 위로 올라갈수록 api 응답시간이 더 늦어짐)


Thread 32개 (VU 500)


Thread 64 (VU 500)

이용 방법
implementation('org.springframework.boot:spring-boot-starter-web')
- 위의 의존성을 선언하면 자동으로
org.springframework.boot:spring-boot-starter-tomcat
을 가져오면서 Tomcat을 Embedded Server로 채택하여 구동함
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'
- spring-boot-starter-web에서 spring-boot-starter-tomcat을 제외하고 spring-boot-starter-undertow를 추가하면 Undertow로 Application Server 시작됨