cover-img

Step by Step learning Redis

This is an introductory blog on Redis where we discuss the features that Redis provides, install it and then play with the redis cli.

7 December, 2022

8

8

0

In this blog, we will take a look at what Redis is, how it works, and how to use it. Then we will also play with the Redis CLI and learn some basic Redis Commands. So let's start.

Introduction

img
Redis stands for REmote DIctionary Server.
In simple words, Redis is an in-memory data structure store and is mainly popular for its low-level latency. The fact that it is in-memory means that the time taken to access the data is very low compared to in-disk databases. This can be useful for applications that require fast data access and manipulation, such as real-time bidding systems or gaming servers.
Redis was created by Salvatore Sanfilippo in 2009, also known as "antirez," in order to provide a fast, in-memory database that could be used for a variety of purposes. It has since become widely popular for its performance, reliability, and flexibility.
img

Salvatore Sanfilippo

It is because of its low latency and high performance that Redis is used by a lot of major companies, such as:

Twitter

Snapchat

GitHub

Pinterest

Craigslist
img

Companies that use Redis

How does Redis work?

Redis is a key-value store, which means that it stores data in the form of key-value pairs. The key is used to access the value, and the value can be of any type, such as a string, a list, a set, a sorted set, a hash. These data types are supported natively by Redis and provide a rich set of commands for manipulating and querying the data.
In addition to these data types, Redis can be extended with Redis Modules. Some of these modules are provided by Redis Labs, and some are provided by the community. Some of the modules provided by Redis Labs are:

RediSearch: This module provides full-text search capabilities for Redis, allowing users to index, search, and query text data stored in Redis similar to Elasticsearch.

RedisGraph: This module provides graph database capabilities for Redis, allowing users to store, query, and manipulate graph data structures similar to Neo4j.

RedisTimeSeries: This module provides time series data management capabilities for Redis, allowing users to store, query, and visualize time series data similar to InfluxDB.

RedisJSON: This module provides native support for JSON data in Redis, allowing users to store, manipulate, and query JSON data using Redis commands similar to MongoDB.

RedisBloom: This module provides probabilistic data structures for Redis, allowing users to store and query data using bloom filters, count-min sketches, and other data structures that can approximate data sets.
These are just a few examples of the many Redis Modules that are available. There are many more modules that provide a wide range of additional functionality for Redis. For a complete list of available modules, you can visit the Redis Modules registry at the following URL: redis.io/resources/modules.
This makes Redis a multi-model database.

Installing Redis

Redis can be installed on Linux, Windows, and macOS using the following steps:

Linux

Begin by updating your local package index with the latest package information:

Windows

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

macOS

Install the Redis server and command-line tools using Homebrew:
Once the installation is complete, you can start the Redis server using the following command:
To enable Redis to start automatically when the system boots, you can use the following command:
Once, you have redis installed, you can start it by running the following command:
img

Redis Server started

Using Redis

Redis provides a command-line interface (CLI) that can be used to interact with the Redis server. You can start the Redis CLI using the following command:

Basic Redis Commands

You can set a key-value pair in Redis using the SET command. For example, to set the key name to the value John, you can use the following command:
It will return OK if the operation is successful.
Now, to get the value of the key name, you can use the GET command:
It will return John.
You can also delete a key using the DEL command:
It will return 1 if the operation is successful. Now, if you try to get the value of the key name, it will return (nil).
To get a list of all the keys in the database, you can use the KEYS command:
Here is how it looks like:
To get the number of keys in the database, you can use the DBSIZE command:
It will return 3 in this case.

Setting Expiration Time

The expiration time of a key refers to the amount of time that the key will remain in the database before it is automatically deleted. We can set the expiration time of a key using the EXPIRE command. For example, to set the expiration time of the key name to 10 seconds, you can use the following command:
It will return 1 if the operation is successful. Now, if you try to get the value of the key name, it will return (nil) after 10 seconds.
To find out the remaining time before the key expires, you can use the TTL command:
It will return -2 if the key does not exist, -1 if the key does not have an expiration time, or the number of seconds remaining before the key expires.

Handling Lists

Redis provides a data structure called a list that can be used to store a collection of strings. You can create a list using the RPUSH command. For example, to create a list named colors and add the values red, green, and blue to it, you can use the following command:
Here RPUSH refers to the right push operation. It will return 3 (the number of elements pushed) if the operation is successful.

Note: Like RPUSH there are other commands like LPUSH (left push), LPOP (left pop), and RPOP (right pop) that can be used to push and pop elements from the list.

To get all the elements in the list, you cannot use the GET. This is because you are dealing with lists, so you have to use the LRANGE command:
Here 0 refers to the starting index and -1 refers to the ending index. It will return all the elements in the list.

Handling Sets

Sets are similar to lists, but they do not allow duplicate elements. You can create a set using the SADD command. For example, to create a set named employee_ids and add the values 1001, 1002, and 1003 to it, you can use the following command:
To get all the elements in the set, you can use the SMEMBERS command:
If you try to add an element that already exists in the set, it will be ignored. For example, if you try to add the value 1001 to the set employee_ids, it will be ignored.
In the case of sets, to check if an element exists in the set, you can use the SISMEMBER command:
To remove an element from the set, you can use the SREM command:

Handling Hashes

Hashes are also key-value pairs. So, using hashes in Redis is like using key-value pairs inside key-value pairs.
To create a hash, you can use the HSET command. For example, to create a hash named employee and add the values id, fname, and lname to it, you can use the following command:
And to get the value of a field in the hash, you can use the HGET command:
To get all the fields in the hash, you can use the HKEYS command:
To get all the values in the hash, you can use the HVALS command:
To get everything in the hash, you can use the HGETALL command:
To delete a field from the hash, you can use the HDEL command:
To check if a field exists in the hash, you can use the HEXISTS command:
You can see initially the field id exists in the hash employee so the output was 1. After deleting it, it no longer exists and hence the value is 0.
Another very useful command is flushall. It will delete all the keys in the database. So, if you want to delete all the keys in the database, you can use the following command:

Conclusion

In conclusion, Redis is a popular and powerful data management tool that is widely used by organizations for a variety of purposes.

Its key features and benefits include high performance, flexibility, and support for a wide range of data structures.

Once you have installed it, you can use a variety of basic Redis commands, such as SET, GET, DEL, and KEYS, to manage and manipulate data in Redis. By understanding and using these commands effectively, you can unlock the full potential of Redis to support your specific use cases.
I would encourage you to experiment with Redis and explore its capabilities to gain a deeper understanding of how it can help your organization.
If you like this article, please share it with your friends and colleagues who are interested in Redis. If you have any questions or comments, please feel free to leave them below. I will be happy to answer them. For more articles on Redis, please follow me.
My Website: arnabsen.dev
 My Twitter: @ArnabSen1729

tutorials

tutorial

handsontutorial

develevate

8

8

0

tutorials

tutorial

handsontutorial

develevate

Arnab Sen
Final year undergrad pursuing B.Tech in CSE and passionate about Software Development.

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.