Tools › Cheat sheets › Web Servers & Databases › Redis
🔴

Redis cheat sheet

Commands for redis-cli. Be careful on production: KEYS blocks the server on large datasets (use SCAN), and FLUSHALL erases everything.

Click any command to copy it.

Connect

CommandWhat it does
redis-cliConnect to localhost:6379.
redis-cli -h host -p 6379 -a passwordConnect to a remote server (passing a password on the command line is visible in process lists).
redis-cli -n 1Select database 1.
PINGCheck the server responds (expects PONG).

Strings and keys

CommandWhat it does
SET key valueSet a value.
GET keyGet a value.
DEL keyDelete a key.
EXISTS keyCheck whether a key exists.
EXPIRE key 60Expire a key in 60 seconds.
TTL keySeconds until a key expires (-1 none, -2 missing).
INCR counterAtomically increment a number.
TYPE keyShow the type of a key.

Other data types

CommandWhat it does
HSET user:1 name Alice age 30Set fields in a hash.
HGETALL user:1Get all fields of a hash.
LPUSH queue job1Push onto a list.
LRANGE queue 0 -1Read an entire list.
SADD tags a b cAdd to a set.
SMEMBERS tagsList a set's members.
ZADD scores 100 aliceAdd to a sorted set with a score.
ZRANGE scores 0 -1 WITHSCORESRead a sorted set with scores.

Find keys safely

CommandWhat it does
SCAN 0 MATCH user:* COUNT 100Iterate keys incrementally; repeat with the returned cursor.
redis-cli --scan --pattern 'user:*'Scan matching keys from the shell.
KEYS user:*Destructive: Lists all matching keys but blocks the server on big datasets. Avoid in production.
DBSIZENumber of keys in the current database.
redis-cli --bigkeysSample the keyspace and report the largest keys.

Server and diagnostics

CommandWhat it does
INFOServer statistics (add a section such as INFO memory).
INFO replicationReplication role and lag.
CONFIG GET maxmemoryRead a configuration value.
SLOWLOG GET 10The ten most recent slow commands.
CLIENT LISTConnected clients.
MONITORStream every command the server receives (heavy; use briefly).
BGSAVEWrite a snapshot to disk in the background.
FLUSHDBDestructive: Delete every key in the current database.
FLUSHALLDestructive: Delete every key in every database.

Frequently asked questions

Why shouldn't I use KEYS in production?

KEYS scans the whole keyspace in a single blocking operation. On large datasets that stalls every other client. SCAN returns results in small batches.

How do I see how much memory Redis uses?

Run INFO memory and look at used_memory_human and maxmemory_human.

More Web Servers & Databases cheat sheets

🚦 nginx🐘 PostgreSQL🐬 MySQL / MariaDBAll Web Servers & Databases cheat sheets →
Copied to clipboard