4. Common Commands Reference
4.1 General Commands
bash
# View all keys (use with caution in production!)
KEYS *
# Use SCAN instead of KEYS in production
SCAN 0 MATCH user:* COUNT 10
# Check key data type
TYPE user:1001
# Check if key exists
EXISTS user:1001
EXISTS user:9999
# Delete keys
DEL temp:key1 temp:key2
# Set expiration time
EXPIRE session:abc123 3600
# Check remaining TTL
TTL session:abc123
# Remove expiration
PERSIST session:abc123
# Rename key
RENAME old_key new_key
# Check database size
DBSIZEbash
(integer) 1
"hash"
(integer) 1
(integer) 0
(integer) 2
(integer) 1
(integer) 3542
(integer) 1
OK
(integer) 1574324.2 String Commands
bash
# Set and get
SET name "Redis"
GET name
# Set only if not exists (for distributed locks)
SETNX lock:1 "holder"
# Batch operations
MSET k1 "v1" k2 "v2" k3 "v3"
MGET k1 k2 k3
# Increment / Decrement
SET counter 10
INCR counter
INCRBY counter 5
DECR counter
DECRBY counter 3
# Append string
SET greeting "Hello"
APPEND greeting " World"
GET greeting
# Get string length
STRLEN greetingbash
"Redis"
OK
OK
1) "v1"
2) "v2"
3) "v3"
OK
(integer) 11
(integer) 16
(integer) 15
(integer) 12
6
"Hello World"
(integer) 114.3 List Commands
bash
# Push to both ends
LPUSH mylist "a" "b" "c"
RPUSH mylist "d" "e"
# View list
LRANGE mylist 0 -1
# Pop from both ends
LPOP mylist
RPOP mylist
# Get by index
LINDEX mylist 0
# List length
LLEN mylist
# Blocking pop (wait 10 seconds)
BRPOP empty_list 10bash
(integer) 3
(integer) 5
1) "c"
2) "b"
3) "a"
4) "d"
5) "e"
"c"
"e"
"b"
(integer) 3
nil4.4 Hash Commands
bash
# Set fields
HSET product:1001 name "Mechanical Keyboard" price 399 stock 50
# Get field
HGET product:1001 name
# Batch get
HMGET product:1001 name price stock
# Get all fields and values
HGETALL product:1001
# Field count
HLEN product:1001
# Increment
HINCRBY product:1001 stock -1
# Delete field
HDEL product:1001 stockbash
(integer) 3
"Mechanical Keyboard"
1) "Mechanical Keyboard"
2) "399"
3) "50"
1) "name"
2) "Mechanical Keyboard"
3) "price"
4) "399"
5) "stock"
6) "50"
(integer) 3
(integer) 49
(integer) 14.5 Set Commands
bash
# Add members
SADD fruits "apple" "banana" "cherry"
SADD fruits "apple"
# View all members
SMEMBERS fruits
# Check membership
SISMEMBER fruits "banana"
SISMEMBER fruits "grape"
# Set size
SCARD fruits
# Random member
SRANDMEMBER fruits 1
# Remove member
SREM fruits "banana"
# Set operations
SADD setA "1" "2" "3"
SADD setB "2" "3" "4"
SINTER setA setB
SUNION setA setB
SDIFF setA setBbash
(integer) 3
(integer) 0
1) "apple"
2) "banana"
3) "cherry"
(integer) 1
(integer) 0
(integer) 3
1) "cherry"
(integer) 1
(integer) 2
1) "2"
2) "3"
1) "1"
2) "2"
3) "3"
4) "4"
1) "1"4.6 Sorted Set Commands
bash
# Add members with scores
ZADD scores 85 "Alice" 92 "Bob" 78 "Charlie" 96 "Diana" 88 "Eve"
# Ascending order (score low to high)
ZRANGE scores 0 -1 WITHSCORES
# Descending order (score high to low, ideal for leaderboards)
ZREVRANGE scores 0 2 WITHSCORES
# Get score
ZSCORE scores "Bob"
# Get rank
ZREVRANK scores "Alice"
# Score range query
ZRANGEBYSCORE scores 80 95 WITHSCORES
# Count members in score range
ZCOUNT scores 80 95
# Member count
ZCARD scores
# Increase score
ZINCRBY scores 5 "Charlie"bash
1) "Charlie"
2) "78"
3) "Alice"
4) "85"
5) "Eve"
6) "88"
7) "Bob"
8) "92"
9) "Diana"
10) "96"
1) "Diana"
2) "96"
3) "Bob"
4) "92"
5) "Eve"
6) "88"
"92"
(integer) 3
1) "Alice"
2) "85"
3) "Eve"
4) "88"
5) "Bob"
6) "92"
(integer) 3
(integer) 5
"83"4.7 Server Management Commands
bash
# View server info
INFO
# View memory info
INFO memory
# View client connections
CLIENT LIST
# View slow query log
SLOWLOG GET 10
# Check key count in current database
DBSIZE
# Switch database (0-15)
SELECT 1
# Flush current database
FLUSHDB
# Flush all databases (extremely dangerous!)
FLUSHALL
# Real-time command monitor
MONITOR
# View configuration
CONFIG GET maxmemory
CONFIG SET maxmemory 4gbbash
# Server
redis_version:7.2.4
redis_mode:standalone
os:Linux 5.15.0-181-generic x86_64
# Memory
used_memory_human:1.45M
maxmemory_human:0B
# Clients
connected_clients:3
(integer) 157432
OK
OK
OK
OK
1) "maxmemory"
2) "0"
OK