Redis for Beginners (Part 1 of 2)
topeogunleye
Posted on July 25, 2024
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
If this command fails, you'll need to follow the Homebrew installation instructions.
Installation
From the terminal, run:
brew install redis
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
This will install Redis on your system.
On Windows, use WSL
Installation
From Powershell, run:
wsl --install
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.
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:
-
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.
-
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.”
-
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.
-
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:
In your workbench input section, type the following command:
SET key value
- Replace
key
with the name of the key you want to set, andvalue
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"
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 "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
- Replace
key1
,key2
,key3
, etc., with the names of the keys you want to set, andvalue1
,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.
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.
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 name color rating
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
- Replace
key
with the name of the key,start
with the starting index, andend
with the ending index.
For example:
-
Set a key named
name
with the value "christopher":
SET name christopher
-
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!
Posted on July 25, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.