Skip to content

[Improvement] Allow Netty Worker thread pool size to dynamically adapt to the number of processor cores #1586

@rickyma

Description

@rickyma

Code of Conduct

Search before asking

  • I have searched in the issues and found no similar issues.

What would you like to be improved?

Allow Netty Worker thread pool size to dynamically adapt to the number of processor cores.
We will have a more reasonable default value and better performance in most cases.

How should we improve?

Just use the default value in Netty (but not less than the original value 100), Netty sets this dynamically in io.netty.channel.MultithreadEventLoopGroup:

static {
    DEFAULT_EVENT_LOOP_THREADS = Math.max(1, SystemPropertyUtil.getInt(
            "io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));

    if (logger.isDebugEnabled()) {
        logger.debug("-Dio.netty.eventLoopThreads: {}", DEFAULT_EVENT_LOOP_THREADS);
    }
}

It will be better than just using the static value 100.

More detailed reasons:
Netty sets the default EventLoop pool size to processor * 2 primarily to fully utilize CPU resources in a multi-core environment while maintaining good performance and throughput.

In Netty, processor refers to the available number of CPU cores. Setting the EventLoop pool size to processor * 2 means that each CPU core will have two event loop threads available. The benefits of this approach are:

  1. Better utilization of multi-core performance: In a multi-core environment, each CPU core can process tasks in parallel. By setting the event loop thread count to twice the number of cores, each core is ensured to have enough threads to handle tasks, thus improving performance and throughput at the same time.

  2. Avoid thread starvation: In high-concurrency scenarios, some threads may be blocked, preventing other threads from continuing to execute. By increasing the number of event loop threads, the likelihood of thread starvation can be reduced, thereby improving the system's responsiveness.

  3. Better handling of I/O operations: Netty is commonly used for handling network I/O operations, which may be blocked while waiting for I/O completion. By increasing the number of event loop threads, enough threads can be ensured to handle I/O operations, thus improving the system's throughput.

However, setting the EventLoop pool size to processor * 2 is not always the best choice. In some scenarios, it may be necessary to adjust the number of event loop threads based on actual requirements and system resources. For example, if system resources are limited, it may be necessary to reduce the size of the event loop pool to avoid resource exhaustion. Therefore, in practical applications, it is best to determine the optimal EventLoop pool size through load testing and performance analysis.

But again, it will still be better than just using the static value 100.

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions