Redis Connections

作者 redisa84871d065f3MIT收录于 2026年10月8日更新于 2026年10月8日

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.

AI 生成的概览

配置 Redis 客户端的指导:连接池、流水线、SCAN、客户端缓存与超时设置。

功能
该技能提供高效访问 Redis 的客户端侧指导,涵盖连接池与多路复用的选择、用流水线批量发送命令、用 SCAN 系列增量命令替代 KEYS、HGETALL 等全量扫描命令、为热点键启用 RESP3 客户端缓存,以及调整连接与读取超时。它产出针对 redis-py、Jedis、Lettuce、go-redis、NRedisStack 等客户端的配置建议和代码片段,并配有各主题的参考文档。
适用场景
适用于创建或审查 Redis 客户端配置、大量小请求导致延迟不明原因升高,以及遍历大型键空间、集合、哈希或列表的场景。也适合为读多写少的负载启用客户端缓存,以及设置连接、读取和写入超时。
运行要求
不附带脚本,仅为说明文档与参考文档。实践示例需要 Redis 服务器以及 redis-py、Jedis、Lettuce、go-redis 或 NRedisStack 等 Redis 客户端库;客户端缓存部分需要 RESP3 支持。

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).
StyleUsed byNote
Poolredis-py, Jedis, go-redisEach lease blocks if pool exhausted; size the pool to your concurrency
MultiplexLettuce, NRedisStackSingle connection; cannot carry blocking commands like BLPOP
python
# redis-py — connection poolpool = redis.ConnectionPool(host="localhost", port=6379, max_connections=50)r = redis.Redis(connection_pool=pool)

See references/pooling.md [blocked] 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.

python
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 [blocked].

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'tUse
KEYS patternSCAN cursor loop
SMEMBERS large_setSSCAN
HGETALL large_hashHSCAN
LRANGE 0 -1 on a huge listPaginate (LRANGE 0 100)
python
cursor = 0while 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 [blocked].

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.

python
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 [blocked].

5. Set explicit timeouts

Defaults vary by client and may be too generous. Pick values that match the application's failure model:

python
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 [blocked].

References

来源与署名

来源:redis/agent-skills位于plugins/redis-development/skills/redis-connections提交a84871d

许可证: MIT

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架