Redis Cheat Sheet

Keys, hashes, lists, expiry, pub/sub, and the server ops commands for the fastest structure store.

Databases5 sections · 46 commands
⌘K

Strings

10
SET key value

Set a value

SET key value EX 60

Set with 60s expiry

SETNX key value

Set only if it doesn't exist

GET key

Read a value

INCR key

Increment a counter

INCRBY key 50

Add a fixed amount

GETSET key newvalue

Set and return old value

MSET a 1 b 2

Set multiple keys at once

TTL key

Time to live in seconds

EXPIRE key 300

Set an expiry

Hashes & Lists

10
HSET user:1 name alice role admin

Set hash fields

HGET user:1 name

Read one field

HGETALL user:1

All fields

HINCRBY user:1 login_count 1

Increment a field

LPUSH queue job1

Push to the head of a list

RPUSH queue job2

Push to the tail

LPOP / RPOP queue

Pop head or tail

LRANGE queue 0 -1

All list items

BLPOP queue 5

Blocking pop with 5s timeout

LLEN queue

List length

Sets & Sorted Sets

9
SADD trackers user:1

Add to a set

SMEMBERS trackers

All set members

SISMEMBER trackers user:1

Membership check

SINTER online active

Intersection

ZADD scores 95 alice

Add with a score

ZRANGE scores 0 4 WITHSCORES

Top 5 by score

ZREVRANGE scores 0 4

Top 5 descending

ZRANK scores alice

Rank of a member

ZINCRBY scores 5 alice

Bump a score

Pub/Sub & Keyspace

7
SUBSCRIBE channel

Listen to a channel

PUBLISH channel message

Send a message

KEYS 'user:*'

Find keys by pattern (avoid on big DBs)

SCAN 0 MATCH 'user:*' COUNT 100

Cursor-based scan (safe)

DEL key

Delete a key

EXISTS key

Check a key exists

TYPE key

Data type of a key

Server Ops

10
INFO

Server stats (sections per arg)

INFO memory

Memory usage

CLIENT LIST

Connected clients

CONFIG GET maxmemory

Read a config

CONFIG SET maxmemory 512mb

Set a runtime config

DBSIZE

Count of keys

FLUSHDB / FLUSHALL

Clear the database / all

SAVE / BGSAVE

Snapshot now / background

redis-cli --no-auth-warning

Suppress the password warning

redis-cli -u redis://user:pass@host:6379

Connect with URI