Redis Cheat Sheet
Connection and keys
// connect with: redis-cli -h host -p 6379
PING
SELECT 1
KEYS user:*
SCAN 0 MATCH user:* COUNT 100
EXISTS k
DEL k
TYPE k
RENAME k k2
FLUSHDB
Strings and TTL
SET k v
SET k v EX 60 NX
GET k
MSET a 1 b 2
MGET a b
INCR n
INCRBY n 5
DECR n
EXPIRE k 60
TTL k
PERSIST k
Hashes and lists
HSET user:1 name Asha xp 10
HGET user:1 name
HGETALL user:1
HINCRBY user:1 xp 5
HDEL user:1 xp
LPUSH q a
RPUSH q b
LPOP q
LRANGE q 0 -1
LTRIM q 0 9
BLPOP q 5
Sets and sorted sets
SADD s a b
SMEMBERS s
SISMEMBER s a
SCARD s
SINTER s1 s2
SUNION s1 s2
ZADD lb 100 asha
ZINCRBY lb 5 asha
ZREVRANGE lb 0 9 WITHSCORES
ZRANK lb asha
ZSCORE lb asha
Pub/Sub, streams, transactions
PUBLISH ch msg
SUBSCRIBE ch
XADD st * field value
XRANGE st - +
MULTI
EXEC
WATCH k
Node.js (redis package)
const redis = createClient({ url: process.env.REDIS_URL }); await redis.connect();
await redis.set("k", "v", { EX: 60 }); await redis.get("k"); await redis.incr("n");
await redis.hSet("user:1", { name: "Asha" }); await redis.zAdd("lb", { score: 10, value: "asha" });
Full Redis documentation