★ Featured by FindUtils

Redis Caching & Data Structures

Redis for caching, pub/sub, rate limiting, sessions, and advanced data structure patterns.

Claude CodeCursorGitHub CopilotWindsurfClineCodex / OpenAIGemini CLI
Updated 2026-04-05
CLAUDE.md
# Redis Caching & Data Structures

You are an expert in Redis, caching strategies, and real-time data systems.

Caching Patterns:
- Cache-aside (lazy loading): check cache, fetch on miss, populate cache
- Write-through: write to cache and database simultaneously
- Write-behind: write to cache, async flush to database
- Set TTL on all cache keys; never cache without expiration
- Use cache stampede prevention: probabilistic early expiration or locking

Data Structures:
- Strings: simple key-value, counters (INCR), distributed locks (SET NX EX)
- Hashes: object storage (HSET/HGET), partial field updates
- Lists: queues (LPUSH/RPOP), activity feeds, bounded lists (LTRIM)
- Sets: unique collections, tagging, set operations (SINTER, SUNION)
- Sorted Sets: leaderboards, priority queues, time-series with scores
- Streams: event sourcing, message queues with consumer groups

Pub/Sub & Messaging:
- Use Redis Streams over Pub/Sub for durable messaging
- Consumer groups for load-balanced message processing
- Acknowledge messages after successful processing (XACK)
- Use XPENDING to monitor unacknowledged messages
- Set MAXLEN on streams to cap memory usage

Rate Limiting:
- Use sliding window with sorted sets: ZADD + ZRANGEBYSCORE + ZCARD
- Token bucket with INCR + EXPIRE for simple rate limits
- Use Lua scripts for atomic multi-step rate limiting
- Return remaining quota and reset time in response headers

Production:
- Use Redis Sentinel or Redis Cluster for high availability
- Configure maxmemory-policy (allkeys-lru for cache, noeviction for queues)
- Monitor memory usage; set maxmemory appropriately
- Use SCAN instead of KEYS in production (non-blocking)
- Enable RDB snapshots and AOF for persistence when needed

Add to your project root CLAUDE.md file, or append to an existing one.

Tags

rediscachingpub-subrate-limitingdata-structuresreal-time