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...

Trending Products
Electronics
Games
Beauty
Fashion
1
Mouse Product
Gaming Mouse
2
Headset Product
Headset
3
Card Reader Product
Card Reader
4
Mouse Product
Mouse

Involved Endpoints

  1. Retrieve top 5 user preference product categories
    • Example: Electronics, beauty, toys, sports, fashion etc.

Endpoint Calling Pattern

Fetch 5 product categories

Endpoint Implementation

02

Thread Pool Exhaustion

More Endpoints

Thread Pool Exhaustion Event

ForkJoinPool Common Pool

User Perspective

  1. Experience slow response time due to high latency.
    Loading...

Developer Perspective

  1. Observed unpredictable delays between the execution of subsequent asynchronous tasks, as thread pool exhaustion can cause irregular time gaps in task scheduling.

Potentail Causes And Fixes

CauseFix

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

  • Core Pool Size: 2
  • Max Pool Size: 4
  • Queue Capacity: 8

Task Num: 6 | Queue Capacity: 8

Defining Thread Pool Executor

Using Thread Pool Executor

Benefits of Thread Pool Executor

  1. Control number of threads to be created in the pool.

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)

Max Pool Size

\(L = \lambda \times W\)

Queue Capacity Guideline

  • Core Pool Size \( \le \) Queue Capacity \( < \) Max Pool Size

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.

Java icon

Datadog

Spring Boot icon

Grafana

Spring Boot icon

Prometheus

Spring Boot icon

Elastic Stack

Important Values

  • Active count: Approximate number of threads that are actively executing tasks.
Jconsole Screenshot

Request Volume

Non-peak traffic
Peak traffic

Scenario 1

Given the active count trends observed during peak traffic...

Scenario 2

Given the active count trends observed during peak traffic...

Scenario 3

Given the active count trends observed during non-peak traffic...

Scenario 4

Given the active count trends observed during non-peak traffic...

Guidelines

  • Monitor CPU and Memory Usage
    • Increase hardware resources if usage consistently exceeds 90%.

06

Virtual Threads

Java Thread Relationship

CPU Core

1:N

OS Thread

1:1

Java
Platform Thread

N:M

Java
Virtual Thread

Platform Threads vs Virtual Threads

AspectPlatform ThreadsVirtual 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

  1. Not optimized for CPU-intensive operations.

Key Takeaways

  • Understand how concurrency / asynchronous is achieved.
    • Java - Platform Thread & Virtual Thread
  • Familiarize method to configure thread pool.
    • Java - Thread Pool Executor
  • Find the right way for monitoring.
    • Java - JMX

Thank you

tingcode.com