Connection Pooling¶
pyrsedis uses an async connection pool internally. Connections are created on demand and reused across calls.
How it works¶
- Each
Redis()instance owns one pool - Pool creates connections lazily (up to
pool_size) - Idle connections are reused in LIFO order (better cache warmth)
- Connections idle longer than
idle_timeout_msare dropped - Connections are initialized with AUTH + SELECT on creation
Configuration¶
r = Redis(
pool_size=8, # max concurrent connections
idle_timeout_ms=300_000, # drop idle connections after 5 minutes
)
Monitoring¶
Best practices¶
Match pool size to concurrency
If your app uses 4 threads hitting Redis, pool_size=4 is sufficient. Extra connections waste server memory (~10 KB each).
Don't create multiple Redis instances for the same server
Each Redis() creates its own pool. Sharing one instance across threads is safe and efficient.
Idle timeout
The default 5-minute idle timeout works well for most apps. Lower it if you're behind a firewall that kills idle TCP connections sooner.