DEV Community

Cover image for Redis for Beginners (Part 1 of 2)
topeogunleye
topeogunleye

Posted on

Redis for Beginners (Part 1 of 2)

Redis has come a long way; 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 JSON support and search modules 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

Enter fullscreen mode Exit fullscreen mode

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

Installation

From the terminal, run:

brew install redis

Enter fullscreen mode Exit fullscreen mode

After running the above command, the following output is usually displayed:

==> Downloading <https://homebrew.bintray.com/bottles/redis-6.2.6.catalina.bottle.tar.gz>
==> Downloading from <https://d29vzk4ow07wi7.cloudfront.net/xxxxx/redis-6.2.6.catalina.bottle.tar.gz>
######################################################################## 100.0%
==> Pouring redis-6.2.6.catalina.bottle.tar.gz
==> Caveats
To have launchd start redis now and restart at login:
  brew services start redis
Or, if you don't want/need a background service you can just run:
  redis-server /usr/local/etc/redis.conf
==> Summary
🍺  /usr/local/Cellar/redis/6.2.6: 1,204 files, 9.6MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /usr/local/Cellar/old_formula/old_version... (xMB)
...
Removing: /Users/username/Library/Caches/Homebrew/old_formula--old_version... (xMB)
...
==> Caveats
==> redis
To have launchd start redis now and restart at login:
  brew services start redis
Or, if you don't want/need a background service you can just run:
  redis-server /usr/local/etc/redis.conf

Enter fullscreen mode Exit fullscreen mode

This will install Redis on your system.

On Windows, use WSL

Installation

From Powershell, run:

wsl --install

Enter fullscreen mode Exit fullscreen mode

After running the above command, the following output is usually displayed:

==> Starting Installation:
Installing: Virtual Machine Platform Virtual Machine Platform has been installed.
==> WSL Installation:
Installing: Windows Subsystem for Linux
Windows Subsystem for Linux has been installed.
==> Downloading and Installing Linux Distribution:
Downloading: [Distribution Name]
Installing: [Distribution Name]
==> Setting WSL2 as the Default Version:
Setting WSL 2 as the default.
Installation successful.
==> Prompt to Reboot:
The requested operation is successful. Changes will not be effective until the system is rebooted.

Enter fullscreen mode Exit fullscreen mode

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:

Untitled

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

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

In your workbench input section, type the following command:

SET key value

Enter fullscreen mode Exit fullscreen mode
  • 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.
  • Press CTRL + Enter to execute the command.

For example, executing SET name charlie will set the value "maria" for the key name.

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

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 "Xhun li"

Enter fullscreen mode Exit fullscreen mode

GET: Retrieving a Value by Key

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

GET key

Enter fullscreen mode Exit fullscreen mode
  • Replace key with the name of the key you want to retrieve.

For example, if you have a key named name with the value "Xhun li", executing GET name will return "Xhun li". 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

Enter fullscreen mode Exit fullscreen mode
  • 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

Enter fullscreen mode Exit fullscreen mode

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.

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 ...

Enter fullscreen mode Exit fullscreen mode
  • Replace key1, key2, key3, etc., with the names of the keys you want to delete.

For example:

DEL name1 name2

Enter fullscreen mode Exit fullscreen mode

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.

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

Enter fullscreen mode Exit fullscreen mode
  • Replace key1, key2, key3, etc., with the names of the keys you want to retrieve.

For example:

MGET name color rating

Enter fullscreen mode Exit fullscreen mode

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

GETRANGE: Retrieving Substrings

To retrieve a substring of a value stored in a key, use the GETRANGE command. Here’s how to use it in your workbench:

GETRANGE key start end

Enter fullscreen mode Exit fullscreen mode
  • Replace key with the name of the key, start with the starting index, and end with the ending index.

For example:

  1. Set a key named name with the value "christopher":

    SET name christopher
    
    
  2. Retrieve the first five letters of "christopher" with the following command:

    GETRANGE name 0 4
    
    

This will return the substring "Chris".


Conclusion

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, and command options, and go into the differences between lists and sets in Redis. Stay tuned!

Top comments (0)