Total Pageviews

Saturday, 6 June 2026

Redis-for-beginners

 

Part 1: Redis for Beginners (Part 1 of 2)

Introduction

Redis has come a long way through an evolution and, by now, it can be placed as your primary database—not just a cache layer. It features added data persistence and replication for durability and availability. Additional modules for JSON support and search make it easier to store and query more complex data. Now, Redis has an Object mapping library, Redis OM, which simplifies things.

In this two-part series, the focus is on the core of Redis. This is the first part, which will guide you through setting up a Redis database and using some basic commands.

Setting Up a Redis Database

On a Macbook, use Homebrew

First, make sure you have Homebrew installed. From the terminal, run:

brew --version

If this command fails, you'll need to follow the Homebrew installation instructions.

Installation

From the terminal, run:

brew install redis

This will install Redis on your system.

On a Windows, use WSL

Microsoft provides detailed instructions for installing WSL. Once you're running Ubuntu on Windows, you can follow the steps detailed at Redis Site Installation Steps which are the same steps for installing Redis on Linux to install recent stable versions of Redis from the official packages.

Using Redis Cloud

The last option for installing Redis is called Redis Cloud, and that's what you’ll be using here. It allows you to set up a Redis database online. It also comes with Redis insights that can be used to test different commands and visualize your stored data.

To use the Redis database online, follow the steps below:

  1. Sign Up for a Free Redis Account:

    • Visit the Redis website (https://redis.io/) and sign up for a free account.
    • Provide the necessary details and create your account.
  2. Create a Subscription:

    • Once logged in, navigate to the “Subscriptions” section (usually found in the left-hand menu).
    • Click on “New Database” or a similar option.
    • Scroll down and select the free tier, which typically comes with 30MB of storage.
    • Click on “Create Database.”
  3. Download the Redis App:

    • To work with Redis locally, download the Redis app for your system (Windows, macOS, or Linux).
    • Install the app following the instructions provided.
  4. Connect to Your Database:

    • After creating the database, find the “Connect” option and click on it.
    • Click on the "Open with RedisInsights" button. This will launch your installed Redis application.

Basic Commands

When using your RedisInsights application, navigate to your workbench. You can access your workbench by clicking on this icon:

SET: Using the SET Command to Set a Key-Value Pair

To set a key-value pair in Redis, follow these steps:

  1. In your workbench input section, type the following command:
SET key value

Replace key with the name of the key you want to set, and value with the corresponding value you want to assign to that key.

  1. Press CTRL + Enter to execute the command. For example, executing SET name maria will set the value "maria" for the key name.

  2. You should receive the message "OK" in the output section below, confirming that the key-value pair was successfully set.

  3. To verify, go back to the Redis browser, switch to the data view, and refresh the view to see the updated key-value pair.

Error Handling:

If you include a space in your data without quotes, you’ll get a syntax error. To include spaces, enclose the data in quotes.

Example:

SET name "chun li"

GET: Retrieving a Value by Key

To retrieve the value of a specific key, use the GET command. In your workbench, type:

GET key

Replace key with the name of the key you want to retrieve. For example, if you have a key named name with the value "chun li", executing GET name will return "chun li". Press CTRL + Enter to execute the command.

DEL: Deleting Keys

To delete one or more keys and their associated values, use the DEL command. In your workbench, type:

DEL key1 key2 key3 ...

Replace key1, key2, key3, etc., with the names of the keys you want to delete. For example:

DEL name1 name2

Executing this command will delete the keys name1 and name2 along with their respective values. The command will return an integer indicating the number of keys deleted. For instance, a response of 2 indicates that two keys were successfully deleted. Press CTRL + Enter to execute the command.

SET MULTIPLE: Setting Multiple Key-Value Pairs

To set multiple key-value pairs simultaneously, use the MSET command. In your workbench, use:

MSET key1 value1 key2 value2 key3 value3

Replace key1, key2, key3, etc., with the names of the keys you want to set, and value1, value2, value3, etc., with their respective values. For instance:

MSET name1 maria name2 yoshi color green rating 10

This command sets the keys name1, name2, color, and rating with their respective values. Press CTRL + Enter. Ensure the key comes first, followed by the value.

GET MULTIPLE: Retrieving Multiple Values

To retrieve values for multiple keys at once, use the MGET command. In your workbench, use:

MGET key1 key2 key3

Replace key1, key2, key3, etc., with the names of the keys you want to retrieve. For example:

MGET name1 name2 rating

This command will return the values associated with keys name1, name2, and rating. Press CTRL + Enter. The values "maria", "Yoshi", and "10" will be returned in the output section.

GETRANGE: Retrieving Substrings

To retrieve a substring of the value of a key, use the GETRANGE command. In your workbench, use:

GETRANGE key start end

Replace key with the name of the key, start with the starting index, and end with the ending index. For example:

GETRANGE name 0 4

This command will return the substring of the value of name from index 0 to 4. If name has the value "chun li", the command will return "chun ".

Conclusion of Part 1

In this first part of our Redis for Beginners series, you learned how to set up a Redis database and run basic commands such as setting, getting, deleting, and retrieving multiple key-value pairs. These are basic skills that anyone working with Redis should be conversant with.

In the next part of this series, we will explore more advanced commands, command options, and go into the differences between lists and sets in Redis. Stay tuned!

from  https://github.com/topeogunleye/Writing/blob/master/Redis-for-beginners-Part-1.md

--------------- 

 

Part 2: Redis for Beginners (Part 2 of 2)

Introduction

Welcome to the second part of our Redis for Beginners series. In the first part, we covered the basics of setting up a Redis database and executing fundamental commands. In this second part, we will explore advanced command options and delve into the differences between lists and sets in Redis.

Command Options

Not all commands have options, but a few, for example SET, have certain added features that can be essential.

  • EX seconds: Sets an expiration time in seconds (must be a positive integer).
  • PX milliseconds: Sets an expiration time in milliseconds (must be a positive integer).
  • EXAT timestamp-seconds: Sets a specific Unix timestamp for expiration (must be a positive integer).
  • NX: Sets the key only if it does not already exist.
  • XX: Sets the key only if it already exists.

You can't use NX and XX together because they will conflict with each other. Also, you can use only one option for expiration (EX or PX) at a time. To learn more about the SET command visit here to check out the Redis documentation page.

Example: SET Command Using the EX Option

When executing the following at your workbench, you will define a key with an expiration in Redis:

SET key value EX seconds

Replace key with the name of the key that contains the string and value with the new value you would like to store. Replace seconds with the amount of time, in seconds, after which the key should expire.

For example, if you have a key name currently holding the value "Mario", and you want to replace it with "Yoshi" after 7 seconds, run:

SET name Yoshi EX 7

This command updates the value of name to "Yoshi" and sets an expiration of 7 seconds from the time the command is executed.

Example: SET Command with NX and XX Options

You can set the key conditionally with respect to its existence in Redis by using the options NX, which stands for Not eXists, and XX, which stands for eXists, in the SET command:

Using NX (Not eXists) Option:

SET key value NX
  • Replace key with the name of the key you want to set.
  • Replace value with the value you want to store in the key.

This command sets the value of key to value only if key does not already exist. If key already exists, the command will not perform any action.

Example:

If you want to set a new key username to "alice" only if username does not already exist, you would use:

SET username alice NX

**Using `XX

` (eXists) Option:**

SET key value XX

This command sets the value of key to value only if key already exists. If key does not exist, the command will not perform any action.

Example:

If you want to update the value of an existing key username to "bob" only if username already exists, you would use:

SET username bob XX

Lists vs. Sets

Lists

  • An ordered collection of strings.
  • Supports operations like adding elements to the head or tail, trimming based on ranges, etc.
  • Useful for maintaining ordered data structures.
  • Commands: RPUSH, LPUSH, LRANGE, LPOP, RPOP, etc.

Sets

  • An unordered collection of unique strings.
  • Supports operations like adding, removing, and checking membership.
  • Useful for storing unique items and performing set operations.
  • Commands: SADD, SREM, SMEMBERS, SISMEMBER, etc.

When deciding between lists and sets, consider the order requirements and the need for uniqueness in your data.


Conclusion

Redis has come a long way through an evolution and, by now, it can be placed as your primary database—not just a cache layer. It features added data persistence and replication for durability and availability. Additional modules for JSON support and search make it easier to store and query more complex data. Now, Redis has an Object mapping library, Redis OM, which simplifies things. In this series, the focus is on the core of Redis.

from  https://github.com/topeogunleye/Writing/blob/master/Redis-for-beginners-Part-2.md

No comments:

Post a Comment