Redis Server Database Cheatsheet

Redis is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. It is known for its speed and flexibility, making it a popular choice for various applications. This cheatsheet serves as a quick reference guide for managing a Redis server database, covering essential commands and concepts.

Installation and Connection

Installation

# Ubuntu
sudo apt-get update
sudo apt-get install redis-server

# macOS
brew install redis

Connection

redis-cli

Basic Commands

Setting and Retrieving Values

# Set a key-value pair
SET key_name "value"

# Get the value of a key
GET key_name

Expiring Keys

# Set a key with an expiration time (in seconds)
SET key_name "value" EX 3600

# Get the remaining time to live for a key
TTL key_name

Data Types

Strings

# Set a string value
SET string_key "Hello, Redis!"

# Get a string value
GET string_key

Lists

# Push elements to the beginning of a list
LPUSH list_name "element1" "element2"

# Get elements from a list
LRANGE list_name 0 -1

Sets

# Add members to a set
SADD set_name "member1" "member2"

# Get members of a set
SMEMBERS set_name

Hashes

# Set fields in a hash
HSET hash_name field1 "value1" field2 "value2"

# Get all fields and values in a hash
HGETALL hash_name

Sorted Sets

# Add members to a sorted set with scores
ZADD sorted_set_name 1 "member1" 2 "member2"

# Get members of a sorted set by score range
ZRANGEBYSCORE sorted_set_name 1 2

Advanced Commands

Pub/Sub (Publish/Subscribe)

# Subscribe to a channel
SUBSCRIBE channel_name

# Publish a message to a channel
PUBLISH channel_name "message"

Transactions

# Begin a transaction
MULTI

# Add commands to the transaction
SET key1 "value1"
SET key2 "value2"

# Execute the transaction
EXEC

Scripting with Lua

# Run a Lua script
EVAL "return redis.call('GET', 'key')" 0

This Redis cheatsheet covers some of the fundamental commands and concepts for managing a Redis server database. Redis is a powerful tool with a rich set of features, and mastering these basics will provide a solid foundation for working with this versatile in-memory data store. Keep this cheatsheet handy for quick reference as you explore and utilize Redis in your applications.

FAQ

1. What is Redis and why is it popular?

Redis is an open-source, in-memory data structure store known for its speed and flexibility. It serves as a database, cache, and message broker, making it popular for various applications due to its high performance and versatility.

2. How do I install Redis on my system?

For Ubuntu, use the command: sudo apt-get install redis-server. For macOS, use: brew install redis. These commands will install Redis on your system, allowing you to start using it.

3. How can I set an expiration time for a key in Redis?

You can set an expiration time for a key using the EX option with the SET command. For example: SET key_name "value" EX 3600 sets the key to expire in 3600 seconds (1 hour).

4. What are Redis data types, and how are they used?

Redis supports various data types, including strings, lists, sets, hashes, and sorted sets. These data types allow you to store and manipulate different kinds of data efficiently, providing flexibility in designing data structures for your application needs.

5. How does Pub/Sub work in Redis?

Pub/Sub (Publish/Subscribe) in Redis allows message broadcasting. You can subscribe to channels and publish messages to those channels. Subscribers receive messages in real-time, making it a powerful tool for building real-time communication and event-driven systems.