Redis Cheatsheet

Keys, strings, hashes, lists, sets, pub/sub & configuration

Database
Contents
🔗

Connection

redis-cli                       # connect local
redis-cli -h host -p 6379 -a pass
redis-cli --url redis://user:pass@host:6379

PING                            # → PONG
SELECT 1                        # switch database
DBSIZE                          # number of keys
INFO                            # server info
📝

Strings

SET key "value"
GET key
SETNX key "value"               # set if not exists
SETEX key 3600 "value"          # set with TTL (seconds)
MSET k1 "v1" k2 "v2"           # multi set
MGET k1 k2                      # multi get
INCR counter                    # increment by 1
INCRBY counter 5                # increment by 5
DECR counter
APPEND key " more"               # append string
STRLEN key                      # string length
🔑

Key Operations

KEYS *                          # all keys (avoid in prod!)
KEYS user:*                     # pattern match
SCAN 0 MATCH user:* COUNT 100   # safe iteration
EXISTS key                      # check exists
DEL key                         # delete
UNLINK key                      # async delete
TYPE key                        # get type
RENAME key newkey
EXPIRE key 3600                 # set TTL
TTL key                         # remaining TTL
PERSIST key                     # remove TTL
RANDOMKEY                       # random key
📋

Hashes

HSET user:1 name "Alice" age 25
HGET user:1 name
HGETALL user:1                  # all fields
HMSET user:1 name "Alice" age 25
HMGET user:1 name age
HDEL user:1 age
HEXISTS user:1 name
HKEYS user:1                    # all field names
HVALS user:1                    # all values
HLEN user:1                     # number of fields
HINCRBY user:1 age 1
📃

Lists

LPUSH mylist "a"                # push left
RPUSH mylist "b"                # push right
LPOP mylist                     # pop left
RPOP mylist                     # pop right
LRANGE mylist 0 -1              # all elements
LRANGE mylist 0 4               # first 5
LLEN mylist                     # length
LINDEX mylist 0                 # get by index
LSET mylist 0 "new"             # set by index
LTRIM mylist 0 99               # keep first 100
BLPOP mylist 30                 # blocking pop (30s timeout)
🎯

Sets

SADD myset "a" "b" "c"
SMEMBERS myset                  # all members
SISMEMBER myset "a"             # check membership
SCARD myset                     # count
SREM myset "b"                  # remove
SPOP myset                      # random remove
SRANDMEMBER myset 2             # random 2 members
SUNION set1 set2                # union
SINTER set1 set2                # intersection
SDIFF set1 set2                 # difference
📊

Sorted Sets

ZADD leaders 100 "Alice" 85 "Bob"
ZRANGE leaders 0 -1             # by rank (asc)
ZREVRANGE leaders 0 2           # top 3
ZRANGE leaders 0 -1 WITHSCORES
ZSCORE leaders "Alice"          # get score
ZRANK leaders "Alice"           # get rank
ZINCRBY leaders 10 "Alice"      # increment
ZCARD leaders                   # count
ZRANGEBYSCORE leaders 80 100    # by score range
ZREM leaders "Bob"
📡

Pub/Sub

# Subscribe
SUBSCRIBE channel1 channel2
PSUBSCRIBE news:*               # pattern subscribe

# Publish (from another client)
PUBLISH channel1 "Hello!"

# Unsubscribe
UNSUBSCRIBE channel1
🔧

Administration

FLUSHDB                         # clear current DB
FLUSHALL                        # clear all DBs
SAVE                            # sync save to disk
BGSAVE                          # async save
CONFIG SET maxmemory 256mb
CONFIG SET maxmemory-policy allkeys-lru
SLOWLOG GET 10                  # slow queries
CLIENT LIST                     # connected clients
MONITOR                         # real-time commands