DEV Community

Cover image for Redis Data Structure Part 1
Michael Owolabi
Michael Owolabi

Posted on

Redis Data Structure Part 1

One of the outstanding features of Redis that make it is so loved among the developer community is the speed at which various operations are carried in a Redis database. So many things contribute to this efficiency in speed but one thing at the core of Redis regardless of how one chooses to look at it is its robust data structure.

The performance of every application that uses Redis is directly tied to how the application implements the various Redis data structures.

In this article, we are going to examine various data structures in Redis so as to give us insight on when it’s best to use which of the available data structures so that we can take full advantage of all the goodies Redis has to offer in our applications.

Prerequisites

  • To get the most out of this article, I strongly recommend that you get a copy of Redis for your computer here OR use the online Redis server at https://try.redis.io/ to try out simple commands that will be used in the course of this article.

  • Basic knowledge of Redis

Redis Data Structures

Below, we will examine a few of the data types that we have in Redis.

To get the type of value a key holds in Redis you can use the type command to do that as shown below.

type key1

String

This is the base and simplest data structure in Redis that can be associated with a key. Strings in Redis are binary safe and support polymorphism which means that a string key can be modified to hold binary, integer, or even floating-point values. String in Redis has a limit of 512 Mb data size that can be stored in them.

Common commands and operations in Redis String

SET
This is the command used to set or declare a Redis key to hold a string data type.

set day Saturday
set name “Michael Owolabi”
Enter fullscreen mode Exit fullscreen mode

Notice that I quote the value in the second example. We use the quote to group string that contains spaces in them.

Alt Text

GET
This can be used to retrieve the value stored in a string Redis key.
Get name will return the value “Michael Owolabi” that was set earlier.

Alt Text

INCR
This command is used to increment the value of a key by exactly one (1).
This is possible if the actual value of the key is a number. Remember that we said Redis strings are binary-safe
E.g.
Set price 10
Incr price
Get price returns 11 because its value has been increased by one.

Alt Text

The full list and detailed explanation of various Redis String commands can be found here

Hashes

A hash can be likened to objects in JavaScript or dictionary in Python or generally a JSON-like data type. It is like a mini key value stored within a Redis key.
A hash can have as many field-value pairs as possible until the size limit of a hash is reached which is 232 - 1 field-value pairs (more than 4 billion).

Redis hash is good for storing JSON like data especially when we’ll be working with the individual fields in the hash.

Common commands and operations in Redis Hash

HMSET
This is used to set the field-value pair of a hash key in Redis
e.g

hmset user:1 firstname Michael lastname Owolabi sex Male

This is the equivalent of a JSON structure like so:
{
"user:1": {
"firstname": "Michael",
"lastname": "Owolabi",
"sex": "Male"
}
}

HGET
This is used to retrieve the value of a single field in a Redis hask key. For example, suppose we want get the firstname of user:1 we’ll use the hmget command as below:

hmget user:1 firstname

Alt Text

HGETALL
Instead of retrieving the individual field value, this command is used to retrieve the entire field value in a Redis hash key.

hgetall user:1

Alt Text

The full list and detailed explanation of various Redis hash commands can be found here

List

This is an ordered collection of elements. Redis list is similar to Arrays in Ruby or JavaScript but differs in that it is implemented using a linked list. Lists in Redis provides a simple way to implement queues, stacks, and other data structures that rely on ordering.

Common commands and operations in Redis List

Unlike String and Hash in Redis, we don’t set a list instead we add or push elements on to the list

LPUSH
This command is used to add an element to the list from the left. The L in the command denotes that we want the new element(s) to be added to the list from the left.
For example, suppose we want to add numbers from 1 through 10 to a number list from the left we do it by running the command below:

lpush numbers 1 2 3 4 5 6 7 8 9 10

Alt Text

Since Redis list is zero-indexed, this means that element at index zero is 10 and the one at index nine is 1 because the elements are added from the left.

RPUSH
This is simply the opposite of the LPUSH command. It adds elements to the array from the right instead of left as we have in LPUSH.

LRANGE
This is used to retrieve elements of the list using a range.
To retrieve all the elements of a list specify the range from 0 to -1 which is the end of the list.

lrange numbers 0 -1

Alt Text

To get the element at a specified range or index specify the desired start and end index for elements you want to retrieve.
Lrange numbers 0 0 will return the element at index zero (0).

LLEN
This returns the length of the list.
llen numbers will return 10 since we have ten elements in the list.

The full list and detailed explanation of various Redis List commands can be found here

Set

The set data structure in Redis is an unordered collection of unique strings.

Common commands and operations in Redis Set

SADD
This command adds a new element or elements to the set e.g SADD cities Lagos Ibadan Accra will add three elements to the cities set.

This command only adds unique elements to the set, so for example, if we have repetitive numbers in an SADD command, only one of the repeated numbers will be added to the set provided the number does not already exist in the set.

SMEMBERS
This is used to retrieve all the elements of a set. This command is a total blocking command that will only release system resources until all the elements of the set have been retrieved.

smembers cities

Alt Text

SSCAN
This is also similar to SMEMBERS command but only that this command runs iteratively or asynchronously without holding on to the system resources until all elements of the set have been retrieved.

SCARD
This command is used to get the length of a set or just the total number of elements in a set.

scard cities

SREM
This command is used to remove one or more elements from a set by the value.

SREM cities Accra

Alt Text

The full list and detailed explanation of various Redis Set commands can be found here

Sorted Set

This is basically a set only that a score is assigned to unique elements as a form of ordering so this means sorted set can be unofficially called an ordered set.
If two elements in a sorted set have the same score, then the tie is broken by the lexical order of the elements in the set.

Note:
The score associated with each element is a floating-point number so even if an integer value is provided as the score of an element, it will be automatically converted to a float.

Common commands and operations in Redis Sorted Set

ZADD
This is used to add an element or elements along with their score to the set.

zadd student-score 98 Michael 82 Bisi 78 Hamdallah 85 Chigozie

ZRANGE
This retrieves the elements of a sorted set

zrange student-score 0 -1

Alt Text

ZRANGEBYSCORE
This returns elements of the set based on their score. For example, suppose we want to find the students who scored 90 and above we can do it by using the zrangbyscore command as shown below:

zrangebyscore student-score 90 +inf

We can use -inf and +inf to denote a range especially when we don't know the bound of values.

ZSCORE
To get the score of an element in a sorted set, we use the zscore command. Suppose we want to get the score of Hamdallah in our sorted set we use the zscore command to achieve that as follows:

zscore student-score Hamdallah

Alt Text

The full list and detailed explanation of various Redis Sorted Set commands can be found here

Conclusion

The knowledge of the Redis data structure is very vital to building a very efficient Redis based applications. Redis is fast and very efficient but with appropriate use and application of its data structures.

If you find this article interesting, don’t forget to drop a ❤️ and use the comment section for any further comments.

Oldest comments (0)