Do you really need Redis?

Mohaned Mashaly
6 min readApr 15, 2021

In this article, we will explore Redis and examine its capabilities and limitations, in addition to presenting some of its use cases and famous commands, in addition to comparing Redis with other data storage solutions.

What is Redis?

Redis is a key-value server, and some people define it as a data structure server that stores data in the form of key and value structure, in simpler terms, Redis is a database server that stores your data according to the data structure you use, and key-value store stores data in the form of key and value, where the key is the unique id and the value is the data you want to retrieve, which somehow contradicts other storage solutions like SQL, for example, when performing a query in SQL, the server search for the value instead of the key.

Redis is composed of Redis-server and Redis client. Redis client is used in performing operations on data stored on the server.

In the next section, we will look at some of the famous commands and data structures Redis supports which hopefully will clarify Redis more.

Redis commands and data-structures

Redis commands are used to perform operations on the data available on the server. We do utilize the Redis commands to execute operations on the Redis client.

Redis supports five data structures, data structures in Redis influence how the data organization and its scheme. We will present some of these data structures that Redis does support.

String

Redis does support type string when defining the value of the key. For example, if I want to save the name of a person in Redis, I will perform the following command.

Set name:Mohamed “Mohamed”

The name key will get assigned to a value of string type equals to “Mohamed”, but wait, what if I want to store other names and we said earlier that key is unique which implies that if I did perform another command, for example, Set name Omar, the value of the key will change from Mohamed to Omar, if you did arrive at the same conclusion, then I believe because you are confusing Redis with SQL, in SQL you declare a column which is equivalent to the key in Redis and the key which is the column contain multiple values of the same type while in Redis, the key represents the column but it is not pre-defined, before inserting values. For example, if I want to create a key with the name “Mohamed”, I will type the following command.

Set name:Mohamed “Mohamed”

If I want to create another key with the name “Omar”, I will type the following command.

Set name:Omar “Omar”

If you wish to retrieve the value of Omar, replace Set with Get.

I hope now you get the idea of the key and how to use it.

Hash

But what if I wish to create a key with multiple values, unlike the string type that supports only a single value. Here comes the benefit of the hash data structure, where you can generate an object which contains various fields.

HMSET users:Mohamed name Mohamed age 29 gender maleHMSET users:Omar name Omar age 29 gender maleHMSET users:Mohamed name Mohamed age 29 gender male

Lists and Sets

If you want to store a list of items, you can use List or Set depending on your need.

To insert in List, type the following command.

LPUSH fruits WatermelonLPUSH fruits Orange

To insert in Set, Type the following command.

SADD courses DataBaseSADD courses SoftwareEngineering

The main difference between List and Set is ordering, List is ordered by insertion sort, while the set is not ordered.

Ordered Set

Sorted set is a set ordered by scores, where every element in the set is assigned a score, elements in a sorted set are ordered descendingly by their score.

ZADD leaderboard 10 mohamedZADD leaderboard 9 Zoda ZADD leaderboard 1 Omar 

Why Redis?

We did previously compare SQL with NoSQL, and we pointed out the differences between both schemas and when to use what.

Redis is an in-memory database, which means that data stored in the main memory, so writing and reading operations are faster in an in-memory database than persistent databases that write on disks instead of memory.

The fact that Redis is an in-memory database does make it more suitable for fast operations where milliseconds do matter. For example, a stock market application where seconds do matter, such an application can benefit from Redis’s fast pace. Redis also can be persistent, where Redis can save snapshots that describe the current state of the data in the system and store these snapshots on the disk.

Redis has a couple of features that make it a proper solution in a different situation, where these features can solve various problems in an efficient and elegant approach.

One of these features is the Publish-subscribe pattern.

Publish-subscribe is a messaging architecture or pattern, where the publisher acts as the sender and the subscriber acts as the receiver. What makes Publish-subscribe different from other messaging systems is the loose coupling and separation of concerns it does achieve. For example, in other messaging systems like client-server system, a client can not send a message unless the server is active and vice versa, wherein Publish-subscribe pattern, the publisher send his messages in a channel, and the subscriber subscribes to this channel, where the publisher loosely coupled from the subscriber, usually channel is called Message broker or event bus.

Publish-subscribe is an efficient solution in cases where the sender has to send messages to thousands or millions of subscribers, instead of sending the message to each subscriber, he does send it in the channel.

What makes Redis a good solution for some problems, is the features Redis does support and how Redis stores and structures data.

Implementing Application with Redis(Redis vs SQL and No-SQL)

To appreciate Redis and its importance. We will compare Redis and other data storage techniques in implementing a specific application.

Let’s say we want to build a leaderboard for an online game. The leaderboard will display player’s rank and their data.

Implementation in Redis

We want a data structure to store the player’s ranking. The best data structure to do so is the sorted set.

HSET users:mohaned name mohaned age 21 country EGY
HSET users:mohamed name mohamed age 22 country EGY
HSET users:Samuel name Samuel age 52 country USA
HSET users:Nielsen name Nielsen age 33 country Germany
HSET users:Mikel name Mikel age 19 country UK
ZADD leaderboard 1 users:Mikel
ZADD leaderboard 10 users:mohaned
ZADD leaderboard 7 users:Samuel
ZADD leaderboard 12 users:Nielsen
ZADD leaderboard 3 users:mohamed

The sorted set does a great job in implementing a leaderboard. It does naturally imitates the workings and design of a leaderboard.

Implementation in NoSQL

We will implement our solution in NoSQL using MongoDB. MongoDB is a document-based database simply it means that MongoDB store data as collections which are equivalent to table and consist of documents which are similar to rows.

First, we will create a database.

use leaderboard//then we will create a collection.db.createCollection("players")//then we will add documents
db.players.insert({name :"Mohaned",age : 21,country : "EGY",rank : 10})
db.players.insert({name :"Mohamed",age : 22,country : "EGY",rank : 3})db.players.insert({name :"Nielsen",age : 33,country : "Germany",rank :12})db.players.insert({name :"Samuel",age : 19,country : "USA",rank : 7})db.players.insert({name :"Mikel",age : 19,country : "UK",rank : 1})

The solution looks less intuitive than Redis, where the Ordered set is more fit to solve the problem.

The same implementation will be in SQL, only replace documents with tables and collections with rows. Of course, aside from the differences, we did mention in the comparison between SQL and NoSQL.

References

--

--

Mohaned Mashaly

Loves Computer Science with focused interest in (Back-end Development, Data Structures and Algorithms ,Machine Learning)