Redis basics.

Install / connect

docker run -d --name redis -p 6379:6379 redis:7-alpine
redis-cli
redis-cli -h host -p 6379 -a password
redis-cli --tls

Strings

SET key value
GET key
SETEX key 60 value     # with TTL
SETNX key value        # only if not exists
MSET a 1 b 2 c 3
MGET a b c
INCR counter
INCRBY counter 5
DECR counter
APPEND log "line\n"
STRLEN key
GETRANGE key 0 4
SETRANGE key 5 "x"

Keys

KEYS pattern*          # AVOID in prod (blocking)
SCAN 0 MATCH user:* COUNT 100
EXISTS key
DEL key key2
UNLINK key             # async delete
TYPE key
RENAME old new
RENAMENX old new       # only if new doesn't exist
DBSIZE
FLUSHDB                # clear current DB
FLUSHALL               # all DBs

TTL

EXPIRE key 60          # seconds
PEXPIRE key 60000      # milliseconds
EXPIREAT key 1700000000  # unix timestamp
TTL key                # remaining
PERSIST key            # remove TTL

Selecting DB

SELECT 0..15           # default 16 DBs

Most use just db 0. Multiple DBs are deprecated pattern; use key prefixes instead.

Pub/Sub

SUBSCRIBE channel
PUBLISH channel message
PSUBSCRIBE news.*      # pattern
UNSUBSCRIBE

For modern use: prefer Streams (more reliable).

Auth

# /etc/redis/redis.conf
requirepass yoursecret
AUTH password
AUTH username password   # ACL

ACL (users)

ACL WHOAMI
ACL LIST
ACL SETUSER alice on >password ~app:* +@read +@write
ACL DELUSER alice

Granular permissions.

INFO

INFO
INFO server
INFO clients
INFO memory
INFO stats
INFO replication
INFO persistence

Monitor

MONITOR                # see all commands (debug only)

Heavy! Don’t use in prod.

Slow log

SLOWLOG GET 10
SLOWLOG RESET
CONFIG SET slowlog-log-slower-than 10000  # microseconds

CLIENT

CLIENT LIST
CLIENT KILL ADDR ip:port
CLIENT GETNAME
CLIENT SETNAME myapp

DEBUG

DEBUG SLEEP 5          # block for 5s
DEBUG OBJECT key       # internal info
DEBUG SET-ACTIVE-EXPIRE 0

Memory

MEMORY USAGE key
MEMORY STATS
MEMORY DOCTOR
CONFIG SET maxmemory 1gb
CONFIG SET maxmemory-policy allkeys-lru

Eviction policies

  • noeviction: error on OOM (default).
  • allkeys-lru: evict LRU across all keys.
  • volatile-lru: only keys with TTL.
  • allkeys-lfu: LFU across all.
  • volatile-ttl: shortest TTL first.
  • volatile-random / allkeys-random.

Save / load

SAVE                   # blocks
BGSAVE                 # async snapshot (RDB)
BGREWRITEAOF           # rewrite AOF
LASTSAVE

CLI tips

redis-cli -n 1                  # db 1
redis-cli --scan --pattern "user:*"
redis-cli --bigkeys             # find big keys
redis-cli --memkeys             # memory usage
redis-cli --hotkeys             # most accessed (with LFU policy)
redis-cli --latency             # ping latency
redis-cli --latency-history
redis-cli MONITOR | grep -i error

Pipelining

Send many commands in one batch:

pipe = redis.pipeline()
for k in keys:
    pipe.get(k)
results = pipe.execute()

Much faster than N round-trips.

Transactions

MULTI
SET a 1
SET b 2
EXEC
with redis.pipeline(transaction=True) as pipe:
    pipe.set("a", 1)
    pipe.set("b", 2)
    pipe.execute()

WATCH for optimistic locking.

Key naming

Use : as separator:

user:1:profile
user:1:sessions
post:42:comments

Lowercase, consistent.

Common mistakes

  • KEYS * in prod blocks server.
  • FLUSHALL accidentally.
  • No maxmemory set → uses all RAM.
  • Storing big blobs (>100KB) in Redis (use S3 / DB).
  • No TTL → cache grows forever.

Read this next

If you want my Redis setup, it’s at rajpoot.dev .


Building something AI-, backend-, or data-heavy and want a second pair of eyes? I do consulting and freelance work — see my projects and ways to reach me at rajpoot.dev .