DEV Community

Cover image for Redis 101: Foundation and Core Concepts
Muhammad Hasham
Muhammad Hasham

Posted on • Originally published at muhammadhasham.com

Redis 101: Foundation and Core Concepts








Redis has been in the database game for quite a while now. The popularity has been increasing due to various factors that are discussed below. But before starting we should understand what is Redis actually.
So as per the docs state

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.

alt text

Now to be honest, the definition is difficult to understand. But I am quite sure that by the end of this article you would comprehend it properly.

Let’s start by understanding what are NoSQL Databases in general. NoSQL databases are used to store data in any other format than rows and columns. These are widely used to store data that is non relational in nature.

For example, consider two tables Person and Mobile_Phones, the two tables tend to have a relation (A person can have one or many mobile phones). So in scenarios where we need to relation among data SQL Databases are preferred.

Consider another scenario where the data to be stored has entries for two schemas such as Furniture and Mobile_Phones. It is quite evident that there is no such relation in above data. So storing this type of data in NoSQL database may increase overall performance.

Examples for relational databases consist of MySQL,PostgreSQL. On the other hand, MongoDB,Redis,Neo4j etc are examples for NoSQL databases.



alt text


Redis has different data structures to store data. Let’s explore them one by one.

Strings

This is the base type of all the types. Every single value can be stored as strings.

SET name fido
Enter fullscreen mode Exit fullscreen mode

We are storing the string as a key value pair.

Key-Value Pair

The most easy and used data structure for Redis is storing string in key-value pair.


SET name fido
GET name
// fido
Enter fullscreen mode Exit fullscreen mode

If you are familiar with Javascript objects or Python dictionaries. You can think of it as:

{
  name: "fido"
}
Enter fullscreen mode Exit fullscreen mode

Lists

alt text

Lists are actually lists of strings stored in a particular order. Lists are also good when we want just to create a collection of N items where usually we access just the top or bottom items, or when N is small. Because random access is slow and takes O(N) time to complete.

Lists have several commands such as RPUSH, LPUSH, LLEN, LRANGE, LPOP, and RPOP. These commands help to perform specific operations lists.

# RPUSH puts the new value at the end of the list.
RPUSH friends "Alice"
# LPUSH puts the new value at the start of the list.
LPUSH friends "Bob"
Enter fullscreen mode Exit fullscreen mode

LRANGE gives a subset of the list. It takes the index of the first element you want to retrieve as its first parameter and the index of the last element you want to retrieve as its second parameter.

LRANGE friends 0 -1
Enter fullscreen mode Exit fullscreen mode

LLEN returns the length of linked list

LPUSH friends "Alice"
LPUSH friends "Bob"
LLEN friends  # => 2
Enter fullscreen mode Exit fullscreen mode

LPOP and RPOP removes (pops) elements from start and end respectively.

# RPOP pops the value from the end of the list.
RPOP friends
# LPOP pops the value at the start of the list.
LPOP friends "Bob"
Enter fullscreen mode Exit fullscreen mode

Sets

Among differences between sets and lists is that sets store unique elements unlike lists.

Sets are an unordered data collection. Sets perform fast iteration than lists across elements.

Sets support complex operations like intersections, unions, and so forth, so this is a good data structure for using Redis in a “computational” manner, when you have data and you want to perform transformations on that data to obtain some output.

In sets adding, removing and lookup for an element takes O(1) constant time.

Small sets are encoded in a very efficient way.

Among many commands for Sets we will discuss SADD, SREM, SISMEMBER, SMEMBERS.

SADD adds a member to a set

SADD superpowers "flight"
Enter fullscreen mode Exit fullscreen mode

SREM removes a member from a set

SREM superpowers "flight"
Enter fullscreen mode Exit fullscreen mode

SISMEMBER tests if the given value is in the set. It returns 1 if the value is there and 0 if it is not.

SISMEMBER superpowers "flight"  # => 1
SISMEMBER superpowers "height"  # => 0
Enter fullscreen mode Exit fullscreen mode

SMEMBERS returns all elements from a set.

SMEMBERS superpowers # => "flight", "height"
Enter fullscreen mode Exit fullscreen mode

Sorted Sets

Sorted sets are the only other data structures, besides lists, to maintain ordered elements.

Sorted sets are like more powerful lists where inserting, removing, or getting ranges from the the middle of the list is always fast. But they use more memory, and are O(log(N)) data structures.

ZADD command is used to add all the specified members with the specified scores to the sorted set stored at key. ZRANGE displays all elements within the set -1 indicates the last index of set.

ZADD mycolorset 1 white
ZADD mycolorset 2 black
ZRANGE mycolorset 0 -1

#white
#black
Enter fullscreen mode Exit fullscreen mode

Suppose if we insert another element at index 2. The element placed at index 2 will be shifted to the next index and the new element would take the place.

ZADD mycolorset 2 yellow
ZRANGE mycolorset 0 -1
#white 
#yellow
#black
Enter fullscreen mode Exit fullscreen mode



To remove an element, we can use ZREM as:

ZREM mycolorset yellow
Enter fullscreen mode Exit fullscreen mode

This would remove the element from the sorted set.


Hashes

Hashes are used to store collective information about something. Let’s discuss an example to get better understanding.

They act as maps between the string fields and the string values. Hence, they are the perfect data type to represent objects.

In Redis, every hash can store up to more than 4 billion field-value pairs.

HMSET person name "John Doe" 
designation "Backend Engineer" likes 20 shares 23
Enter fullscreen mode Exit fullscreen mode

In the above code snippet, we are using HMSET to store a hash. As we can see there are different fields associated with person.

Let’s retrieve all the data with HGETALL as

HGETALL person 
1) name
2) John Doe
3) designation
4) Backend Engineer
5) Likes
6) 20
7) shares
8) 23
Enter fullscreen mode Exit fullscreen mode

Now that we have covered the basic data types. Let’s discuss the use cases for Redis.

Apart from it’s many use cases, there are some popular use cases for Redis such as

  • Caching Layer Database
  • Pub/Sub
  • Get top analysis for something
  • Message Queues

Let’s discuss each of them briefly,

Caching Layer Database:

Redis being an in-memory store can be used as cache for storing user session or other relevant details.
Redis is highly performant to read data, which is one of the reasons of using it as a caching layer database
on top of the main database.

alt text

Pub/Sub:

Redis Pub/Sub implements the messaging system where the publishers sends the messages while the subscribers receive them.
The link by which the messages are transferred is called channel.A subscriber can subscribe to multiple publishers bases on scenario.

PUBLISH chat Hi there
SUBSCRIBE chat
Enter fullscreen mode Exit fullscreen mode

alt text

Get top analysis for something:

We can get top users, top trends or anything on edges. By edges, that means anything that lies on top or bottom of our list.
For example suppose we have some users and they have some scores. So we can easily store this in a sorted set and query as:

ZRANGE user_scores 0 10 WITHSCORES
Enter fullscreen mode Exit fullscreen mode

The above query would return top 10 users with their scores respectively. In scenarios like this Redis is the go to database.


Message Queues:

Redis has the same concept of queues as in general programming. Message queues can be implemented with push and pop if there are multiple processes running.





There are many libraries that provide ease and enhanced functionalities to work with Redis queues.


This was a brief introduction to the world of Redis there is a lot to explore. I would highly recommend to give a shot to this.

Good luck and don’t forget to like and share

Have any questions? just want to say 👋 @MohammadHasham_

Top comments (3)

Collapse
 
swarupkm profile image
Swarup Kumar Mahapatra

Most important point about Redis I feel, redis should not be treated as a persistance layer.
Once that mindset is achieved, the use redis is a charm.
The application should not crash/misbehave if redis data gets reset or wiped out.

One good use case of redis is to store "Feature Switches" or "Configurable Data".

Collapse
 
muhammadhasham profile image
Muhammad Hasham

Thank you for the feedback. It is indeed a good use-case of Redis.

 
muhammadhasham profile image
Muhammad Hasham

Great!