Optimizing Performance:
Tackling Thread Pool Exhaustion to Reduce Backend Latency
Optimizing Performance:
Tackling Thread Pool Exhaustion to Reduce Backend Latency
Ting Wei Jing
Backend Engineer I, MoneyLion
September 04, 2024
Content
01 Concurrency in Java
02 Thread Pool Exhaustion
03 Thread Pool Executor
04 Configuring Thread Pool Executor
05 Monitoring and Adjustment
06 Virtual Threads
01
Concurrency in Java
Java Thread Relationship
CPU Core
1:N
OS Thread
1:1
Java
Platform Thread
N:M
Java
Virtual Thread
Thread Pool
Scenario
Developing an endpoint to retrieve trending products...
Involved Endpoints
Java 8
Spring Boot 2.7.0
Endpoint Calling Pattern
Fetch trending electronic products
Fetch trending beauty products
Fetch trending toy products
Fetch trending sport products
Fetch trending fashion products
Endpoint Implementation
02
Thread Pool Exhaustion
More Endpoints
Thread Pool Exhaustion Event
ForkJoinPool Common Pool
User Perspective
408 Request Timeout
503 Service Unavailable
504 Gateway Timeout
Developer Perspective
java.net.SocketException: Broken pipe (Write failed)
java.io.IOException: Broken pipe
Potentail Causes And Fixes
Cause | Fix |
---|---|
Unregulated thread spawning | Configure the thread pool. |
Insufficient CPU or memory resources | Increase hardware resources, or scale horizontally by adding more replicas or instances. |
Flawed implementation or the presence of bugs in the code | Revisit and review the implementation & perform debugging. |
03
Thread Pool Executor
Thread Pool Executor Mechanism
Task Num: 6 | Queue Capacity: 8
Defining Thread Pool Executor
Using Thread Pool Executor
Benefits of Thread Pool Executor
04
Configuring Thread Pool Executor
Little's Law
\(L = \lambda \times W\)
\(L:\) Required pool size (approximately)
\(\lambda:\) Estimated average incoming task rate
\(W:\) Estimated average task processing time
Core Pool Size
\(L = \lambda \times W\)
\(L:\) Required core pool size
\(\lambda:\) Thread spawn rate (per second)
\(W:\) Request completion time (second)
\(\lambda = 100 \times 5 = 500 s^{-1} \)
\(W = 0.2 s \)
\(L = 500 s^{-1} \times 0.2 s = 100\)
Max Pool Size
\(L = \lambda \times W\)
\(\lambda= 500 \times 5 = 2500 s^{-1} \)
\(W= 0.2 s \)
\(L= 2500 s^{-1} \times 0.2 s = 500\)
Queue Capacity Guideline
Configuration Result
05
Monitoring & Adjustment
Java Management Extensions (JMX)
A Java technology that provides tools for managing and monitoring applications and services using a standardized interface.
Datadog
Grafana
Prometheus
Elastic Stack
Important Values
Request Volume
Scenario 1
Given the active count trends observed during peak traffic...
Sign to increase Max Pool Size value
Scenario 2
Given the active count trends observed during peak traffic...
No adjustment needed
Scenario 3
Given the active count trends observed during non-peak traffic...
Recommended to increase Core Pool Size value
Scenario 4
Given the active count trends observed during non-peak traffic...
No adjustment needed
Guidelines
06
Virtual Threads
Java 21
Spring Boot 3.2.X
Java Thread Relationship
CPU Core
1:N
OS Thread
1:1
Java
Platform Thread
N:M
Java
Virtual Thread
Platform Threads vs Virtual Threads
Aspect | Platform Threads | Virtual Threads |
---|---|---|
Use Case | Suitable for both CPU & I/O operations | Designed for I/O operation only |
Resource Usage | High memory usage | Extremely lightweight in terms of memory usage |
Creation and Teardown | Slower | Faster |
Using Virtual Threads
Using Virtual Threads
Notes on Virtual Threads
Key Takeaways
Thank you
tingcode.com