The Redis cheat sheet includes basic syntax and methods to help you using Redis. Redis is an open-source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. Output:!Redis Enterpise Cluster( /images/rs/rpstack.png ) shows: To make an image appear on the next line in a list: Write your instruction. Add 2 spaces at the end of the line and put the image on the next line with a tab indentation.
This is a super light weight reference for the interactive mode of the Redis CLI tool. I really just want to use it to explore what data is in Redis, so the emphasis is on getting in and listing data rather than setting values.
Documentation for all the Redis commands is at https://redis.io/commands
Starting the CLI in interactive mode
Connect to a locally running instance of Redis and start the interactive REPL:
Configure hints:
Get help for a command:
Query Config
List all sorts of useful config:
Select a database (key space)
Show the max number of databases this server is configured to host:
List the available databases:
Select the redis database to use:
Find out what’s in a key space
List all the keys in the currently selected keyspace:
Batch list keys. Pass 0 the first time, then the cursor number given by Redis next time:
Query key value type
Find out what data structure a key holds:
String types
Getting, setting and removing keys holding string values:
Set types
List all members of a key holding a set data structure:
Hash types
List all keys and values of a hash data structure:
List types
List all elements of a list data structure:
-1
refers to the last element of the list.
Expiring keys
- 1Basic Redis commands - Cheat Sheet
Basic Redis commands - Cheat Sheet
When you encounter a Redis instance and you quickly want to learn about the setup you just need a few simple commands to peak into the setup. Of course it doesn't hurt to look at the official full command documentation, but below is a listing just for sysadmins.Accessing Redis
First thing to know is that you can use 'telnet' (usually on default port 6397)
or the Redis CLI client
to connect to Redis. The advantage of redis-cli is that you have a help interface and command line history.
Scripting Redis Commands
For scripting just pass commands to 'redis-cli'. For example:
Server Statistics
The statistics command is 'INFO' and will give you an output as following:
Changing Runtime Configuration
The command
gives you a list of all active configuration variables you can change. The output might look like this:
Note that keys and values are alternating and you can change each key by issuing a 'CONFIG SET' command like:
Such a change will be effective instantly. When changing values consider also updating the redis configuration file.
Multiple Databases
Redis has a concept of separated namespaces called 'databases'. You can select the database number you want to use with 'SELECT'. By default the database with index 0 is used. So issuing
switches to the second database. Note how the prompt changed and now has a '[1]' to indicate the database selection.
To find out how many databases there are you might want to run redis-cli from the shell:
Dropping Databases
To drop the currently selected database run
to drop all databases at once run
Checking for Replication
To see if the instance is a replication slave or master issue
and watch for the 'role' line which shows either 'master' or 'slave'.
Starting with version 2.8 the 'INFO' command also gives you per slave replication status looking like this
Enabling Replication
If you quickly need to set up replication just issue
on a machine that you want to become slave of the given IP. It will immediately get values from the master. Note that this instance will still be writable. If you want it to be read-only change the redis config file (only available in most recent version, e.g. not on Debian).
To revert the slave setting run
Dump Database Backup
As Redis allows RDB database dumps in background, you can issue a dump at any time. Just run:
When running this command Redis will fork and the new process will dump into the 'dbfilename' configured in the Redis configuration without the original process being blocked. Of course the fork itself might cause an interruption.
Use 'LASTSAVE' to check when the dump file was last updated. For a simple backup solution just backup the dump file.
If you need a synchronous save run 'SAVE' instead of 'BGSAVE'.
Listing Connections
Starting with version 2.4 you can list connections with
and you can terminate connections with
Monitoring Traffic
The propably most useful command compared to memcached where you need to trace network traffic is the 'MONITOR' command which will dump incoming commands in real time.
Checking for Keys
If you want to know if an instance has a key or keys matching some pattern use 'KEYS' instead of 'GET' to get an overview.
On production servers use 'KEYS' with care as you can limit it and it will cause a full scan of all keys!