Common Commands

Valkey supports 200+ commands. This page covers the most commonly used core commands, organized by data type.

String

The most basic key-value operations, also the foundation for other advanced features:

valkeyCopy
# Set key-value
SET user:name "John"
SET session:token "abc123" EX 3600  # Expires in 1 hour

# Get value
GET user:name

# Increment / Decrement
INCR page:views        # Atomic increment
INCRBY score 10        # Increment by specific value

# Batch operations
MSET key1 "v1" key2 "v2" key3 "v3"
MGET key1 key2 key3

# Set only if key does not exist
SETNX lock:resource "owner1"

Hash

Ideal for storing objects, organizing multiple fields under a single key:

valkeyCopy
# Set hash fields
HSET user:1001 name "John" age 30 email "[email protected]"

# Get single / multiple fields
HGET user:1001 name
HMGET user:1001 name age

# Get entire hash
HGETALL user:1001

# Increment field
HINCRBY user:1001 login_count 1

List

Ordered collection of elements supporting operations on both ends. Commonly used for message queues and timelines:

valkeyCopy
# Push to right end
RPUSH tasks "send-email" "generate-report" "clear-cache"

# Pop from left end (FIFO queue)
LPOP tasks

# Blocking pop (wait for new elements)
BLPOP tasks 30  # Wait up to 30 seconds

# Get range
LRANGE tasks 0 -1  # Get all
LLEN tasks          # List length

Set & Sorted Set

valkeyCopy
# --- Set: unordered, unique ---
SADD tags:article:1 "tech" "valkey" "database"
SMEMBERS tags:article:1
SISMEMBER tags:article:1 "tech"

# Intersection / Union
SINTER tags:article:1 tags:article:2
SUNION tags:article:1 tags:article:2

# --- Sorted Set: scored ordered collection ---
ZADD leaderboard 100 "PlayerA" 85 "PlayerB" 92 "PlayerC"
ZRANGE leaderboard 0 -1 WITHSCORES  # Low to high
ZREVRANGE leaderboard 0 2 WITHSCORES # Top 3
ZRANK leaderboard "PlayerB"            # Rank

Key Management

CommandDescriptionExample
DEL keyDelete a keyDEL user:1001
EXISTS keyCheck if key existsEXISTS user:1001
EXPIRE key sSet expiry time (seconds)EXPIRE session 3600
TTL keyCheck remaining time-to-liveTTL session
TYPE keyCheck value type of keyTYPE user:1001
KEYS patternMatch keys by pattern (use with caution)KEYS user:*
SCAN cursorIncrementally iterate keys (recommended)SCAN 0 MATCH user:* COUNT 100
RENAMERename a keyRENAME old_key new_key

Production Warning: Avoid using the KEYS command to scan large numbers of keys — it blocks the server. Use SCAN instead, which iterates incrementally via cursor without blocking.

Stream

Stream data structure introduced in Valkey 5.0, ideal for message queues and event sourcing:

valkeyCopy
# Append message
XADD events * user "John" action "login" ip "192.168.1.1"

# Read messages
XRANGE events - + COUNT 10

# Consumer groups
XGROUP CREATE events mygroup 0
XREADGROUP GROUP mygroup consumer1 COUNT 1 BLOCK 5000 STREAMS events >