jmservera/aithena/redis-connection-patterns
Claude Code and agent skill by jmservera/aithena
- community
Checking that the SKILL.md file is still in the repository.
Proper Redis authentication, connection pooling, and batch operations for multi-worker FastAPI services
Install
npx vskill@latest install jmservera/aithena/redis-connection-patternsSKILL.md
The same SKILL.md was published earlier as jmservera/aithena/redis-connection-patterns.
Context
Redis is used in aithena for:
- Document indexing state (processed/failed counts)
- Rate limiting (login attempts per IP)
- Caching (search results, embeddings)
- Session state (optional, for future auth)
Multi-worker FastAPI services (solr-search runs with uvicorn workers) require thread-safe connection management. Mistakes can cause:
- Silent authentication failures (502 errors on /stats and /search)
- Lost rate limiting (attacks possible)
- Memory leaks (unclosed connections)
- Cascading failures (Solr health check fails when Redis fails)
Patterns
1. ConnectionPool Must Include Password Parameter
❌ CRITICAL BUG — password was missing:
✅ FIXED — password must be passed to ConnectionPool:
Why: The ConnectionPool constructor creates all connections in the pool with the specified parameters. Passing password to Redis(pool=pool) is not enough — the pool itself must know about the password.
2. Singleton ConnectionPool with Double-Checked Locking
For thread-safe, lazy-initialized connection pool:
Why: Double-checked locking avoids lock contention after initialization. All FastAPI workers share the same pool and its connections.
3. Use `scan_iter()` Instead of `KEYS`
❌ Bad — blocks Redis server:
✅ Good — iterates in batches:
Why: KEYS is O(N) and blocks the entire server until complete. scan_iter() uses SCAN cursor-based iteration (non-blocking, batches of ~100 keys).
4. Use `mget()` for Batch Reads
❌ Bad — N round-trips to Redis:
✅ Good — single batch operation:
Why: Each get() requires a network round-trip. mget() fetches all keys in one command, dramatically faster.
5. Use `mset()` for Batch Writes
❌ Bad — N round-trips:
✅ Good — single batch:
6. Set TTL for Temporary State
For rate limiting and time-bounded state:
Or in one command:
7. Credential Sourcing
Always use environment variables:
In docker-compose.yml:
Why: Never hardcode passwords. Separate code from configuration.
Examples
Reference implementations in aithena:
src/solr-search/main.py— connection pool (fixed version)src/solr-search/search_service.py— batch operations for indexing statesrc/document-indexer/— rate limiting usagesrc/admin/src/pages/shared/config.py— health check
Anti-Patterns
- Don't use `redis.Redis(..., password=...)` alone — must also update ConnectionPool
- Don't use `KEYS` in production — use SCAN or maintain explicit key registries
- Don't chain individual `get()`/`set()` calls — use
mget()/mset()for batches - Don't forget TTL on rate limiting keys — they'll accumulate forever
- Don't assume connection pooling is automatic — FastAPI doesn't auto-pool; you must create it
- Don't share redis.Redis() instances across threads — thread-safe only when using ConnectionPool
Testing
Mock Redis for unit tests:
Scope & Enforcement
Applies to:
- solr-search (state tracking, rate limiting, caching)
- document-indexer (state management)
- document-lister (optional, future optimization)
- admin (optional, dashboard state)
Checked in:
- Code reviews (architecture audit)
- Unit tests (mocked Redis behavior)
- Integration tests (real Redis container)
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryChecking.squad/skills/redis-connection-patterns/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/jmservera/aithena/redis-connection-patterns)