56 results available
Sign in
Skills directory

redis-connections

redis

·

Last changed
Hardened

This skill has been hardened by Chainguard

Last changed

This version passed the hardening gate. Hardening applied 11 targeted changes and preserved all other text. Classified findings before hardening: 0. Classified findings closed: 0. Classified findings remaining: 0. Unclassified scanner findings remaining: 0. Review findings closed: 7. Review findings remaining: 1.


Open before hardening

None

Open after hardening

None


Expand
File
Group
Subtype
Severity
Status

No findings available

0 findings


SKILL.md

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

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

Set a bounded wait timeout for leasing a pooled connection, so callers fail fast instead of hanging when the pool is exhausted.

# redis-py — connection pool
pool = redis.ConnectionPool(host="localhost", port=6379, max_connections=50)
r = redis.Redis(connection_pool=pool)

These examples connect to a local development server. In production, enable authentication and TLS with certificate verification, and load the username and password from the environment or a secret store rather than source code.

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. In these snippets redis stands for a client instance, such as r from section 1, not the module.

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'tUse

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), 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),
)

Client-side caching needs a redis-py release that supports it, protocol 3, and a Redis server with RESP3 client tracking; check where your client version exports the cache config class. Keep session, authentication and revocation state out of the local cache, since it changes often and must stay current after a missed invalidation or reconnect.

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.

Use retry on timeout only for commands that are safe to repeat, such as reads. A timed out write may already have been applied, so for counters, list pushes and other writes that are not idempotent, turn retries off or use transactions. Newer redis-py versions favor an explicit retry policy with backoff over the simple flag; check your client version. These timeout examples also target a local server; apply the same production authentication and TLS settings described in section 1.

See references/timeouts.md.

References

Install

Install with chainctl
chainctl skill install public/redis/redis-connections:latest
Get started with chainctl
View source

Harden your own skills

Bring Chainguard's hardening pipeline to your first-party skills — custom rulesets, automated remediation, full audit trail.

Provenance

Upstream sourcehttps://github.com/redis/agent-skills
Upstream commitd3d89fae1110012741e07b46926e91119d31e0e7
OCI reference

skills.cgr.dev/public/github.com/redis/skills/redis-connections:latest

License

MIT

Files

HARDENING.md

LICENSE

SKILL.md

references/blocking.md

references/client-cache.md

references/pipelining.md

references/pooling.md

references/timeouts.md


The trusted source for open source

Talk to an expert
PrivacyTerms

Product

Chainguard ContainersChainguard LibrariesChainguard VMsChainguard OS PackagesChainguard ActionsChainguard Agent SkillsIntegrationsPricing
© 2026 Chainguard, Inc. All Rights Reserved.
Chainguard® and the Chainguard logo are registered trademarks of Chainguard, Inc. in the United States and/or other countries.
The other respective trademarks mentioned on this page are owned by the respective companies and use of them does not imply any affiliation or endorsement.