logo
eng-flag

Redis Cheatsheet

Table of Contents

  1. Basic Commands
  2. Strings
  3. Lists
  4. Sets
  5. Sorted Sets
  6. Hashes
  7. Pub/Sub
  8. Transactions
  9. Keys and Expiration
  10. Server
  11. Persistence
  12. Replication
  13. Cluster
  14. Lua Scripting
  15. Monitoring and Debugging

Basic Commands

Connect to Redis

redis-cli

Ping the server

PING

Get all keys

KEYS *

Delete a key

DEL key

Check if a key exists

EXISTS key

Get the type of a key

TYPE key

Strings

Set a string value

SET key value

Get a string value

GET key

Set multiple string values

MSET key1 value1 key2 value2

Get multiple string values

MGET key1 key2

Increment a number

INCR key

Decrement a number

DECR key

Lists

Push to the head of a list

LPUSH key value

Push to the tail of a list

RPUSH key value

Pop from the head of a list

LPOP key

Pop from the tail of a list

RPOP key

Get a range of elements from a list

LRANGE key start stop

Get the length of a list

LLEN key

Sets

Add members to a set

SADD key member [member ...]

Remove members from a set

SREM key member [member ...]

Get all members of a set

SMEMBERS key

Check if a member exists in a set

SISMEMBER key member

Get the number of members in a set

SCARD key

Get the intersection of multiple sets

SINTER key [key ...]

Sorted Sets

Add members to a sorted set

ZADD key score member [score member ...]

Get a range of members from a sorted set

ZRANGE key start stop [WITHSCORES]

Remove members from a sorted set

ZREM key member [member ...]

Get the score of a member

ZSCORE key member

Get the rank of a member

ZRANK key member

Hashes

Set hash fields

HSET key field value

Get hash fields

HGET key field

Set multiple hash fields

HMSET key field1 value1 field2 value2

Get multiple hash fields

HMGET key field1 field2

Get all fields and values of a hash

HGETALL key

Delete hash fields

HDEL key field [field ...]

Pub/Sub

Subscribe to channels

SUBSCRIBE channel [channel ...]

Publish messages to a channel

PUBLISH channel message

Unsubscribe from channels

UNSUBSCRIBE [channel [channel ...]]

Transactions

Start a transaction

MULTI

Execute a transaction

EXEC

Discard a transaction

DISCARD

Watch keys for changes

WATCH key [key ...]

Keys and Expiration

Set expiration time for a key

EXPIRE key seconds

Set expiration timestamp for a key

EXPIREAT key timestamp

Get the remaining time to live of a key

TTL key

Remove the expiration from a key

PERSIST key

Server

Get server information

INFO

Get the current server time

TIME

Execute a command on a slave

SLAVEOF host port

Get the role of the instance

ROLE

Persistence

Save the DB on disk

SAVE

Save the DB in the background

BGSAVE

Get the UNIX time stamp of the last successful save

LASTSAVE

Replication

Make the server a replica of another instance

SLAVEOF host port

Make a replica into a master

SLAVEOF NO ONE

Get replication information

INFO replication

Cluster

Meet another cluster node

CLUSTER MEET ip port

Get information about the cluster

CLUSTER INFO

Get the state of cluster nodes

CLUSTER NODES

Assign slots to a node

CLUSTER ADDSLOTS slot [slot ...]

Lua Scripting

Execute a Lua script

EVAL script numkeys key [key ...] arg [arg ...]

Load a script into the scripts cache

SCRIPT LOAD script

Execute a loaded script

EVALSHA sha1 numkeys key [key ...] arg [arg ...]

Monitoring and Debugging

Monitor all commands processed by Redis

MONITOR

Get slow logs

SLOWLOG GET [number]

Get the current client list

CLIENT LIST

Kill a client connection

CLIENT KILL ip:port

Get memory usage information

MEMORY USAGE key

2024 © All rights reserved - buraxta.com