Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Connect

redis-cli
redis-cli -n 1
redis-cli -h 127.0.0.1 -p 6379

Without options, redis-cli connects to 127.0.0.1 port 6379 and database 0. -n picks another numbered database. If a password is set, log in inside the session:

AUTH STRONG_PASSWORD
AUTH app APP_PASSWORD

The first form is for requirepass, the second for an ACL user. Setting these up is covered in how to install and secure Redis.

Never Put the Password on the Command Line

redis-cli -a PASSWORD works, but the password stays in your shell history and is visible to other users in the process list while the client runs; redis-cli itself prints a warning about it. For scripts, read it into REDISCLI_AUTH from a file with mode 600:

REDISCLI_AUTH="$(cat ~/.redis-pass)" redis-cli --user app ping

Set, Get, and Delete

SET app:greeting "hello"
GET app:greeting
SET app:session:42 "data" EX 3600
EXISTS app:greeting
TYPE app:greeting
DEL app:greeting
UNLINK app:big-list

EX 3600 makes the key expire after an hour. UNLINK removes a key like DEL but frees large values in the background, so it does not pause the server.

Find Keys: SCAN, Not KEYS

KEYS pattern walks every key in the database in one go. Redis runs commands one at a time, so on a busy server with millions of keys KEYS * blocks every client until it finishes. Use SCAN, which returns a cursor and a small batch at a time:

SCAN 0 MATCH app:session:* COUNT 100

Repeat with the cursor it returns until the cursor is 0 again. From the shell, redis-cli does the looping for you:

redis-cli --scan --pattern 'app:session:*'

Pipe the result to wc -l to count matches. DBSIZE gives the total number of keys at once without scanning.

Expiry: TTL and EXPIRE

CommandWhat it does
TTL keySeconds left
EXPIRE key 600Expires in 600 s
PERSIST keyRemoves the expiry
PTTL keyMilliseconds left

TTL returns -1 for a key with no expiry and -2 for a key that does not exist.

Server State: INFO and --stat

INFO memory
INFO keyspace
INFO clients

INFO without a section prints everything. Look at used_memory_human against your maxmemory, the key count per database, and the number of connected clients. For a live view that updates every second:

redis-cli --stat

Find Big Keys

redis-cli --bigkeys
redis-cli --memkeys

Both scan the whole keyspace with SCAN and report the largest key of each type, by element count or by memory. Because they use SCAN, they do not block the server, but they add load while they run; -i 0.1 adds a pause after every 100 scans.

MONITOR and Its Cost

MONITOR prints every command the server receives, from every client, as it happens. It is the fastest way to see what an application really sends, but on a busy server it can cut throughput by half or more, and the output includes the keys and values your applications write. Current versions hide the arguments of AUTH. Run it for a few seconds only, stop it with Ctrl+C, and do not leave it running.

Delete Everything: FLUSHDB and FLUSHALL

Read this before you type either command. There is no confirmation and no undo. If persistence is on, the empty state is also written to disk, so restarting does not bring the data back. Check which server and which database you are connected to, and make sure a recent copy of dump.rdb exists elsewhere; see snapshot vs backup.

  • FLUSHDB: deletes all keys in the current database only.
  • FLUSHALL: deletes all keys in every database of the server.
  • ASYNC: added to either one, frees the memory in the background.

To remove only one group of keys, combine --scan with a pattern and delete what it finds, after you have read the list.

Was this answer helpful?
Loading...