redis-state-management

Comprehensive guide for Redis state management including caching strategies, session management, pub/sub patterns, distributed locks, and data structures

By manutej · 443 installs

npx skills add manutej/luxor-claude-marketplace --skill redis-state-management

Source repository · Upstream listing

Redis State Management A comprehensive skill for mastering Redis state management patterns in distributed systems. This skill covers caching strategies, session management, pub/sub messaging, distributed locks, data structures, and production ready patterns using redis py. When to Use This Skill Use this skill when: Implementing high performance caching layers for web applications Managing user sessions in distributed environments Building real time messaging and event distribution systems Coordinating distributed processes with locks and synchronization Storing and querying structured data with Redis data structures Optimizing application performance with Redis Scaling applications horizontally with shared state Implementing rate limiting, counters, and analytics Building microservices with Redis as a communication layer Managing temporary data with automatic expiration (TTL) Implementing leaderboards, queues, and real time features Core Concepts Redis Fundamentals Redis (Remote Dictionary Server) is an in memory data structure store used as: Database : Persistent key value storage Cache : High speed data layer Message Broker : Pub/sub and stream messaging Session Store : Distributed session management Key Characteristics: In memory storage (microsecond latency) Optional persistence (RDB snapshots, AOF logs) Rich data structures beyond key value Atomic operations on complex data types Built in replication and clustering Pub/sub messaging support Lua scripting for complex operations Pipelining for batch operations Redis Data Structures Redis provides multiple data types for different use cases: 1. Strings : Simple key value pairs, binary safe Use for: Cache values, counters, flags, JSON objects Max size: 512 MB Commands: SET, GET, INCR, APPEND 2. Hashes : Field value maps (objects) Use for: User profiles, configuration objects, small entities Efficient for storing objects with multiple fields Commands: HSET, HGET, HMGET, HINCRBY 3. Lists : Ordered collections (linked lists) Use for: Queues, activity feeds, recent items Operations at head/tail are O(1) Commands: LPUSH, RPUSH, LPOP, RPOP, LRANGE 4. Sets : Unordered unique collections Use for: Tags, unique visitors, relationships Set operations: union, intersection, difference Commands: SADD, SMEMBERS, SISMEMBER, SINTER 5. Sorted Sets : Ordered sets with scores Use for: Leaderboards, time series, priority queues Range queries by score or rank Commands: ZADD, ZRANGE, ZRANGEBYSCORE, ZRANK 6. Streams : Append only logs with consumer groups Use for: Event sourcing, activity logs, message queues Built in consumer group support Commands: XADD, XREAD, XREADGROUP Connection Management Connection Pools: Redis connections are expensive to create. Always use connection pools: Best Practices: Use connection pools for all applications Set appropriate max connections based on workload Enable decode responses=True for string data Configure socket timeout and socket keepalive Handle connection errors with retries Data Persistence Redis offers two persistence mechanisms: RDB (Redis Database) : Point in time snapshots Compact binary format Fast restart times Lower disk I/O Potential data loss between snapshots AOF (Append Only File) : Log of write operations Better durability (fsync policies) Larger files, slower restarts Can be automatically rewritten/compacted Minimal data loss potential Hybrid Approach : RDB + AOF for best of both worlds RESP 3 Protocol Redis Serialization Protocol version 3 offers: Client side caching support Better data type support Push notifications Performance improvements Caching Strategies Cache Aside (Lazy Loading) Pattern : Application checks cache first, loads from database on miss Advantages: Only requested data is cached (efficient memory usage) Cache failures don't break the application Simple to implement Disadvantages: Cache miss penalty (latency spike) Thundering herd on popular items Stale data until cache expiration Write Through Cache Pattern : Write to cache and database simultaneously Advantages: Cache always consistent with database No read penalty for recently written data Disadvantages: Write latency increases Unused data may be cached Extra cache write overhead Write Behind (Write Back) Cache Pattern : Write to cache immediately, sync to database asynchronously Advantages: Minimal write latency Can batch database writes Handles write spikes Disadvantages: Risk of data loss if cache fails Complex error handling Consistency challenges Cache Invalidation Strategies Time based Expiration (TTL): Event based Invalidation: Pattern based Invalidation: Cache Stampede Prevention Problem : Multiple requests simultaneously miss cache and query database Solution 1: Probabilistic Early Expiration Solution 2: Locking Session Management Distributed Session Storage Basic Session Management: Session with Hash Storage More efficient for session objects: User Activity Tracking Pub/Sub Patterns Basic Publisher/Subscriber Publisher: Subscriber: Pattern Based Subscriptions Async Pub/Sub with Background Thread Async Pub/Sub with asyncio Sharded Pub/Sub (Redis 7.0+) Distributed Locks Simple Lock Implementation Advanced Lock with Auto Renewal Redlock Algorithm (Multiple Redis Instances) Data Structures and Operations Working with Hashes Working with Lists Working with Sets Working with Sorted Sets Working with Streams Performance Optimization Pipelining for Batch Operations Transactions with WATCH Lua Scripts for Complex Operations Production Patterns High Availability with Sentinel Async Redis with asyncio Connection Pool Configuration Error Handling and Resilience Monitoring and Metrics Best Practices Key Naming Conventions Use consistent, hierarchical naming: Memory Management Security Testing Examples Example 1: User Session Management with Redis Example 2: Real Time Leaderboard Example 3: Distributed Rate Limiter Example 4: Distributed Job Queue Example 5: Real Time Event Streaming Example 6: Cache Aside Pattern with Multi Level Caching Example 7: Geo Location with Redis Quick Reference Common Operations Time Complexity GET, SET: O(1) HGET, HSET: O(1) LPUSH, RPUSH, LPOP, RPOP: O(1) SADD, SREM, SISMEMBER: O(1) ZADD, ZREM: O(log(N)) ZRANGE, ZREVRANGE: O(log(N)+M) where M is result size SCAN, SSCAN, HSCAN, ZSCAN: O(1) per iteration Skill Version : 1.0.0 Last Updated : October 2025 Skill Category : State Management, Distributed Systems, Performance Optimization Compatible With : redis py, Redis 6.0+, Redis 7.0+