All skills
redis avatar

/redis-connections

@d3d89fa official
by redisredis/agent-skills163 stars
30

Redis client and connection guidance covering connection pooling, multiplexing, pipelining, client-side caching with RESP3, avoiding slow commands (KEYS, SMEMBERS, HGETALL), and tuning socket timeouts. Use when configuring a Redis client (redis-py, Jedis, Lettuce, NRedisStack), batching commands for throughput, eliminating per-request connection creation, iterating large keyspaces with SCAN, enabling client-side caching for read-heavy workloads, or setting connect and read timeouts.

Use this Skill: https://skilld.dev/gh/redis/agent-skills/redis-connections

This session only. Nothing lands on disk.

SKILL.md

β‰ˆ127 tokens always: the name and description. β‰ˆ1.1k when used: this file. β‰ˆ2.1k more on demand in 5 files.

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, Jedis JedisPooled, 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).

See references/pipelining.md.

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:
        break

Blocking 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).

See references/blocking.md.

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.

See references/timeouts.md.

References

Source: SKILL.md on GitHub

No alerts1mo3 checks Β· Risk SAFE
  • Gen Agent Trust Hub1mo

    This skill provides architectural guidance and code examples for Redis connection management, authored by Redis, Inc. It contains no malicious patterns or security risks.

  • Socket1mo

    No alerts

  • Snyk1mo

    Risk: LOW Β· No issues

Signed by skilld at d3d89fa. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Activeupdated 4 months ago
metadata
{
  "author": "Redis, Inc.",
  "version": "0.1.0"
}
  • redis
  • connection-pooling
  • pipelining
  • resp3
  • client-side-caching
  • timeouts
  • redis-py
  • jedis
  • lettuce
  • performance-tuning

README badge

README badge for redis/agent-skills/redis-connections

Provides patterns for configuring Redis clients to use connection pooling or multiplexing, pipelining bulk commands, avoiding blocking full-keyspace scans, enabling RESP3 client-side caching, and setting appropriate socket timeouts. Covers redis-py, Jedis, Lettuce, go-redis, and NRedisStack.

Generated from the current SKILL.md.

Which Redis clients does this skill cover?
It covers redis-py, Jedis, Lettuce, go-redis, and NRedisStack. The guidance applies to any client but examples are tailored to these libraries.
Does this skill explain how to use blocking commands like BLPOP?
Yes. It notes that blocking commands are fine for queue consumers but must always have a timeout set, and cannot be issued on a multiplexed connection (Lettuce, NRedisStack).
When should I enable client-side caching?
Enable it for read-heavy workloads with infrequent writes, like config, feature flags, or session data on every request. Skip it for write-heavy or frequently changing data, where invalidation traffic outweighs the savings.
What's the difference between connection pooling and multiplexing?
Pooling (redis-py, Jedis, go-redis) keeps multiple persistent connections that are leased per call and blocks if exhausted. Multiplexing (Lettuce, NRedisStack) shares a single connection across all requests and cannot carry blocking commands.
Why should I avoid the KEYS command in production?
KEYS scans the entire keyspace and blocks the server. Use SCAN with a cursor loop instead, or SSCAN/HSCAN for sets and hashes.

Generated from the current SKILL.md. These answers refresh after source changes.