Redis commands cheat sheet
Connecting
-
redis-cliOpens an interactive shell connected to a local Redis server at 127.0.0.1 on port 6379, which are the defaults. In the shell, you type Redis commands directly, and the prompt shows the server and the port you're connected to.
Example:
127.0.0.1:6379> -
redis-cli -h 203.0.113.5 -p 6380 PINGConnects to a specific server: the -h option for the host name or IP address, and the -p option for the port. When you add a command at the end of the line, redis-cli runs it and exits, without opening the interactive shell.
Example:
PONG -
redis-cli --askpassPrompts for the password and hides it as you type, so the password isn't saved in the command history. This is the recommended way to connect manually to a server that requires a password.
-
redis-cli -a 'StrongPass' PINGConnects with a password written on the command line. Caution: the password is saved in the shell's command history, and redis-cli shows a warning about it. In scripts, it's better to pass the password in the REDISCLI_AUTH environment variable, and when you use both, the -a option takes precedence.
-
redis-cli --user app_user --pass 'StrongPass'Connects as an ACL user, with a user name and a password. The --pass option is the same as -a, so here too the password is saved in the command history.
-
AUTH app_user StrongPassAuthenticates from inside the redis-cli shell, after you've already connected to the server. Without a user name, for example AUTH StrongPass, you authenticate as the default user.
Example:
OK -
redis-cli -n 2Connects directly to database number 2. A Redis server has several numbered databases, and the default is database 0.
-
redis-cli --tls --cacert ca.pem -h redis.example.com -p 6380Connects over an encrypted (TLS) connection. The --cacert option sets the CA certificate file the server is verified against, and when the server requires a client certificate, add the --cert option with the certificate and the --key option with the key.
-
redis-cli -u rediss://app_user:StrongPass@redis.example.com:6380/0Connects with a URI that holds all the connection details: redis:// for a regular connection and rediss:// for an encrypted connection. The user name, the password, and the database number are optional. Caution: a password written in the URI is saved in the command history.
Keys and basic commands
-
PINGChecks that the server is available and responding. A healthy reply is PONG.
Example:
PONG -
DBSIZEShows the number of keys in the current database.
Example:
(integer) 1520 -
SELECT 1Switches to database number 1 on the same connection. In the redis-cli shell, the database number appears in square brackets in the prompt.
Example:
127.0.0.1:6379[1]> -
KEYS user:*Caution: returns all the keys that match the pattern and goes over the whole database at once. On a large database, the command can hurt performance, so on a live server, use SCAN instead.
-
SCAN 0 MATCH user:* COUNT 100Goes over the keys gradually, without blocking the server. Each reply returns a cursor and some of the keys, and you send the command again with the cursor you received until it returns 0. COUNT is a hint for the number of keys checked in each call (the default is 10), and a key may appear more than once.
-
redis-cli --scan --pattern 'user:*'Lists all the keys that match the pattern. It uses SCAN, so it doesn't block the server. To count the keys, add | wc -l at the end.
-
TYPE user:1Shows the data type of the key, for example string, hash, list, set, or zset. When the key doesn't exist, the reply is none.
Example:
hash -
EXISTS user:1 user:2Checks whether keys exist and returns how many of the listed keys were found.
Example:
(integer) 1 -
TTL session:abcShows how many seconds are left until the key is deleted automatically. A reply of -1 means the key has no expiration, and a reply of -2 means the key doesn't exist. The PTTL command shows the time in milliseconds.
Example:
(integer) 3542 -
EXPIRE session:abc 3600Sets the key to be deleted automatically in 3600 seconds. You can add a condition: NX only when there's no expiration, XX only when there is one, GT only when the new expiration is longer, and LT only when it's shorter.
Example:
(integer) 1 -
PERSIST session:abcRemoves the expiration of the key, so it stays until you delete it.
Example:
(integer) 1 -
RENAME cart:temp cart:42Caution: renames the key, and when a key with the new name already exists, the command overwrites it and its previous value is deleted.
Example:
OK
Data types
-
SET greeting "Hello"Stores a text value (string) in a key and overwrites any existing value. The GET greeting command returns the value. When SET succeeds, it removes any expiration that was set on the key.
Example:
OK -
SET session:abc "data" EX 3600Stores a value with an expiration of 3600 seconds, in one command. The PX option sets the expiration in milliseconds, and KEEPTTL keeps the existing expiration of the key.
Example:
OK -
SET lock:job1 "worker-1" NX EX 30Stores the value only when the key doesn't exist (NX), with an expiration of 30 seconds. This is a simple way to create a lock, so that only one process runs a task. When the key already exists, the reply is nil and the value doesn't change.
Example:
OK -
INCR page:viewsIncrements a counter by 1 and returns the new value. When the key doesn't exist, it's created with the value 0 before the increment. INCRBY page:views 10 increments by any number you choose.
Example:
(integer) 1 -
HSET user:1 name "Dana" email "dana@example.com"Stores fields and values in a hash key, which suits an object such as a user or a product. You can store several fields in one command, and the reply is the number of new fields added.
Example:
(integer) 2 -
HGETALL user:1Returns all the fields and values of the hash. To get a single field, use the HGET user:1 name command.
-
LPUSH queue:jobs job1 job2Adds elements to the beginning of a list and creates the list when it doesn't exist. The RPUSH command adds to the end of the list, and together they're used to build queues.
Example:
(integer) 2 -
LRANGE queue:jobs 0 -1Shows the elements of the list from the first (0) to the last (-1). LRANGE queue:jobs 0 9 shows only the first ten elements.
-
SADD tags:post1 redis cacheAdds members to a set, where each member appears only once. A member that's already in the set isn't added again.
Example:
(integer) 2 -
SMEMBERS tags:post1Returns all the members of the set, in no fixed order. For a very large set, it's better to go over the members gradually with the SSCAN command.
-
ZADD leaderboard 100 dana 85 yossiAdds members to a sorted set, where each member has a score that the set is sorted by. Suitable for leaderboards and priority queues.
Example:
(integer) 2 -
ZRANGE leaderboard 0 -1 WITHSCORESShows all the members of the sorted set from the lowest score to the highest, together with the score of each member.
-
ZRANGE leaderboard 0 9 REV WITHSCORESShows the ten members with the highest scores, for example the top ten on a leaderboard.
Deleting data
-
DEL user:1 user:2Deletes keys and returns the number of keys deleted. A key that doesn't exist isn't counted. Deleting a very large key with DEL blocks the server until the memory is freed.
Example:
(integer) 2 -
UNLINK big:listDeletes keys like DEL, but the memory is freed in the background, so the command doesn't block the server. Especially suitable for deleting large keys.
Example:
(integer) 1 -
FLUSHDB ASYNCCaution: deletes all the keys in the current database, with no way to recover them. ASYNC runs the deletion in the background, without blocking the server.
Example:
OK -
FLUSHALL ASYNCCaution: deletes all the keys in every database on the server, and also deletes the snapshot file (RDB) on the disk. Before you run it, copy the backup file to another location and make sure you're connected to the right server.
Example:
OK
Monitoring and performance
-
INFOShows information about the server by section, such as server, clients, memory, persistence, stats, replication, and keyspace. You can request a single section, for example INFO clients.
-
INFO memoryShows memory usage. The used_memory_human field shows the memory in use in readable units, and the maxmemory_human field shows the configured limit.
Example:
used_memory_human:1.25M -
INFO statsShows general statistics. The keyspace_hits and keyspace_misses fields show how many lookups found the key and how many didn't, and evicted_keys shows how many keys were removed because of the memory limit.
-
INFO keyspaceShows, for each database, the number of keys and the number of keys with an expiration set.
-
redis-cli --statPrints a new line every second, with the number of keys, memory usage, the number of connections, and the number of requests. Stop it with Ctrl+C.
-
redis-cli --bigkeysScans all the keys and shows the keys with the most elements in each data type. The scan uses SCAN, so it's also suitable for a live server. The -i 0.1 option slows the scan down and reduces the load.
-
redis-cli --memkeysScans the keys and shows the keys that use the most memory. A convenient way to find out what's using the memory of the server.
-
MEMORY USAGE user:1Shows how much memory, in bytes, a specific key uses, including its value.
Example:
(integer) 72 -
SLOWLOG GET 10Shows the ten most recent slow commands, with the execution time of each one in microseconds and the command itself. SLOWLOG RESET clears the list.
-
CLIENT LISTLists all the connections to the server: the ID (id), the address (addr), the connection age (age), the idle time (idle), and the last command (cmd). Useful for finding an application that opens too many connections.
-
CLIENT KILL ID 12Caution: closes the connection with the ID 12, as shown in CLIENT LIST. The application that used the connection gets an error and has to reconnect.
Example:
(integer) 1 -
MONITORCaution: shows every command the server receives, in real time. The command can reduce the throughput of the server by more than 50%, so on a live server, run it only briefly and stop it with Ctrl+C.
-
redis-cli --latencySends PING continuously and shows the minimum, maximum, and average response time, in milliseconds. The --latency-history option starts a new measurement every 15 seconds.
Example:
min: 0, max: 1, avg: 0.19 (427 samples) -
CONFIG SET latency-monitor-threshold 100Turns on latency monitoring and records every event that takes 100 milliseconds or more. Monitoring is off by default (the value 0).
Example:
OK -
LATENCY DOCTORShows a readable report on the recorded latency events, with explanations and recommendations. The report is based on latency monitoring, so turn it on first.
Memory and persistence
-
CONFIG GET maxmemory*Shows the settings whose names start with maxmemory, such as the memory limit and the eviction policy. CONFIG GET accepts a pattern with an asterisk and returns several settings at once.
-
CONFIG SET maxmemory 256mbLimits the memory the data can use. The value 0 means no limit, which is the default on 64-bit systems. When you also save data to disk, set a limit that leaves some free memory on the server.
Example:
OK -
CONFIG SET maxmemory-policy allkeys-lruSets what happens when the memory limit is reached. allkeys-lru removes the keys that haven't been used for the longest time and suits a cache. noeviction removes nothing and returns an error to commands that add data, and a policy that starts with volatile removes only keys with an expiration.
Example:
OK -
CONFIG GET saveShows the rules for saving a snapshot of the data to disk (RDB). For example, the rule save 60 1000 saves a snapshot when at least 1,000 keys changed within 60 seconds.
-
CONFIG SET appendonly yesTurns on the AOF file, which logs every write command and lets you restore the data after a restart. By default, it's written to disk once per second. To keep your data safe, use both RDB and AOF.
Example:
OK -
BGSAVESaves a snapshot of all the data to an RDB file in the background, and the server keeps answering requests while it saves. The file is suitable for backups and for moving the data to another server.
Example:
Background saving started -
SAVECaution: saves the data to disk while the server is blocked, and all clients wait until it finishes. On a live server, use BGSAVE instead.
Example:
OK -
LASTSAVEReturns the time of the last successful save to disk, as a Unix timestamp in seconds. Compare the value before and after BGSAVE to confirm that the save finished.
Example:
(integer) 1764400000 -
BGREWRITEAOFRewrites the AOF file in the background as a smaller version that holds only what's needed to restore the current state. When the rewrite fails, the existing file stays unchanged.
-
CONFIG REWRITEWrites the settings you changed with CONFIG SET to the redis.conf file. Without this command, the changes are kept in memory only and are lost when the service restarts. The command fails when the server was started without a configuration file.
Example:
OK -
redis-cli --rdb /backup/dump.rdbDownloads a current RDB file from the server to the computer you run redis-cli on, as a backup. In scripts and cron jobs, check that the command finished successfully (exit code 0).
Example:
Transfer finished with success.
Security and users
-
ACL WHOAMIShows the user name the current connection is authenticated as. A new connection is authenticated as the default user.
Example:
default -
ACL LISTLists all the users and the rules of each one: whether it's enabled, which keys it may access, and which commands it may run.
Example:
user default on nopass ~* &* +@all -
ACL SETUSER app_user on >StrongPass ~app:* +@all -@dangerousCreates or updates a user: on enables it, >StrongPass adds a password, ~app:* allows only keys that start with app:, and +@all -@dangerous allow all the commands except the dangerous ones. Caution: the password in the command may be saved in the command history.
Example:
OK -
ACL DELUSER app_userDeletes a user, and open connections authenticated as that user are closed. You can't delete the default user.
Example:
(integer) 1 -
ACL SAVESaves the users and permissions to the ACL file, so they're kept after a restart. The command works only when an external ACL file is set with the aclfile setting in the redis.conf file.
Example:
OK -
requirepass StrongPassA line in the redis.conf file that sets a password for connecting to the server. This is the older method, and today it's recommended to manage users and permissions with ACL.
-
bind 127.0.0.1A line in the redis.conf file that makes the server listen only for connections from the server itself. When an application on another server needs access, listen on an additional address and restrict the port in the firewall to authorized addresses only.
-
protected-mode yesProtected Mode: when the server listens on all addresses and no password is set, it answers only connections from the server itself. It's recommended to leave it on.
Redis service
-
sudo systemctl status redis-serverShows whether the Redis service is running. In an installation from APT packages, for example on Debian and Ubuntu, the service name is redis-server.
-
sudo systemctl status redisShows whether the Redis service is running in an installation from RPM packages, for example on AlmaLinux and Rocky Linux, where the service name is redis. In this installation, the service doesn't start automatically, so enable and start it with sudo systemctl enable redis and sudo systemctl start redis.
-
sudo systemctl restart redis-serverRestarts the service, and all the connections are closed. When saving to disk (RDB or AOF) isn't configured, the data in memory is lost on restart.
-
redis-cli INFO server | grep redis_versionShows the version of the Redis server. The redis-cli --version command shows the version of the redis-cli tool itself.
No matching commands.
Redis is a database that keeps its data in memory, and it's used mainly as a cache, for storing sessions, for queues, and for counters. This page includes three kinds of commands: options of the redis-cli tool, which you run from the server's command line; commands of Redis itself, such as SET and SCAN, which you type in the redis-cli shell or add at the end of the command line; and configuration lines in the redis.conf file, such as bind and requirepass.
A Redis server that's open to the internet without a password can be accessed by anyone, so restrict listening to the local address or to authorized addresses in the firewall, and set a password or ACL users. Before a command that deletes data, such as FLUSHALL, create a backup with the BGSAVE command, confirm with the LASTSAVE command that the save finished, and copy the backup file to another location.
In recent versions, Redis Open Source is offered under your choice of three licenses: RSALv2, SSPLv1, or AGPLv3. End-of-support dates for each version are on the Redis lifecycle page.
On a Linux VPS, you connect over SSH with full root access, install Redis, and manage it with every command on this page: secure connections, memory and performance monitoring, saving data to disk, and user permissions.
Redis commands FAQ
How do I connect to Redis from the command line with a password?
The recommended way is redis-cli --askpass, which prompts for the password and hides it as you type, so the password isn't saved in the command history. For a remote server, add the -h option with the server address and the -p option with the port, and for an ACL user, add --user with the user name. In scripts, you can pass the password in the REDISCLI_AUTH environment variable.
What's the difference between KEYS and SCAN?
The KEYS command goes over all the keys at once, and on a large database it can slow down the server and the applications that work with it. The SCAN command goes over the keys gradually, in several short calls, so it suits a live server. From the command line, the convenient way is redis-cli --scan --pattern 'user:*', which runs the whole scan with SCAN.
How do I check how much memory Redis uses and what's using it?
The INFO memory command shows the memory in use and the configured limit. To find the keys that use the most memory, run redis-cli --memkeys, and for a specific key, use the MEMORY USAGE command. When Redis serves as a cache, limit its memory with the maxmemory setting and choose an eviction policy such as allkeys-lru.
How do I keep Redis data after a restart?
Redis has two ways to save data to disk: RDB, which saves a snapshot of all the data according to the rules in the save setting, and AOF, which logs every write command and is turned on with the appendonly yes setting. To keep your data safe, use both together. Changes made with CONFIG SET are saved to the redis.conf file only after the CONFIG REWRITE command.
How do I create a Redis user with limited permissions?
Create the user with the ACL SETUSER command, with a password, a pattern of the keys it may access, and command categories, for example ACL SETUSER app_user on >StrongPass ~app:* +@all -@dangerous. This way, the application works only with its own keys and can't run dangerous commands such as FLUSHALL. To keep the users after a restart, set up an ACL file and save it with the ACL SAVE command.