Skip to content

Use Client Pool

The Client Pool manages a set of reusable GLIDE client instances that can be borrowed and returned on demand. Use it when your application needs high concurrency, runs blocking commands like BLPOP, BRPOP, or XREAD BLOCK that tie up a connection, or requires connection isolation between independent workflows.

A single GLIDE client multiplexes commands over shared connections. That is efficient for most commands but a poor fit for blocking commands, large values, and WATCH, which hold a connection or per-connection state. Creating a client for each of these is costly: every new client opens its own connections (to every node in cluster mode) and repeats connection setup and authentication.

A pool avoids that cost:

  • Faster reuse of connections: Borrow an already-connected client instead of creating one, which skips the cost of establishing connections and authenticating. Set min_idle to connect clients when the pool is created.
  • Fewer connections: Callers share up to max_size clients, which caps the connections to the server however many callers your application has.
  • Isolated commands: A borrowed client serves one caller at a time, so state such as WATCH or MULTI isn’t disturbed by other callers’ commands, and returning it resets state the borrower left behind, such as an open MULTI. Up to max_size callers can also block on BRPOP at once, and a large value doesn’t delay other callers’ requests.

If your application only sends short, non-blocking commands, a single shared client is enough.

The pool is configured with a PoolConfig object. All parameters are optional and have sensible defaults.

ParameterTypeDefaultDescription
max_size / maxSize / MaxSizeint10Maximum number of client instances in the pool
min_idle / minIdle / MinIdleint1Number of clients to pre-warm at creation
idle_timeout_ms / idleTimeout / IdleTimeoutduration5 min (Node.js: 30 sec)How long an idle client stays in the pool before eviction
request_timeout_ms / requestTimeout / RequestTimeoutduration5 secBase timeout for state reset (DISCARD, SELECT) when returning a client; the reset is abandoned and the connection discarded after 2× this value. (Node.js has no pool field; it uses the client config’s requestTimeout.)
acquire_timeout_s / acquireTimeoutS / AcquireTimeoutduration5 secHow long to wait for a client when the pool is exhausted
abandon_timeout_ms / abandonTimeoutMs / AbandonTimeoutduration5 min (300000 ms)Maximum inactivity time for a borrowed client before the pool reclaims it. The timer resets on every command sent. The abandon monitor skips clients executing blocking commands. Set to 0 to disable (in Go, set to a negative value to disable; 0 means use default).
import glide.api.models.pool.ClientPool;
import glide.api.models.pool.ClientPoolConfig;
import glide.api.models.configuration.GlideClientConfiguration;
import glide.api.models.configuration.NodeAddress;
ClientPoolConfig poolConfig = ClientPoolConfig.builder()
.maxSize(10)
.minIdle(2)
.acquireTimeout(Duration.ofSeconds(5))
.clientConfig(GlideClientConfiguration.builder()
.address(NodeAddress.builder().host("localhost").port(6379).build())
.build())
.build();
ClientPool pool = ClientPool.create(poolConfig);

Acquire a client from the pool before use, and always release it when done. Use context managers or try/finally blocks to guarantee the client is returned even if an error occurs.

try (PooledGlideClient client = pool.acquire().get()) {
client.set("key", "value").get();
String result = client.get("key").get();
}
// client is automatically returned to the pool on close

Check the current state of the pool to monitor utilization and diagnose capacity issues.

System.out.println("Idle: " + pool.getIdleCount()
+ ", Active: " + pool.getActiveCount()
+ ", Total: " + pool.getTotalCount());

The pool runs a background monitor that reclaims borrowed clients when they have been inactive beyond a configurable timeout. This prevents connection leaks when application code forgets to return a client.

How it works:

  • The monitor scans at half the timeout interval (e.g., every 2.5 minutes with the default 5-minute timeout).
  • A borrowed client is reclaimed if no command has been sent to it within the threshold.
  • The inactivity timer resets on every command sent to the borrowed client.
  • Blocking commands (BLPOP, BRPOP, XREAD BLOCK, etc.) are automatically detected and excluded from reclaim.
  • When a client is reclaimed, the connection is discarded and a fresh one is created on the next acquire().
import glide.api.models.pool.ClientPoolConfig;
import java.time.Duration;
// Set abandon timeout to 1 minute
ClientPoolConfig poolConfig = ClientPoolConfig.builder()
.abandonTimeout(Duration.ofMinutes(1))
.build();
// Disable abandon detection
ClientPoolConfig poolConfig = ClientPoolConfig.builder()
.abandonTimeout(Duration.ZERO)
.build();

When your application shuts down, close the pool to release all connections.

pool.close();

The following apply to the preview release.

These are intentional constraints for this release. Design your application around them.

  • No Pub/Sub subscriptions: Clients with active subscriptions are rejected at pool creation, because subscription state cannot be reliably reset when the client is returned. A borrowed client that subscribes is discarded on release instead of being reused. Use a dedicated client for Pub/Sub.
  • Close any WATCH before releasing a client: On release the pool resets state with DISCARD and SELECT, which does not clear a WATCH that was set without a following MULTI. The next borrower inherits that WATCH, so its EXEC can abort over a key it never watched. Always end a WATCH with EXEC, DISCARD, or UNWATCH before returning the client.
  • No custom IAM credential providers: Python, Java, Node.js, and Go reject a pooled client configuration that contains a custom IAM credential provider. Omit the provider to use IAM authentication with the default AWS credential chain in a pool.
  • Custom address resolver support varies by binding: Node.js pools support custom address resolvers. Java and Go reject a pool configuration that contains one. Python uses the resolver for its connectivity probe but does not forward it to pooled connections. Resolve addresses before creating Python, Java, or Go pools.
  • No background eviction: Idle clients are evicted lazily when acquire() is called, not on a background timer.
  • No automatic pool sharing: Each call that creates a pool produces an independent pool. Two pools built from the same configuration are not deduplicated or shared. Hold and reuse the pool you created wherever you need it.
  • Scopes do not outlive the pool: Closing the pool invalidates any isolated scope still open on a borrowed client. Commands on such a scope fail rather than continuing to run on a connection the pool no longer owns.

These are being tracked and are expected to be resolved in a future release.

  • A borrowed client stays usable after its borrow ends: A handle is not tied to the borrow it came from, so after release it can still run commands on a connection the pool has lent to someone else. In Go, call Close() exactly once per borrow; a second call releases the next borrower’s client. In Java, don’t keep the client returned by unwrap(), and use acquire(timeout) instead of cancelling the returned future, since a cancelled acquire leaks a pool slot. In Node.js and Python, keep borrows shorter than the abandon timeout.
  • Some connection state carries over to the next borrower: Release does not reset AUTH identity, HELLO/RESET protocol changes, CLIENT NO-EVICT, CLIENT NO-TOUCH, CLIENT SETNAME, CLIENT TRACKING, or cluster READONLY. Don’t change connection-level state on a borrowed client; use a dedicated client instead.
  • Cluster pools return a standalone client in Java and Go: A client borrowed from a cluster pool doesn’t expose cluster-only APIs, such as routing options or Go’s ClusterClient.ScopedConnection(). Node.js and Python return a cluster client.
  • close() does not wait for borrowed clients: Closing a pool tears it down immediately, interrupting commands still running on borrowed clients. Release all clients before closing the pool.
  • Python async pools require asyncio: Borrowing from an AsyncClientPool fails under trio when the borrow has to wait, and borrowed clients are bound to the asyncio event loop. Use asyncio with Python client pools.
  • Pool timeouts and errors are inconsistent across bindings: An acquire timeout of 0 uses the configured default instead of not waiting in Python async, Go, and Node.js. Pool exhaustion is not raised as the binding’s GLIDE TimeoutError in Java, Node.js, and Python. Use a positive timeout, and handle exhaustion as a generic error.