Cheatsheet: Redis

Last updated 2026-09-28

redis-cli Basics

Connect to a local server

redis-cli

Connect to a remote host/port with auth

redis-cli -h host -p 6379 -a password

Run one command non-interactively

redis-cli GET mykey

Ping the server

PING

List all keys matching a pattern (avoid in production; use SCAN)

KEYS user:*

Iterate keys without blocking the server

SCAN 0 MATCH user:* COUNT 100

Delete a key / check if a key exists

DEL mykey / EXISTS mykey

Switch logical database (0-15 by default)

SELECT 1

Flush the current database (deletes everything in it)

FLUSHDB

Strings & Expiration

Set / get a string value

SET mykey "value"
GET mykey

Set only if the key doesn't exist, with a 30s expiration (atomic lock pattern)

SET lock:foo 1 NX EX 30

Increment / decrement a numeric value

INCR counter
DECR counter

Increment by a specific amount

INCRBY counter 5

Set an expiration (seconds) on an existing key

EXPIRE mykey 60

Check remaining time-to-live

TTL mykey

Remove expiration, making a key persist forever

PERSIST mykey

Set multiple keys at once

MSET key1 val1 key2 val2

Hashes

Set one or more fields on a hash

HSET user:1 name Alice age 30

Get one field / all fields

HGET user:1 name
HGETALL user:1

Check if a field exists / delete a field

HEXISTS user:1 age
HDEL user:1 age

Increment a numeric field

HINCRBY user:1 age 1

List all field names / all values

HKEYS user:1
HVALS user:1

Lists, Sets & Sorted Sets

Push to the left/right of a list

LPUSH mylist a
RPUSH mylist b

Read a range of a list (0 -1 = whole list)

LRANGE mylist 0 -1

Pop from the left/right (blocking variants: BLPOP/BRPOP)

LPOP mylist
RPOP mylist

Add members to a set / check membership

SADD myset a b c
SISMEMBER myset a

Set intersection / union / difference

SINTER set1 set2
SUNION set1 set2
SDIFF set1 set2

Add a member with a score to a sorted set

ZADD leaderboard 100 alice

Read a sorted set by rank, or by score range

ZRANGE leaderboard 0 -1 WITHSCORES
ZRANGEBYSCORE leaderboard 50 150

Get a member's rank or score

ZRANK leaderboard alice
ZSCORE leaderboard alice

Pub/Sub

Subscribe to a channel (blocks in the CLI)

SUBSCRIBE news

Publish a message to a channel

PUBLISH news "hello subscribers"

Subscribe to channels matching a glob pattern

PSUBSCRIBE news.*

Unsubscribe from a channel

UNSUBSCRIBE news

Persistence, Replication & Info

Trigger a synchronous RDB snapshot (blocks the server)

SAVE

Trigger a background (non-blocking) RDB snapshot

BGSAVE

Rewrite the AOF file to compact it

BGREWRITEAOF

Make this instance a replica of another

REPLICAOF host 6379

Promote a replica back to primary

REPLICAOF NO ONE

Show server info (memory, replication, persistence, stats)

INFO

Show and reload live config values

CONFIG GET maxmemory
CONFIG SET maxmemory 256mb

FAQ