🦀 ClawHub
Redis
by @ivangdavila
Use Redis effectively for caching, queues, and data structures with proper expiration and persistence.
TERMINAL
clawhub install redis-store📖 About This Skill
name: Redis description: Use Redis effectively for caching, queues, and data structures with proper expiration and persistence. metadata: {"clawdbot":{"emoji":"🔴","requires":{"anyBins":["redis-cli"]},"os":["linux","darwin","win32"]}}
Expiration (Memory Leaks)
SET key value EX 3600SETEX or SET ... EXEXPIRE resets on key update by default—SET removes TTL; use SET ... KEEPTTL (Redis 6+)SCAN with large database: expired keys still show until cleanup cycle runsData Structures I Underuse
ZADD limits:{user} {now} {request_id} + ZREMRANGEBYSCORE for sliding windowPFADD visitors {ip} uses 12KB for billions of uniquesXADD, XREAD, XACK—better than LIST for reliable queuesHSET user:1 name "Alice" email "a@b.com"—more memory efficient than JSON stringAtomicity Traps
GET then SET is not atomic—another client can modify between; use INCR, SETNX, or LuaSETNX for locks: SET lock:resource {token} NX EX 30—NX = only if not existsWATCH/MULTI/EXEC for optimistic locking—transaction aborts if watched key changedEVAL "script" keys argsPub/Sub Limitations
XREAD BLOCK + XACK patternPersistence Configuration
appendfsync everysec is good balanceBGSAVE for manual snapshot—doesn't block but forks process, needs memory headroomMemory Management (Critical)
maxmemory must be set—without it, Redis uses all RAM, then swap = disasterallkeys-lru for cache, volatile-lru for mixed, noeviction for persistent dataINFO memory shows usage—monitor used_memory vs maxmemoryClustering
{user:1}:profile and {user:1}:sessions go to same slot—use for related keysMGET/MSET—error unless all keys in same slotMOVED redirect: client must follow—use cluster-aware client libraryCommon Patterns
INCR requests:{ip}:{minute} with EXPIRE—simple fixed windowSET ... NX EX + unique token—verify token on releaseConnection Management
QUIT on shutdown—graceful disconnectCommon Mistakes
KEYS * blocks everything; use SCANmaxmemory—production Redis without limit will crash host