Use Redis effectively for caching, queues, and data structures with proper expiration and persistence.
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 runsZADD 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 "[email protected]"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 argsXREAD BLOCK + XACK patternappendfsync everysec is good balanceBGSAVE for manual snapshot—doesn't block but forks process, needs memory headroommaxmemory 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 maxmemory{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 libraryINCR requests:{ip}:{minute} with EXPIRE—simple fixed windowSET ... NX EX + unique token—verify token on releaseQUIT on shutdown—graceful disconnectKEYS * blocks everything; use SCANmaxmemory—production Redis without limit will crash host