Redis Connections
RedisOfficialCreated by Redis, Inc.
- Skill
- Tool integration
- Software development
- MIT
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides best practices for configuring Redis clients to manage connections and execute commands effectively. It covers strategies for connection pooling and multiplexing, techniques for batching operations, and guidance on avoiding blocking commands in production environments. Additionally, it outlines how to implement client-side caching for specific workloads and how to configure appropriate socket timeouts.
What it does
- Explains how to use connection pooling and multiplexing to avoid per-request connection overhead.
- Demonstrates how to use pipelining to batch multiple commands into a single round-trip.
- Provides alternatives to blocking commands like KEYS, SMEMBERS, and HGETALL using incremental SCAN variants.
- Guides the implementation of client-side caching using the RESP3 protocol.
- Advises on setting explicit connect and read timeouts for Redis clients.
When to use it
- When setting up or reviewing a Redis client configuration.
- When optimizing applications that perform frequent small Redis operations.
- When working with large keyspaces or datasets that require iterative access.
- When identifying and addressing latency in Redis-dependent applications.
- When configuring timeouts for different application failure models.
Advantages
- Reduces network round-trips through command batching and pipelining.
- Prevents server-side blocking by replacing heavy commands with incremental alternatives.
- Improves resource usage by reusing connections instead of creating new ones per request.
- Provides mechanisms to reduce server load for frequently read data via client-side caching.
Usage examples
- “How do I avoid blocking the Redis server when iterating over all keys?”
- The skill recommends using the SCAN command with a cursor loop instead of the KEYS command to iterate through keyspaces incrementally.
- “Should I use a new connection for every Redis request?”
- The skill advises against this, suggesting either connection pooling or multiplexing depending on the client library used.
- “How can I reduce network round-trips for multiple Redis commands?”
- The skill suggests using pipelining to send multiple commands in a single batch.
- “When is it appropriate to enable client-side caching?”
- It is recommended for read-heavy workloads where data is updated rarely, such as configuration settings or feature flags.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/redis/redis-connections/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip redis-connections.zip -d .agents/skills
Documentation by Redis, Inc.
Redis Connections
Client-side guidance for talking to Redis efficiently: how to share connections, how to batch commands, which commands not to call in production, when to turn on client-side caching, and how to set timeouts that fail fast without breaking healthy traffic.
When to apply
Creating or reviewing a Redis client setup (redis-py, Jedis, Lettuce, go-redis, NRedisStack).
Making many small Redis calls and wondering where the latency is going.
Iterating large keyspaces, sets, hashes, or lists.
Enabling client-side caching for hot keys.
Tuning connect / read / write timeouts.
1. Pool or multiplex — never one connection per request
The single biggest mistake in Redis client code is opening a new TCP connection for every operation. Always either:
Pool — keep N persistent connections that the application leases per call (redis-py
ConnectionPool, JedisJedisPooled, go-redis client).Multiplex — share a single connection across all requests (Lettuce, NRedisStack).
| Style | Used by | Note |
|---|---|---|
| Pool | redis-py, Jedis, go-redis | Each lease blocks if pool exhausted; size the pool to your concurrency |
| Multiplex | Lettuce, NRedisStack | Single connection; cannot carry blocking commands like BLPOP |
# redis-py — connection pool
pool = redis.ConnectionPool(host="localhost", port=6379, max_connections=50)
r = redis.Redis(connection_pool=pool)See references/pooling.md for Python + Java + Lettuce examples.
2. Pipeline bulk work
For N commands that don't depend on each other's results, send them as a single batch with pipelining. One round-trip instead of N.
pipe = redis.pipeline()
for user_id in user_ids:
pipe.get(f"user:{user_id}")
results = pipe.execute()Use non-transactional pipelining for performance, and pipeline(transaction=True) only when you actually need atomicity (see redis-core's transactions guidance).
3. Avoid commands that scan everything
Anything that walks the whole keyspace (or a whole large container) blocks the server. Use incremental variants instead.
| Don't | Use |
|---|---|
KEYS pattern | SCAN cursor loop |
SMEMBERS large_set | SSCAN |
HGETALL large_hash | HSCAN |
LRANGE 0 -1 on a huge list | Paginate (LRANGE 0 100) |
cursor = 0
while True:
cursor, keys = redis.scan(cursor, match="user:*", count=100)
for key in keys:
process(key)
if cursor == 0:
breakBlocking commands (BLPOP, BRPOP, BLMOVE) are different — they intentionally wait for data and are fine for queue consumers, but always pass a timeout, and don't issue them on a multiplexed connection (Lettuce, NRedisStack).
4. Client-side caching for hot keys
For data that's read often and written rarely (config, feature flags, sessions on every request), enable RESP3 client-side caching. The client keeps a local copy and the server invalidates it on writes — saving the round trip for hot reads.
client = redis.Redis(
host="localhost",
port=6379,
protocol=3, # RESP3 is required
cache_config=redis.CacheConfig(max_size=1000),
)Skip it for write-heavy workloads or data that changes constantly — the invalidation traffic overruns the savings.
See references/client-cache.md.
5. Set explicit timeouts
Defaults vary by client and may be too generous. Pick values that match the application's failure model:
r = redis.Redis(
host="localhost",
socket_connect_timeout=2.0, # fail fast on dead nodes
socket_timeout=5.0, # tune to expected operation time
retry_on_timeout=True,
)Rule of thumb: connect timeout shorter than read/write timeout. Tight timeouts + retry-on-timeout for latency-sensitive paths; longer timeouts for batch jobs.
References
Files
- ATTRIBUTION.mdAdded by AutoGineer894 B
- LICENSELicence file added by AutoGineer1.0 KiB
- SKILL.md5.0 KiB
- references/blocking.md1.8 KiB
- references/client-cache.md2.0 KiB
- references/pipelining.md1.1 KiB
- references/pooling.md1.9 KiB
- references/timeouts.md1.3 KiB
references/pooling.md
Use Connection Pooling or Multiplexing
Reuse connections via a pool or multiplexing instead of creating new connections per request.
Correct: Use a connection pool.
Python (redis-py):
import redis
# Good: Connection pool - reuses existing connections
pool = redis.ConnectionPool(host='localhost', port=6379, max_connections=50)
r = redis.Redis(connection_pool=pool)Java (Jedis):
import redis.clients.jedis.JedisPooled;
// JedisPooled manages a connection pool internally
try (JedisPooled jedis = new JedisPooled("redis://localhost:6379")) {
jedis.set("testKey", "testValue");
}Correct: Use multiplexing (Lettuce, NRedisStack).
// Lettuce uses multiplexing by default - single connection handles all traffic
RedisClient client = RedisClient.create("redis://localhost:6379");
StatefulRedisConnection<String, String> connection = client.connect();
// All commands share the single connection efficiently
connection.sync().set("key", "value");Incorrect: Creating new connections per request.
Python (redis-py):
# Bad: New connection every time
def get_user(user_id):
r = redis.Redis(host='localhost', port=6379) # Don't do this
return r.get(f"user:{user_id}")Java (Jedis):
// Bad: Creating new client per request
public String getUser(String userId) {
try (UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379")) {
return jedis.get("user:" + userId); // Don't do this
}
}Pooling vs Multiplexing:
Pooling: Multiple connections shared across requests (redis-py, Jedis, go-redis)
Multiplexing: Single connection handles all traffic (NRedisStack, Lettuce)
Multiplexing cannot support blocking commands (BLPOP, etc.) as they would stall all callers
Reference: Connection Pools and Multiplexing
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Needs Python
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- Redis, Inc.
- Publisher
- Redis (Official)
- Source
- redis/agent-skills · skills/redis-connections · commit 74708da
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
- Also distributed in
- redis/agent-skills
AutoGineer is not affiliated with or endorsed by Redis, Inc. or Redis.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download redis-connections.zip