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.
Why Use a Client Pool
Section titled “Why Use a Client Pool”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_idleto connect clients when the pool is created. - Fewer connections: Callers share up to
max_sizeclients, 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
WATCHorMULTIisn’t disturbed by other callers’ commands, and returning it resets state the borrower left behind, such as an openMULTI. Up tomax_sizecallers can also block onBRPOPat 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.
Configuration
Section titled “Configuration”The pool is configured with a PoolConfig object. All parameters are optional and have sensible defaults.
| Parameter | Type | Default | Description |
|---|---|---|---|
max_size / maxSize / MaxSize | int | 10 | Maximum number of client instances in the pool |
min_idle / minIdle / MinIdle | int | 1 | Number of clients to pre-warm at creation |
idle_timeout_ms / idleTimeout / IdleTimeout | duration | 5 min (Node.js: 30 sec) | How long an idle client stays in the pool before eviction |
request_timeout_ms / requestTimeout / RequestTimeout | duration | 5 sec | Base 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 / AcquireTimeout | duration | 5 sec | How long to wait for a client when the pool is exhausted |
abandon_timeout_ms / abandonTimeoutMs / AbandonTimeout | duration | 5 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). |
Create a Pool
Section titled “Create a Pool”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);from glide import GlideClientConfiguration, NodeAddressfrom glide import AsyncClientPool, PoolConfig
pool_config = PoolConfig( max_size=10, min_idle=2, acquire_timeout_s=5.0,)client_config = GlideClientConfiguration([NodeAddress("localhost", 6379)])pool = await AsyncClientPool.create(client_config, pool_config)import { ClientPool, GlideClientConfiguration } from '@valkey/valkey-glide';
const clientConfig: GlideClientConfiguration = { addresses: [{ host: 'localhost', port: 6379 }],};
const pool = await ClientPool.create(clientConfig, { maxSize: 10, minIdle: 2, acquireTimeoutS: 5,});import ( glide "github.com/valkey-io/valkey-glide/go/v2" "github.com/valkey-io/valkey-glide/go/v2/config" "time")
poolCfg := glide.PoolConfig{ MaxSize: 10, MinIdle: 2, AcquireTimeout: 5 * time.Second,}clientCfg := config.NewClientConfiguration(). WithAddress(&config.NodeAddress{Host: "localhost", Port: 6379})
pool, err := glide.NewClientPool(clientCfg, poolCfg)Borrow and Return Clients
Section titled “Borrow and Return Clients”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 closeasync with pool.borrow() as client: await client.set("key", "value") result = await client.get("key")Using borrow() (recommended):
const result = await pool.borrow(async (client) => { await client.set('key', 'value'); return await client.get('key');});Manual acquire/release:
const client = await pool.acquire();try { await client.set('key', 'value'); const result = await client.get('key');} finally { await pool.release(client);}clientID, err := pool.Acquire(ctx)if err != nil { log.Fatal(err)}
client, err := pool.GetClient(clientID)if err != nil { log.Fatal(err)}defer client.Close() // returns to pool
client.Set(ctx, "key", "value")result, _ := client.Get(ctx, "key")Pool Metrics
Section titled “Pool Metrics”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());metrics = pool.metrics()print(f"Idle: {metrics['idle']}, Active: {metrics['active']}, Total: {metrics['total']}")const metrics = pool.getMetrics();console.log(`Idle: ${metrics.idle}, Active: ${metrics.active}, Total: ${metrics.total}`);fmt.Printf("Idle: %d, Active: %d, Total: %d\n", pool.IdleCount(), pool.ActiveCount(), pool.TotalCount())Abandon Detection
Section titled “Abandon Detection”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().
Configure a Custom Timeout
Section titled “Configure a Custom Timeout”import glide.api.models.pool.ClientPoolConfig;import java.time.Duration;
// Set abandon timeout to 1 minuteClientPoolConfig poolConfig = ClientPoolConfig.builder() .abandonTimeout(Duration.ofMinutes(1)) .build();from glide import PoolConfig
# Set abandon timeout to 1 minutepool_config = PoolConfig(abandon_timeout_ms=60000)// Set abandon timeout to 1 minuteconst pool = await ClientPool.create( { addresses: [{ host: 'localhost', port: 6379 }] }, { abandonTimeoutMs: 60000 },);// Set abandon timeout to 1 minutepoolCfg := glide.PoolConfig{ AbandonTimeout: 1 * time.Minute,}Disable Abandon Detection
Section titled “Disable Abandon Detection”// Disable abandon detectionClientPoolConfig poolConfig = ClientPoolConfig.builder() .abandonTimeout(Duration.ZERO) .build();# Disable abandon detectionpool_config = PoolConfig(abandon_timeout_ms=0)// Disable abandon detectionconst pool = await ClientPool.create( { addresses: [{ host: 'localhost', port: 6379 }] }, { abandonTimeoutMs: 0 },);// Disable abandon detection (use a negative value in Go)poolCfg := glide.PoolConfig{ AbandonTimeout: -1 * time.Second,}Close the Pool
Section titled “Close the Pool”When your application shuts down, close the pool to release all connections.
pool.close();pool.close()
# Or use as an async context manager:async with await AsyncClientPool.create(config, pool_config) as pool: async with pool.borrow() as client: await client.set("key", "value")pool.close();pool.Close()Limitations
Section titled “Limitations”The following apply to the preview release.
By design
Section titled “By design”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
WATCHbefore releasing a client: On release the pool resets state withDISCARDandSELECT, which does not clear aWATCHthat was set without a followingMULTI. The next borrower inherits thatWATCH, so itsEXECcan abort over a key it never watched. Always end aWATCHwithEXEC,DISCARD, orUNWATCHbefore 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.
Known issues
Section titled “Known issues”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 byunwrap(), and useacquire(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
AUTHidentity,HELLO/RESETprotocol changes,CLIENT NO-EVICT,CLIENT NO-TOUCH,CLIENT SETNAME,CLIENT TRACKING, or clusterREADONLY. 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
AsyncClientPoolfails 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
0uses the configured default instead of not waiting in Python async, Go, and Node.js. Pool exhaustion is not raised as the binding’s GLIDETimeoutErrorin Java, Node.js, and Python. Use a positive timeout, and handle exhaustion as a generic error.