top of page

Ideal Thread Pool Size

  • Writer: Anand Nerurkar
    Anand Nerurkar
  • May 1
  • 3 min read

ideal pool size

ChatGPT said:

🧮 What Is the Ideal Thread Pool Size?

The ideal thread pool sizeĀ depends on:

  • The type of workloadĀ (CPU-bound vs I/O-bound)

  • The number of available CPU cores

  • The task duration and wait time

  • System constraintsĀ (memory, queue capacity, SLA)

āœ… For CPU-bound tasks:

These tasks keep the CPU busy (e.g., number crunching, encryption, image processing)

šŸ”¢ Formula (Java Executors or Thread Pools):

javascript

Ideal Pool SizeĀ ā‰ˆ Number of CPUĀ cores + 1

Example: If you have 8 cores:

nginx

PoolĀ Size ā‰ˆ 8Ā + 1Ā = 9Ā threads


Why? You want just enough threads to keep all cores busy without too much context switching.

āœ… For I/O-bound tasks:

These tasks spend most time waitingĀ on external resources (e.g., DB calls, network, disk I/O)

šŸ”¢ Formula:

css

Ideal Pool Size ā‰ˆ Number of Cores Ɨ (1Ā + (Wait TimeĀ / Compute Time))

Example: 8 cores, 80% wait time vs 20% compute:

nginx

PoolĀ Size ā‰ˆ 8Ā Ć— (1Ā + 0.8Ā / 0.2) = 8Ā Ć— (1Ā + 4) = 40Ā threads

Why? While some threads wait, others can execute — so you need more threads to mask latency.

āš™ļø In Practice:

Workload Type

Recommended Pool Size Strategy

CPU-bound (e.g., ML, encryption)

cores + 1

I/O-bound (e.g., DB calls, REST APIs)

cores Ɨ (1 + wait/compute)

Mixed workloads

Use separate pools per workload type

šŸ” Best Practices:

  • Always set a bounded queueĀ and max threadsĀ to avoid OOM

  • Monitor queue size, throughput, and thread contention

  • Use work-stealing poolsĀ for fine-grained concurrency (Java ForkJoinPool)

  • Consider async/reactive models (e.g., Spring WebFlux) for high-scale I/O

Would you like a sample Java thread pool executor config (e.g., Spring Boot) for CPU or I/O-bound workloads?

You said:

Ā Spring Boot) for CPU or I/O-bound workloads?

ChatGPT said:

Here’s how to configure thread pools in Spring BootĀ for both CPU-boundĀ and I/O-boundĀ workloads using ThreadPoolTaskExecutor. You can adjust these settings based on the nature of your tasks.

🧠 Spring Boot ThreadPoolTaskExecutor Setup (Java Config)

āœ… 1. For CPU-Bound Workloads

Use fewer threads: corePoolSize ā‰ˆ # of CPU cores + 1
java

@Configuration

public class CpuBoundExecutorConfig {


@Bean(name = "cpuBoundExecutor")

public Executor cpuBoundExecutor() {

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

executor.setCorePoolSize(9); // 8 cores + 1

executor.setMaxPoolSize(10);

executor.setQueueCapacity(100);

executor.setThreadNamePrefix("CPU-Exec-");

executor.initialize();

return executor;

}

}

āœ… 2. For I/O-Bound Workloads

Use more threads: corePoolSize ā‰ˆ cores * (1 + wait/compute ratio)
java

@Configuration

public class IoBoundExecutorConfig {


@Bean(name = "ioBoundExecutor")

public Executor ioBoundExecutor() {

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

executor.setCorePoolSize(32); // assuming 8-core machine and wait/compute = 3

executor.setMaxPoolSize(64);

executor.setQueueCapacity(500);

executor.setThreadNamePrefix("IO-Exec-");

executor.initialize();

return executor;

}

}

āš™ļø How to Use It in Your Code

java

@Service

public class ReportService {


@Async("cpuBoundExecutor")

public void generateCpuIntensiveReport() {

// Heavy CPU work here

}


@Async("ioBoundExecutor")

public void callExternalApi() {

// Network I/O or DB

}

}

šŸ” Don’t forget:

  • Add @EnableAsyncĀ in a @ConfigurationĀ class

  • Use @AsyncĀ on methods returning voidĀ or CompletableFuture<T>

šŸ“ˆ Monitoring Tips:

  • Monitor with JMX, Actuator metrics, or Thread dump analysis

  • Tune queueCapacityĀ to avoid bottlenecks

  • Watch out for long-lived or blocking tasks in async pools


Certainly! Here's how to configure a thread pool executor in application.ymlĀ for Spring BootĀ using I/O-boundĀ and CPU-boundĀ strategies.

🧾 application.yml Configuration Example

yaml

spring:

task:

execution:

pool:

# General (default) executor config

core-size: 16 # Adjust based on CPU vs I/O bound

max-size: 32

queue-capacity: 500

keep-alive: 60s

thread-name-prefix: "DefaultExecutor-"

This config sets the default thread poolĀ used by @AsyncĀ when no qualifier is specified.

āœ… For More Control: Use Custom Executors via Java Config + application.yml

If you want separate named executors (e.g., for CPU vs I/O), configure custom beansĀ in Java and externalize propertiesĀ like this:

1. application.ymlĀ Example for Custom Pools

yaml

app:

executor:

cpu:

core-size: 8

max-size: 10

queue-capacity: 100

io:

core-size: 32

max-size: 64

queue-capacity: 500

2. Java Config Using These Properties

java

@Configuration

@ConfigurationProperties(prefix = "app.executor")

public class ThreadPoolProperties {

private final Pool cpu = new Pool();

private final Pool io = new Pool();


@Data

public static class Pool {

private int coreSize;

private int maxSize;

private int queueCapacity;

}


// getters

public Pool getCpu() { return cpu; }

public Pool getIo() { return io; }

}

java

@Configuration

public class CustomExecutorConfig {


@Bean(name = "cpuBoundExecutor")

public Executor cpuExecutor(ThreadPoolProperties props) {

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

executor.setCorePoolSize(props.getCpu().getCoreSize());

executor.setMaxPoolSize(props.getCpu().getMaxSize());

executor.setQueueCapacity(props.getCpu().getQueueCapacity());

executor.setThreadNamePrefix("CPU-Exec-");

executor.initialize();

return executor;

}


@Bean(name = "ioBoundExecutor")

public Executor ioExecutor(ThreadPoolProperties props) {

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

executor.setCorePoolSize(props.getIo().getCoreSize());

executor.setMaxPoolSize(props.getIo().getMaxSize());

executor.setQueueCapacity(props.getIo().getQueueCapacity());

executor.setThreadNamePrefix("IO-Exec-");

executor.initialize();

return executor;

}

}

Ā 
Ā 
Ā 

Recent Posts

See All
Ops Efficiency 30 % improvement

how did you achieve 30 % operational efficiency Achieving 30% operational efficiencyĀ in a BFSI-grade, microservices-based personal...

Ā 
Ā 
Ā 

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
  • Facebook
  • Twitter
  • LinkedIn

©2024 by AeeroTech. Proudly created with Wix.com

bottom of page