From 68e842baee2f667ef72adace087f335d69c58499 Mon Sep 17 00:00:00 2001 From: brido4125 Date: Wed, 19 Mar 2025 16:25:09 +0900 Subject: [PATCH 1/3] docs: add default threads count about NioEventLoopGroup --- src/main/java/io/lettuce/core/AbstractRedisClient.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/lettuce/core/AbstractRedisClient.java b/src/main/java/io/lettuce/core/AbstractRedisClient.java index 9adff50624..46156af191 100644 --- a/src/main/java/io/lettuce/core/AbstractRedisClient.java +++ b/src/main/java/io/lettuce/core/AbstractRedisClient.java @@ -68,10 +68,12 @@ /** * Base Redis client. This class holds the netty infrastructure, {@link ClientOptions} and the basic connection procedure. This - * class creates the netty {@link EventLoopGroup}s for NIO ({@link NioEventLoopGroup}) and EPoll ( - * {@link io.netty.channel.epoll.EpollEventLoopGroup}) with a default of {@code Runtime.getRuntime().availableProcessors() * 4} - * threads. Reuse the instance as much as possible since the {@link EventLoopGroup} instances are expensive and can consume a - * huge part of your resources, if you create multiple instances. + * class creates the netty {@link EventLoopGroup}s for NIO ({@link NioEventLoopGroup}) with a default of + * {@code SystemPropertyUtil.getInt("io.netty.eventLoopThreads", Math.max(MIN_IO_THREADS, Runtime.getRuntime().availableProcessors()))} + * threads and EPoll ({@link io.netty.channel.epoll.EpollEventLoopGroup}) with a default of + * {@code Runtime.getRuntime().availableProcessors() * 4} threads. Reuse the instance as much as possible since the + * {@link EventLoopGroup} instances are expensive and can consume a huge part of your resources, if you create multiple + * instances. *

* You can set the number of threads per {@link NioEventLoopGroup} by setting the {@code io.netty.eventLoopThreads} system * property to a reasonable number of threads. From 8d85b048ac83018246c28f62795cbc1673aaa4ce Mon Sep 17 00:00:00 2001 From: Tihomir Mateev Date: Fri, 16 May 2025 17:14:24 +0300 Subject: [PATCH 2/3] Polishing --- docs/advanced-usage.md | 19 +++++++++++++------ .../io/lettuce/core/AbstractRedisClient.java | 14 +++++--------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md index 6b95f7d9a5..5f6f1a5c71 100644 --- a/docs/advanced-usage.md +++ b/docs/advanced-usage.md @@ -97,12 +97,19 @@ instance needs to be shut down once it’s not used anymore. The basic configuration options are listed in the table below: -| Name | Method | Default | -|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------|------------------------| -| **I/O Thread Pool Size** | `ioThreadPoolSize` | `Number of processors` | -| The number of threads in the I/O thread pools. The number defaults to the number of available processors that the runtime returns (which, as a well-known fact, sometimes does not represent the actual number of processors). Every thread represents an internal event loop where all I/O tasks are run. The number does not reflect the actual number of I/O threads because the client requires different thread pools for Network (NIO) and Unix Domain Socket (EPoll) connections. The minimum I/O threads are `3`. A pool with fewer threads can cause undefined behavior. | | | -| **Computation Thread Pool Size** | `comput ationThreadPoolSize` | `Number of processors` | -| The number of threads in the computation thread pool. The number defaults to the number of available processors that the runtime returns (which, as a well-known fact, sometimes does not represent the actual number of processors). Every thread represents an internal event loop where all computation tasks are run. The minimum computation threads are `3`. A pool with fewer threads can cause undefined behavior. | | | +| Name | Method | Default | +|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------|-------------| +| **I/O Thread Pool Size** | `ioThreadPoolSize` | `See below` | +| The number of threads in the I/O thread pools. Every thread represents an internal event loop where all I/O tasks are run. The number does not reflect the actual number of I/O threads because the client requires different thread pools for Network (NIO) and Unix Domain Socket (EPoll) connections. The minimum I/O threads are `2`. | | | +| **Computation Thread Pool Size** | `comput ationThreadPoolSize` | `See below` | +| The number of threads in the computation thread pool. Every thread represents an internal event loop where all computation tasks are run. The minimum computation threads are `2`. | | | + +#### Default thread pool size + +Unless configured otherwise by the settings above, the number of threads (for both computation and I/O) is determined in the following order: +* if there is an environment variable setting for `io.netty.eventLoopThreads` we use it as default setting +* otherwise we take the number of available processors, retrieved by `Runtime.getRuntime().availableProcessors()` _(which, as a well-known fact, sometimes does not represent the actual number of processors)_ +* in any case if the chosen number is lower than the minimum, which is `2` threads, then we use the minimum. ### Advanced settings diff --git a/src/main/java/io/lettuce/core/AbstractRedisClient.java b/src/main/java/io/lettuce/core/AbstractRedisClient.java index 46156af191..ae88ecf8bb 100644 --- a/src/main/java/io/lettuce/core/AbstractRedisClient.java +++ b/src/main/java/io/lettuce/core/AbstractRedisClient.java @@ -60,7 +60,6 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.group.ChannelGroup; import io.netty.channel.group.DefaultChannelGroup; -import io.netty.channel.nio.NioEventLoopGroup; import io.netty.util.concurrent.EventExecutorGroup; import io.netty.util.concurrent.Future; import io.netty.util.internal.logging.InternalLogger; @@ -68,22 +67,19 @@ /** * Base Redis client. This class holds the netty infrastructure, {@link ClientOptions} and the basic connection procedure. This - * class creates the netty {@link EventLoopGroup}s for NIO ({@link NioEventLoopGroup}) with a default of - * {@code SystemPropertyUtil.getInt("io.netty.eventLoopThreads", Math.max(MIN_IO_THREADS, Runtime.getRuntime().availableProcessors()))} - * threads and EPoll ({@link io.netty.channel.epoll.EpollEventLoopGroup}) with a default of - * {@code Runtime.getRuntime().availableProcessors() * 4} threads. Reuse the instance as much as possible since the - * {@link EventLoopGroup} instances are expensive and can consume a huge part of your resources, if you create multiple - * instances. + * class creates different Netty {@link EventLoopGroup}s depending on the {@link NativeTransports} used. *

- * You can set the number of threads per {@link NioEventLoopGroup} by setting the {@code io.netty.eventLoopThreads} system - * property to a reasonable number of threads. + * You can set the number of threads by using the {@link ClientResources} configuration. + * For more details, check the documentation of the {@link DefaultClientResources} class. *

* * @author Mark Paluch * @author Jongyeol Choi * @author Poorva Gokhale + * @author Tihomir Mateev * @since 3.0 * @see ClientResources + * @see DefaultClientResources */ public abstract class AbstractRedisClient implements AutoCloseable { From 22f5f57f76cfaa4cef1a798f4038906180a8b705 Mon Sep 17 00:00:00 2001 From: Tihomir Mateev Date: Fri, 16 May 2025 17:18:42 +0300 Subject: [PATCH 3/3] Formatting issue --- src/main/java/io/lettuce/core/AbstractRedisClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/lettuce/core/AbstractRedisClient.java b/src/main/java/io/lettuce/core/AbstractRedisClient.java index ae88ecf8bb..fcb65f2914 100644 --- a/src/main/java/io/lettuce/core/AbstractRedisClient.java +++ b/src/main/java/io/lettuce/core/AbstractRedisClient.java @@ -69,8 +69,8 @@ * Base Redis client. This class holds the netty infrastructure, {@link ClientOptions} and the basic connection procedure. This * class creates different Netty {@link EventLoopGroup}s depending on the {@link NativeTransports} used. *

- * You can set the number of threads by using the {@link ClientResources} configuration. - * For more details, check the documentation of the {@link DefaultClientResources} class. + * You can set the number of threads by using the {@link ClientResources} configuration. For more details, check the + * documentation of the {@link DefaultClientResources} class. *

* * @author Mark Paluch