Getting started with Redis

Sayeem Md Abdullah
3 min readJan 28, 2022

--

Redis, or Remote Dictionary Server, which is sometimes referred to as a data structure server, is used as a distributed key-value database, cache, and memory broker with optional durability. It is recommended that you have knowledge of Data Structures before getting started with Redis, since its primary data types are very similar to those found in programming languages, such as strings, lists, dictionaries (or hashes), sets, sorted sets, etc.

Installation

Install Redis on Mac With Homebrew

brew update
brew install redis

Use the following command to install Homebrew if you don’t already have it.

/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Install Redis on Ubuntu

$sudo apt-get update 
$sudo apt-get install redis-server

Testing 1, 2, 3

As a first step, we’ll run the Redis server. To do so, we’ll use the following commands.

redis-server

The above message indicates that the server is running, but I use the following command to run it in the background.

brew services start redis

The magic begins now, so we will run the Redis command-line interface by typing the following command.

redis-cli

Playing with the Redis CLI

Hurrah! Now that the CLI is running successfully, let’s start playing around. Let’s do a ping. We should get the following output when we type PING.

Now I will show some commands below, but there are many more available in this doc. It is not necessary to know all of them as we can always Google while working or solving a problem.

These are some of the most basic commands in Redis. As we are using “SET name Sayeem”, the name is the key and Sayeem is the value. If the key already holds a value, the value is overwritten. Then we used GET, EXISTS and DEL to retrieve, check if the data exists, and delete data by key, respectively. EXPIRE is an interesting command, this command is used to set an expiration time for a key so that it will expire automatically after the seconds we specified.

Recently, I used these commands for OTP verification. A basic process involved creating an OTP and storing it in Redis with an expiration time and then, upon getting a request, we check if the key and value exist and if they do, they are verified, and then they are deleted after the verification.

In addition to these, Redis also lets us use Hashes, Lists, Sets, Sorted Sets, among others to store data. I’ll end this blog here, but will soon post more on Redis Configuration, Clusters, and more.

--

--

Sayeem Md Abdullah

A Software Engineer with a passion for Storytelling and Filmmaking