- Deprecated starting with Redis 5. Use REPLICAOF instead. REPLICAOF host port Make the server a replica of another instance, or promote it as master. SLOWLOG subcommand argument Manages the Redis slow queries log.
- Redis Cheat Sheet by James Hopkin (tasjaevan) via cheatography.com/18964/cs/2046/ Scripts EVAL Run EVALSHA Run cached SCRIPT EXISTS Check by hash SCRIPT FLUSH Clear cache SCRIPT KILL Kill running script SCRIPT.
Redis Cheat Sheet¶ Connect to redis: $ redis-cli Select a DB: redis 127.0.0.1:6379 SELECT 4 List all keys: redis 127.0.0.1:6379 KEYS. Delete a key: redis 127.0.0.1:6379 DEL.
Introduction
This is part one of a Redis cheat sheet tutorial series. Redis, an acronym for Remote Directory Server, is an in-memory database that supports multiple data types. Redis works by mapping keys to values that functions as a key-value-based caching system. Redis commands perform some operations on Redis server, with some commands being able to be executed from the Redis command-line interface, or CLI. The CLI is an uncomplicated program that permits the sending of commands to and reading the replies from Redis directly from the terminal. This fist section of this tutorial series will provide instructions for using many of the Redis commands.
Prerequisites
A properly running Redis server and Redis CLI installed and working on the local device are required to follow the examples provided in this Redis cheat sheet tutorial.
Execute of the
redis-cli PING
command will return an output ofPONG
if Redis is installed. Thesudo systemctl status redis
command in a Linux distribution system will also verify if Redis is running.
Display the information on the Redis cli server statics
Execute the following INFO
command to obtain the statistics of the Redis server in a readable format:
redis-cli INFO |
The results should provide information similar to the following on the parameters that a computer is able to parse:
server
: Information about the Redis server on the local device, such as the version number and pertinent data.clients
: The section where the clients connections are counted.memory
: Provides information on memory usage of the server and other related data.persistence
: Gives information about the Redis database backup and the Append Only file.stats
: Provides general information regarding the statistics in Redis.replication
: Supplies role master and replication data.cpu
: Information about CPU consumption.cluster
: This shows the cluster section of the Redis server.keyspace
: Relates to database statistics.
Accessing the interactive cli tool for Redis
Execute the redis-cli
command in the terminal to open the prompt that accesses the Redis command-line interface tool.
- Execute the following command to monitor the currently active commands in Redis:
127.0.0.1:6379> monitor OK 1580867465.612664[0 127.0.0.1:51038]'PING' 1580867466.645500[0 127.0.0.1:51038]'PING' 1580867466.708354[0 127.0.0.1:51038]'PUBLISH''__sentinel__:hello''127.0.0.1,26379,05db92f504c2d072ff76c8d566502be6b73514c1,0,mymaster,127.0.0.1,6379,0' |
NOTE: Executing the CTRL + C will end the above operation.
- The following command will display the maximum number of the databases in Redis:
127.0.0.1:6379> config get databases 1)'databases' 2)'16' |
- The following command will list all available databases:
info 'keyspace' # Keyspace db0:keys=3,expires=0,avg_ttl=0 |
- Now execute the following command to select the desired database:
This will present an index of databases, displayed as numbers. For example, the default database in Redis is [0], so executing this command allows for switching to another database. The output should resemble the following:
127.0.0.1:6379[1]> |
Now the second database, which has the index of [1]
, can be selected.
- Execute the following command to drop the currently selected database:
Note that changing the DB
to ALL
after the FLUSH
command will drop all of the databases.
String commands in Redis
The basic type of value in Redis is known as a string and may contain any type of the data.
The APPEND function
- This shows how to append the key value:
EXISTS mykey (integer)1 APPEND mykey World (integer)10 GET mykey 'HelloWorld' |
BITCOUNT function
- This counts the set bits number.
The DECR function
- This will decrement the integer by one:
SET count 21 OK DECR count (integer)20 |
The DECRBY function
- This function decrements the integer value of given number:
The GET function
- This returns the value of the key:
GET mykey 'HelloWorld' GET count '15' |
The GETRANGE function
- This will return the substring value of the key:
The GETSET function
- This function will set the new value of the key and return the old value:
GETSET count 0 '15' GETSET count 1 '0' GETSET count 12 '1' GETSET count 0 '12' |
The INCR function
- This increments the value of the key:
INCR count (integer)1 INCR count (integer)2 INCR count (integer)3 |
The INCRBY function
- This function will increment the integer value of given number:
The INCRBYFLOAT function
- This will increment the given value of the key by a float value:
INCRBYFLOAT count 2.6 '105.6' |
Redis Cheat Sheet 2020
The MSET function
- This function sets a multiple key with a multiple-key value:
The MGET function
- This will return the value of all of the given keys:
MGET key1 key2 key3 1)'How ' 2)'are ' 3)'you?' |
The MSETNX function
- This function sets the multiple key value, but only if no value currently exists in the keys:
MSETNX 1key 'I'm' 2key 'fine' (integer)1 MSETNX 2key 'hey' 3key 'thankyou' (integer)0 |
The SET function
- This sets the key value:
The SETBIT function
- This will offset the bit in the string value:
SETBIT kv 51 (integer)0 |
The SETNX function
- Like the MSETNX function, this will set the key value if no value currently exists:
SETNX value 'ObjectRocket' (integer)1 SETNX value 'orkb' (integer)0 |
The SETRANGE function
- This function will overwrite the stored string in the key starting with a specified offset:
The STRLEN function S atomic symbol.
- This returns the length of the key value:
STRLEN mykey (integer)11 |
Conclusion
This was part one in a Redis cheat sheet tutorial series that gave instructions for using many of the more common and basic Redis commands. Part one of of this series covered how to display the information on the Redis CLI server, how to access the interactive CLI tool for Redis and provided examples for over a dozen functions for Redis string commands. Remember that while the Redis CLI is a very useful tool for executing many Redis commands, it is not suitable for executing some of the more complicated Redis functions. Subsequent parts of this tutorial series will cover more of the commonly used Redis commands.
Note: The Redis Documentation is also available in raw (computer friendly) format in the redis-doc github repository. Apple part. The Redis Documentation is released under the Creative Commons Attribution-ShareAlike 4.0 International license.
*Programming with Redis
- The full list of commands implemented by Redis, along with thorough documentation for each of them.
- Pipelining: Learn how to send multiple commands at once, saving on round trip time.
- Redis Pub/Sub: Redis is a fast and stable Publish/Subscribe messaging system! Check it out.
- Redis Lua scripting: Redis Lua scripting feature documentation.
- Debugging Lua scripts: Redis 3.2 introduces a native Lua debugger for Redis scripts.
- Memory optimization: Understand how Redis uses RAM and learn some tricks to use less of it.
- Expires: Redis allows to set a time to live different for every key so that the key will be automatically removed from the server when it expires.
- Redis as an LRU cache: How to configure and use Redis as a cache with a fixed amount of memory and auto eviction of keys.
- Redis transactions: It is possible to group commands together so that they are executed as a single transaction.
- Client side caching: Starting with version 6 Redis supports server assisted client side caching. This document describes how to use it.
- Mass insertion of data: How to add a big amount of pre existing or generated data to a Redis instance in a short time.
- Partitioning: How to distribute your data among multiple Redis instances.
- Distributed locks: Implementing a distributed lock manager with Redis.
- Redis keyspace notifications: Get notifications of keyspace events via Pub/Sub (Redis 2.8 or greater).
- Creating secondary indexes with Redis: Use Redis data structures to create secondary indexes, composed indexes and traverse graphs.
*Redis modules API
- Introduction to Redis modules. A good place to start learing about Redis 4.0 modules programming.
- Implementing native data types. Modules scan implement new data types (data structures and more) that look like built-in data types. This documentation covers the API to do so.
- Blocking operations with modules. This is still an experimental API, but a very powerful one to write commands that can block the client (without blocking Redis) and can execute tasks in other threads.
- Redis modules API reference. Directly generated from the top comments in the source code inside
src/module.c
. Contains many low level details about API usage.
*Tutorials & FAQ
- Introduction to Redis data types: This is a good starting point to learn the Redis API and data model.
- Introduction to Redis streams: A detailed description of the Redis 5 new data type, the Stream.
- Data types short summary: A short summary of the different types of values that Redis supports, not as updated and info rich as the first tutorial listed in this section.
- FAQ: Some common questions about Redis.
*Administration
- Redis-cli: Learn how to master the Redis command line interface, something you'll be using a lot in order to administer, troubleshoot and experiment with Redis.
- Configuration: How to configure redis.
- Replication: What you need to know in order to set up master-replicas replication.
- Persistence: Know your options when configuring Redis' durability.
- Redis Administration: Selected administration topics.
- Security: An overview of Redis security.
- Redis Access Control Lists: Starting with version 6 Redis supports ACLs. It is possible to configure users able to run only selected commands and able to access only specific key patterns.
- Encryption: How to encrypt Redis client-server communication.
- Signals Handling: How Redis handles signals.
- Connections Handling: How Redis handles clients connections.
- High Availability: Redis Sentinel is the official high availability solution for Redis.
- Latency monitoring: Redis integrated latency monitoring and reporting capabilities are helpful to tune Redis instances for low latency workloads.
- Benchmarks: See how fast Redis is in different platforms.
- Redis Releases: Redis development cycle and version numbering.
*Embedded and IoT
- Redis on ARM and Raspberry Pi: Starting with Redis 4.0 ARM and the Raspberry Pi are officially supported platforms. This page contains general information and benchmarks.
- A reference implementation of Redis for IoT and Edge Computing can be found here.
*Troubleshooting
- Redis problems?: Bugs? High latency? Other issues? Use our problems troubleshooting page as a starting point to find more information.
*Redis Cluster
- Redis Cluster tutorial: a gentle introduction and setup guide to Redis Cluster.
- Redis Cluster specification: the more formal description of the behavior and algorithms used in Redis Cluster.
*Other distributed systems based on Redis
- Redis CRDTs an active-active geo-distribution solutions for Redis.
- Roshi is a large-scale CRDT set implementation for timestamped events based on Redis and implemented in Go. It was initially developed for the SoundCloud stream.
*Redis on SSD and persistent memory
- Redis on Flash by Redis Labs extends DRAM capacity with SSD and persistent memory.
*Specifications
- Redis Design Drafts: Design drafts of new proposals.
- Redis Protocol specification: if you're implementing a client, or out of curiosity, learn how to communicate with Redis at a low level.
- Redis RDB format specification, and RDB version history.
- Internals: Learn details about how Redis is implemented under the hood.
*Resources
- Redis Cheat Sheet: Online or printable function reference for Redis.
*Use cases
*Books
The following is a list of books covering Redis that are already published. Books are ordered by release date (newer books first).
- Mastering Redis (Packt, 2016) by Jeremy Nelson.
- Redis Essentials (Packt, 2015) by Maxwell Da Silva and Hugo Tavares
- Redis in Action (Manning, 2013) by Josiah L. Carlson (early access edition).
- Instant Redis Optimization How-to (Packt, 2013) by Arun Chinnachamy.
- Instant Redis Persistence (Packt, 2013) by Matt Palmer.
- The Little Redis Book (Free Book, 2012) by Karl Seguin is a great free and concise book that will get you started with Redis.
- Redis Cookbook (O'Reilly Media, 2011) by Tiago Macedo and Fred Oliveira.
The following books have Redis related content but are not specifically about Redis:
- Seven databases in seven weeks (The Pragmatic Bookshelf, 2012).
*Credits
Redis is developed and maintained by the Redis community.
The project was created, developed and maintained by Salvatore Sanfilippo until June 30th, 2020. In the past Pieter Noordhuis and Matt Stancliff provided a very significant amount of code and ideas to both the Redis core and client libraries.
The full list of Redis contributors can be found in the Redis contributors page at Github. However there are other forms of contributions such as ideas, testing, and bug reporting. When it is possible, contributions are acknowledged in commit messages. The mailing list archives and the Github issues page are good sources to find people active in the Redis community providing ideas and helping other users.
*Sponsors
The work Salvatore Sanfilippo does in order to develop Redis is sponsored by Redis Labs. Other sponsors and past sponsors of the Redis project are listed in the Sponsors page.
*License, Trademark and Logo
Redis Commands Cheat Sheet
- Redis is released under the three clause BSD license. You can find additional information in our license page.
- The Redis trademark and logos are owned by Redis Labs Ltd, please read the Redis trademark guidelines for our policy about the use of the Redis trademarks and logo.