Skip to content

Redis Cheat Sheet

Redis is an in-memory data structure store used as a database, cache, and message broker. This cheatsheet provides an advanced, structured reference of Redis commands and behaviors.

Strings

Redis strings store binary-safe values up to 512MB and support atomic operations.

SET key value
GET key
APPEND key value
STRLEN key
INCR key
INCRBY key increment
DECR key
GETSET key value
SET counter 10
INCR counter
GET counter
# 11

Bit Operations

Bit-level operations on string values.

SETBIT key offset value
GETBIT key offset
BITCOUNT key [start end]
BITPOS key bit [start end]
BITOP AND|OR|XOR|NOT destkey srckey [srckey ...]
SETBIT flags 7 1
BITCOUNT flags
# 1

Key Management

Commands that operate on keys independently of value type.

DEL key [key ...]
EXISTS key [key ...]
TYPE key
RENAME key newkey
RENAMENX key newkey
SET a 1
RENAME a b
TYPE b
# string

Key Scanning

Incremental iteration over keys without blocking the server.

SCAN cursor [MATCH pattern] [COUNT count]
SCAN 0 MATCH user:* COUNT 100

Expiration and TTL

Key expiration controls lifecycle and memory eviction.

EXPIRE key seconds
PEXPIRE key milliseconds
EXPIREAT key timestamp
TTL key
PTTL key
PERSIST key
SET session abc
EXPIRE session 60
TTL session
# 58

Hashes

Hashes map fields to values and are optimized for small objects.

HSET key field value
HGET key field
HMSET key field value [field value ...]
HMGET key field [field ...]
HGETALL key
HSET user:1 name Alice age 30
HGET user:1 name
# Alice

Hash Counters

Atomic numeric operations on hash fields.

HINCRBY key field increment
HINCRBYFLOAT key field increment
HSTRLEN key field
HINCRBY user:1 age 1
# 31

Lists

Ordered collections optimized for queue and stack patterns.

LPUSH key value [value ...]
RPUSH key value [value ...]
LPOP key
RPOP key
LRANGE key start stop
LLEN key
LPUSH queue job1 job2
RPOP queue
# job1

Blocking List Operations

Used for producer-consumer patterns.

BLPOP key [key ...] timeout
BRPOP key [key ...] timeout
BRPOPLPUSH source destination timeout
BRPOP queue 0

Sets

Unordered collections of unique elements.

SADD key member [member ...]
SREM key member [member ...]
SMEMBERS key
SCARD key
SISMEMBER key member
SADD tags redis cache nosql
SMEMBERS tags

Set Algebra

Mathematical operations across sets.

SUNION key [key ...]
SINTER key [key ...]
SDIFF key [key ...]
SUNIONSTORE dest key [key ...]
SINTER team:a team:b

Sorted Sets

Sets ordered by floating-point score.

ZADD key score member [score member ...]
ZRANGE key start stop [WITHSCORES]
ZREVRANGE key start stop [WITHSCORES]
ZRANK key member
ZINCRBY key increment member
ZADD leaderboard 100 user1
ZINCRBY leaderboard 50 user1
ZRANGE leaderboard 0 -1 WITHSCORES

Keyspace Databases

Redis supports multiple logical databases.

SELECT index
MOVE key db
FLUSHDB
FLUSHALL
SELECT 1
FLUSHDB

Transactions

Atomic execution of command groups without rollback.

MULTI
EXEC
DISCARD
WATCH key [key ...]
UNWATCH
WATCH balance
MULTI
DECR balance
EXEC

Pub/Sub

Message broadcasting mechanism without persistence.

PUBLISH channel message
SUBSCRIBE channel [channel ...]
UNSUBSCRIBE [channel ...]
PSUBSCRIBE pattern
SUBSCRIBE events

Lua Scripting

Server-side scripting for complex atomic operations.

EVAL script numkeys key [key ...] arg [arg ...]
EVALSHA sha1 numkeys key [key ...] arg [arg ...]
SCRIPT LOAD script
SCRIPT EXISTS sha1
SCRIPT FLUSH
EVAL "return redis.call('INCR', KEYS[1])" 1 counter

Persistence (RDB)

Point-in-time snapshots of the dataset.

SAVE
BGSAVE
LASTSAVE
BGSAVE

Append Only File (AOF)

Command-based persistence mechanism.

BGREWRITEAOF
BGREWRITEAOF

Replication

Primary-replica replication configuration.

ROLE
REPLICAOF host port
REPLICAOF NO ONE
ROLE

Client and Server

Inspect and manage connected clients and server state.

AUTH password
PING
INFO
CLIENT LIST
CLIENT KILL
INFO memory

Performance and Debugging

Low-level inspection and profiling tools.

MONITOR
SLOWLOG GET
SLOWLOG LEN
SLOWLOG RESET
MEMORY USAGE key
MEMORY STATS
SLOWLOG GET 5