Redis

Essential Redis commands for caching, data structures, and in-memory database operations.

databases
rediscachedatabasenosqlkey-value

Connection

# Connect to Redis
redis-cli

# Connect to remote Redis
redis-cli -h hostname -p 6379 -a password

# Ping server
PING

# Select database (0-15)
SELECT 0

Strings

# Set a key
SET key "value"

# Set with expiration (seconds)
SET key "value" EX 60

# Set with expiration (milliseconds)
SET key "value" PX 60000

# Set only if key doesn't exist
SETNX key "value"

# Get a key
GET key

# Get multiple keys
MGET key1 key2 key3

# Set multiple keys
MSET key1 "value1" key2 "value2"

# Increment/Decrement
INCR counter
INCRBY counter 5
DECR counter
DECRBY counter 5

# Append to string
APPEND key "more text"

# Get string length
STRLEN key

Keys

# Check if key exists
EXISTS key

# Delete key(s)
DEL key1 key2

# Delete key asynchronously
UNLINK key

# Find keys by pattern
KEYS pattern*

# Get key type
TYPE key

# Rename key
RENAME oldkey newkey

# Set expiration (seconds)
EXPIRE key 60

# Set expiration (milliseconds)
PEXPIRE key 60000

# Remove expiration
PERSIST key

# Get time to live (seconds)
TTL key

# Get time to live (milliseconds)
PTTL key

# Scan keys (cursor-based)
SCAN 0 MATCH pattern* COUNT 100

Lists

# Push to left/right
LPUSH list "value"
RPUSH list "value"

# Pop from left/right
LPOP list
RPOP list

# Get range (0 to -1 for all)
LRANGE list 0 -1

# Get by index
LINDEX list 0

# Get list length
LLEN list

# Set value at index
LSET list 0 "new value"

# Trim list
LTRIM list 0 99

# Blocking pop
BLPOP list 30
BRPOP list 30

Sets

# Add members
SADD set "member1" "member2"

# Get all members
SMEMBERS set

# Check if member exists
SISMEMBER set "member1"

# Get set size
SCARD set

# Remove member
SREM set "member1"

# Pop random member
SPOP set

# Get random member(s)
SRANDMEMBER set 2

# Set operations
SUNION set1 set2
SINTER set1 set2
SDIFF set1 set2

Sorted Sets

# Add members with scores
ZADD zset 1 "one" 2 "two" 3 "three"

# Get range by index (with scores)
ZRANGE zset 0 -1 WITHSCORES

# Get range by score
ZRANGEBYSCORE zset 1 3

# Get reverse range
ZREVRANGE zset 0 -1

# Get member score
ZSCORE zset "member"

# Get member rank
ZRANK zset "member"

# Increment score
ZINCRBY zset 5 "member"

# Get set size
ZCARD zset

# Count members in score range
ZCOUNT zset 1 5

# Remove members
ZREM zset "member1" "member2"

Hashes

# Set field
HSET hash field "value"

# Set multiple fields
HMSET hash field1 "value1" field2 "value2"

# Get field
HGET hash field

# Get multiple fields
HMGET hash field1 field2

# Get all fields and values
HGETALL hash

# Get all field names
HKEYS hash

# Get all values
HVALS hash

# Check if field exists
HEXISTS hash field

# Delete field
HDEL hash field

# Get number of fields
HLEN hash

# Increment field value
HINCRBY hash field 5

Pub/Sub

# Subscribe to channel(s)
SUBSCRIBE channel1 channel2

# Subscribe to pattern
PSUBSCRIBE news.*

# Publish message
PUBLISH channel "message"

# Unsubscribe
UNSUBSCRIBE channel
PUNSUBSCRIBE pattern

Transactions

# Start transaction
MULTI

# Queue commands
SET key1 "value1"
SET key2 "value2"

# Execute transaction
EXEC

# Discard transaction
DISCARD

# Watch keys for optimistic locking
WATCH key1 key2

Server

# Get server info
INFO

# Get specific section
INFO memory

# Get all config
CONFIG GET *

# Set config
CONFIG SET maxmemory 100mb

# Save database to disk
SAVE
BGSAVE

# Get database size
DBSIZE

# Clear current database
FLUSHDB

# Clear all databases
FLUSHALL

# Get slow queries
SLOWLOG GET 10

# Monitor commands in real-time
MONITOR

Lua Scripting

# Execute Lua script
EVAL "return redis.call('GET', KEYS[1])" 1 mykey

# Load script
SCRIPT LOAD "return redis.call('GET', KEYS[1])"

# Execute loaded script by SHA
EVALSHA sha1 1 mykey

# Check if script exists
SCRIPT EXISTS sha1

# Flush all scripts
SCRIPT FLUSH