# Redis Cheatsheet ## Memory & Diagnostics ```bash # Overall memory stats redis-cli INFO memory # Human-readable used memory redis-cli INFO memory | grep used_memory_human # Total number of keys redis-cli DBSIZE # Memory usage of a specific key (in bytes) redis-cli MEMORY USAGE "some:key" # Find biggest keys per type (safe, non-blocking) redis-cli --bigkeys # Top 30 keys by memory usage (scans first 500 keys) redis-cli --scan --pattern "*" | head -500 | while read key; do bytes=$(redis-cli MEMORY USAGE "$key" 2>/dev/null) echo "$bytes $key" done | sort -rn | head -30 # Full scan (slow on large datasets) redis-cli --scan | while read key; do bytes=$(redis-cli MEMORY USAGE "$key" 2>/dev/null) echo "$bytes $key" done | sort -rn | head -50 # Memory doctor -- quick health check redis-cli MEMORY DOCTOR ``` ## Key Inspection ```bash # Count keys matching a pattern redis-cli --scan --pattern "cache:*" | wc -l redis-cli --scan --pattern "horizon:*" | wc -l redis-cli --scan --pattern "queues:*" | wc -l # Check key type redis-cli TYPE "some:key" # Check TTL (-1 = no expiry, -2 = key doesn't exist) redis-cli TTL "some:key" # Find keys with no expiry (sample first 100) redis-cli --scan --pattern "*" | head -100 | while read key; do ttl=$(redis-cli TTL "$key") if [ "$ttl" = "-1" ]; then echo "NO EXPIRY: $key ($(redis-cli MEMORY USAGE "$key") bytes)"; fi done # List all key prefixes and their counts redis-cli --scan | sed 's/:[^:]*$//' | sort | uniq -c | sort -rn | head -30 ``` ## Streams ```bash # Stream length redis-cli XLEN "stream:key" # Stream info (memory, first/last entry, etc.) redis-cli XINFO STREAM "stream:key" # Trim stream to max N entries redis-cli XTRIM "stream:key" MAXLEN 10000 # Delete a stream entirely redis-cli DEL "stream:key" # Read last 5 entries redis-cli XREVRANGE "stream:key" + - COUNT 5 ``` ## Hashes, Lists, Sets, Sorted Sets ```bash # Hash: field count and sample fields redis-cli HLEN "hash:key" redis-cli HSCAN "hash:key" 0 COUNT 10 # List: length and sample redis-cli LLEN "list:key" redis-cli LRANGE "list:key" 0 9 # Set: member count and sample redis-cli SCARD "set:key" redis-cli SSCAN "set:key" 0 COUNT 10 # Sorted set: member count and top 10 by score redis-cli ZCARD "zset:key" redis-cli ZREVRANGE "zset:key" 0 9 WITHSCORES ``` ## Memory Limits & Eviction ```bash # Set memory limit (runtime, no restart) redis-cli CONFIG SET maxmemory 2gb # Set eviction policy redis-cli CONFIG SET maxmemory-policy volatile-lru # Persist config changes to redis.conf redis-cli CONFIG REWRITE # Check current settings redis-cli CONFIG GET maxmemory redis-cli CONFIG GET maxmemory-policy redis-cli CONFIG GET save ``` ### Eviction policies | Policy | Behavior | |---|---| | `noeviction` | Refuse writes when full (default -- dangerous) | | `volatile-lru` | Evict LRU keys that have a TTL set | | `allkeys-lru` | Evict any LRU key | | `volatile-ttl` | Evict keys with shortest TTL first | | `volatile-random` | Evict random keys that have a TTL | | `allkeys-random` | Evict any random key | ## Persistence ```bash # Disable RDB snapshots (prevents fork that doubles memory) redis-cli CONFIG SET save "" # Trigger manual save (blocks until done) redis-cli SAVE # Trigger background save (forks -- dangerous if low memory) redis-cli BGSAVE # Check last save time redis-cli LASTSAVE # Check if background save is in progress redis-cli INFO persistence | grep rdb_bgsave_in_progress ``` ## Bulk Operations ```bash # Delete all keys matching a pattern (use with care) redis-cli --scan --pattern "prefix:*" | xargs -L 100 redis-cli DEL # Count + size of keys matching a pattern redis-cli --scan --pattern "horizon:*" | while read key; do redis-cli MEMORY USAGE "$key" done | awk '{s+=$1} END {printf "Total: %.2f MB\n", s/1024/1024}' # Flush current database (DESTRUCTIVE) redis-cli FLUSHDB # Flush all databases (DESTRUCTIVE) redis-cli FLUSHALL ``` ## Monitoring ```bash # Watch commands in real time (Ctrl+C to stop) redis-cli MONITOR # Slow log -- queries that took longer than threshold redis-cli SLOWLOG GET 10 # Connected clients redis-cli INFO clients | grep connected_clients # Stats since startup redis-cli INFO stats ``` ## Config File Locations ``` /etc/redis/redis.conf # Debian/Ubuntu default /etc/redis.conf # RHEL/CentOS /etc/redis/6379.conf # Multi-instance setups ```