Redis: Everything You Need To Know

Redis: Everything You Need To Know

ยท

6 min read

Have you ever imagined a huge JSON object used as a data store for an application, where data stored would exist as Key/Value pairs; Redis just made your imagination a reality. ๐Ÿค“

Redis is one of the most recent database technologies. It is similar to no-SQL databases like MongoDB but it has a lot of key and minor differences. Data stored in Redis are volatile because they are stored on the RAM instead of the disk of a computer, like most database systems. Redis data being stored on the RAM makes its operations faster, but Redis being volatile gives it the disadvantage of being unreliable as a primary database. That's why it's not advisable to use Redis as a primary database and in most scenarios, Redis is used as a caching system due to its speed.

Getting Started With Redis

To get started with Redis you need to have Redis installed on your computer. So, we would be looking at the Redis installation options for different operating systems; starting with Windows.

Install Redis on Windows

Use Redis on Windows for development

Redis is not officially supported on Windows. However, you can install Redis on Windows for development by following the instructions below.

To install Redis on Windows, you'll first need to enable WSL2 (Windows Subsystem for Linux). WSL2 lets you run Linux binaries natively on Windows. For this method to work, you'll need to be running Windows 10 version 2004 and higher or Windows 11.

Install or enable WSL2

Microsoft provides detailed instructions for installing WSL. Follow these instructions, and take note of the default Linux distribution it installs. This guide assumes Ubuntu.

Install Redis

Once you're running Ubuntu on Windows, you can follow the steps detailed at Install on Ubuntu/Debian to install recent stable versions of Redis from the official packages.redis.io APT repository. Add the repository to the apt index, update it, and then install:

curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

sudo apt-get update
sudo apt-get install redis

Lastly, start the Redis server like so:

sudo service redis-server start

Connect to Redis

You can test that your Redis server is running by connecting with the Redis CLI:

redis-cli 
127.0.0.1:6379> ping
PONG

Redis server runs on its default port of 6379.

Install Redis on macOS

Use Homebrew to install and start Redis on macOS

This guide shows you how to install Redis on macOS using Homebrew. Homebrew is the easiest way to install Redis on macOS. If you'd prefer to build Redis from the source files on macOS, see [Installing Redis from Source].

Prerequisites

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.

Starting and stopping Redis in the foreground

To test your Redis installation, you can run the redis-server executable from the command line:

redis-server

If successful, you'll see the startup logs for Redis, and Redis will be running in the foreground.

To stop Redis, enter Ctrl-C.

Starting and stopping Redis using launchd

As an alternative to running Redis in the foreground, you can also use launchd to start the process in the background:

brew services start redis

This launches Redis and restarts it at login. You can check the status of a launchd managed Redis by running the following:

brew services info redis

If the service is running, you'll see output like the following:

redis (homebrew.mxcl.redis)
Running: โœ”
Loaded: โœ”
User: miranda
PID: 67975

To stop the service, run:

brew services stop redis

Connect to Redis

Once Redis is running, you can test it by running redis-cli:

redis-cli

This will open the Redis REPL. Try running some commands:

127.0.0.1:6379> lpush demos redis-macOS-demo
OK
127.0.0.1:6379> rpop demos
"redis-macOS-demo"

Install Redis on Linux

How to install Redis on Linux

Most major Linux distributions provide packages for Redis.

Install on Ubuntu/Debian

You can install recent stable versions of Redis from the official packages.redis.io APT repository.

Prerequisites

If you're running a very minimal distribution (such as a Docker container) you may need to install lsb-release first:

sudo apt install lsb-release

Add the repository to the apt index, update it, and then install:

curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

sudo apt-get update
sudo apt-get install redis

Install from Snapcraft

The Snapcraft store provides Redis packages that can be installed on platforms that support snap. Snap is supported and available on most major Linux distributions.

To install via snap, run:

sudo snap install redis

If your Linux does not currently have snap installed, install it using the instructions described in Installing snapd.

Basic Commands For Redis-CLI

Inputting Data

To input data into a Redis database using the Redis-CLI, this command is used SET name value. Note that Redis-CLI is case insensitive so keywords such as SET can be either lowercase or uppercase.

For example, let's say we want to create a user named John with an age of 24. To do this we would run these commands in our Redis-CLI :

To start the server:

redis server
#It runs on its default port 6379

Open the CLI:

redis cli

Run the SET command:

#Name
SET name John 
#Or
set name John

#Age
SET age 24

When you use SET to input data into a Redis database, It will always be returned as a string.

Fetching Data

To fetch data from a Redis database using the Redis-CLI, this command is used SET name value. Note that Redis-CLI is case insensitive so keywords such as SET can be either lowercase or uppercase.

For example, let's say we want to create a user named John with an age of 24. To do this we would run these commands in our Redis-CLI :

To start the server:

redis server
#It runs on its default port 6379

Open the CLI:

redis cli

Run the GET command:

#Name
SET name John 
#Or
set name John

#Age
SET age 24

Data fetched will always be returned as a string.

Some Other Redis-CLI Commands

EXISTS:

This command is used to check if a key exists in a Redis database. If the key exists it will return 1.

SETEX:

This command is used to set key/value pairs with an expiration.

LPUSH:

This command is used to create a list datatype in a Redis database and add data at the top of the list.

LPOP:

This command is used to remove the leftmost or top data element in a Redis list.

RPUSH:

This command is used to add data at the bottom of a Redis list.

RPOP:

This command is used to remove the data element at the bottom of a Redis list.

LRANGE:

It is used to print or fetch all the elements in a Redis List.

SADD:

This command is used to add elements to a Redis set.

SREM:

This command is used to remove an element from a Redis set.

SMEMBERS:

This command is used to fetch/print all the elements in a Redis Set.

Redis Datatypes

Redis String: Redis strings are sequences of bytes. Strings in Redis are binary safe, meaning they have a known length not determined by any special terminating characters.

Redis List: Redis lists are collections of strings, sorted by insertion order.

Redis Set: Redis sets are an unordered collection of strings.

To use Redis in any of your preferred backend technology, ensure that you read its documentation on Redis and check for available tools and libraries that can assist you.

Thank You ๐Ÿ˜Š.

ย