Bookmark
🗂️ Hash
HSET colors white fffff
HSET colors black 000000
HSET colors blue 0000FF
HGET colors white
HDEL colors white
HGETALL colors # Get all fields and values
HMSET user:1 name Alice age 30
HINCRBY user:1 age 1 # Increment field value
HEXISTS colors black # Check if field exists
HKEYS colors # List all fields
HVALS colors # List all values
📋 List
LPUSH tasks task1
LPUSH tasks task2
RPUSH tasks task3
RPOP tasks # Remove and get last element
LPOP tasks # Remove and get first element
LRANGE tasks 0 2 # Get first 3 elements
BRPOP tasks 5 # Blocking RPOP with timeout
BLPOP tasks 5 # Blocking LPOP with timeout
LLEN tasks # List length
LREM tasks 1 task2 # Remove first occurrence of value
LSET tasks 0 urgent_task # Set value at index
🧩 Set
SADD colors white
SADD colors black
SADD colors blue
SREM colors white
SMEMBERS colors # List all members
SISMEMBER colors black # Check membership
SCARD colors # Set cardinality
SRANDMEMBER colors # Get random member
SPOP colors # Remove and return random member
SDIFF colors othercolors # Diff sets
SINTER colors othercolors # Intersect sets
SUNION colors othercolors # Union sets
📊 Sorted Set
ZADD scores 100 "Alice"
ZADD scores 200 "Bob"
ZINCRBY scores 50 "Alice" # Increment score
ZRANGE scores 0 -1 WITHSCORES # Get all members with scores
ZREVRANGE scores 0 1 WITHSCORES # Top 2 scores
ZREM scores "Bob" # Remove member
ZCOUNT scores 100 200 # Count members in score range
ZRANK scores "Alice" # Get rank of member
ZPOPMAX scores # Remove & return member with max score
ZPOPMIN scores # Remove & return member with min score
ZSCAN scores 0 MATCH "A*" # Scan by pattern
📝 String
SET welcomeMessage "Hello"
APPEND welcomeMessage " World"
GET welcomeMessage
SET masterServer host123 EX 60 NX # EX: expire in seconds, NX: only if not exists
SET anotherKey value PX 5000 XX # PX: expire in ms, XX: only if exists
GETRANGE welcomeMessage 0 4 # Substring
STRLEN welcomeMessage # String length
INCR counter # Increment integer value
DECR counter # Decrement integer value
🛠️ MISC
FLUSHALL # Remove all keys from all DBs
FLUSHDB # Remove all keys from current DB
ECHO "Hello Redis" # Print message
TIME # Server time
PING # Test connection
🔑 Key Management
EXISTS keyname # Check if key exists
DEL keyname # Delete key
EXPIRE keyname 60 # Set key to expire in 60 seconds
TTL keyname # Time to live for key
PERSIST keyname # Remove expiration
RENAME oldkey newkey # Rename key
TYPE keyname # Get key type
KEYS *pattern* # List keys by pattern
SCAN 0 MATCH *pattern* COUNT 100 # Incremental key scan
DUMP keyname # Serialize key
RESTORE keyname 0 serialized_val # Restore key
🔄 Transactions
MULTI
SET foo bar
INCR counter
EXEC
DISCARD # Cancel transaction
WATCH key1 key2 # Watch keys for changes
UNWATCH
📡 Pub/Sub
SUBSCRIBE channel1
PUBLISH channel1 "Hello World"
UNSUBSCRIBE channel1
PSUBSCRIBE news.*
PUNSUBSCRIBE news.*
PUBSUB CHANNELS # List active channels
PUBSUB NUMSUB channel1 # Subscribers count
💾 Persistence & Info
SAVE # Synchronous save
BGSAVE # Background save
LASTSAVE # Last save timestamp
INFO # Server info and stats
DBSIZE # Number of keys in DB
MONITOR # Real-time command monitoring
CONFIG GET * # Show config
CONFIG SET maxmemory 256mb # Set config
🧠 Useful Patterns
SETNX lock_key "locked" # Simple lock
GETSET counter 0 # Get old value and set new
MSET key1 val1 key2 val2 # Set multiple keys
MGET key1 key2 # Get multiple keys
INCRBY counter 5 # Increment by value
DECRBY counter 2 # Decrement by value
BITCOUNT bitmap # Count set bits
PFADD hll user1 user2 # HyperLogLog add
PFCOUNT hll # HyperLogLog count
React
SQL
Want to update?
Subscribe to my blog to receive the latest updates from us.
Monthly articles
New articles will be created every month. No spam
All notifications are notified to you, not spam or advertising.