Redis Data Types – Tutorial with Commands (Examples)

Redis Data Types – Tutorial with Commands (Examples). Written in C programming language, Redis ( short for Remote Dictionary Serve ) is a free and open source, key value data store and in memory database that stores data in RAM. Importantly, it’s a hugely popular NoSQL database renowned for its caching abilities , rich data types and incredibly fast read and write speeds. Furthermore, its  key value data model makes for an impressive database, caching server and a message broker.

Generally, since data is stored on RAM,  Redis provides remarkably fast read and write speeds compared to traditional database solutions such as MongoDB.  Well, its key value architecture allows it to handle large datasets while maintaining high availability and scalability. As such, it is an ideal database and caching solution for modern and complex microservice applications.

In this tutorial, we explore Redis Data Types – Tutorial with Commands for handling each data type.

How does Redis Work?

Primarily, as an in memory database store, Redis primarily stores data on the random access memory (RAM) instead of writing it to disk. However, this comes at the expense of cost and security. RAM is quite expensive compared to hard disk storage and is highly volatile. In addition, data is lost every time the system powers off or crashes.

In order to address this issue, each entry in memory is assigned a unique key that is used to access the data record on the hard drive. In addition, you configure persistence on Redis. This implies configuring Redis to write data on durable storage such as HDD or SDD. Check out the range of persistence options offered by Redis

Benefits of Redis

Having briefly looked at Redis and what is it, let us explore some of the key benefits it offers:

High availability and Scalability

Hence, the in memory database for Redis offers high availability by replicating a primary Redis node to one or more replica nodes in a cluster. Any changes made to the data on the primary node are copied to the replica using the Redis asynchronous replication protocol. Therefore, it allows developers to build highly available and reliable applications. Allows you to scale up or scale down the cluster size as per your needs.

Simplicity and Ease of Use

Unlike traditional databases where code to store and access data is complex, Redis allows you to write the same code in fewer and simpler lines. This allows developers to leverage a simple command structure as opposed to query languages used by traditional database solutions.

Support For Multiple Programming Languages

The in memory database and caching server supports most leading programming languages including Python, Java, C, C++, Go, PHP, JavaScript and Node.JS

Redis is Opensource

Indeed, Redis is open source and prides itself in having a vibrant community of developers. Hence there’s no technology or vendor lock in. This makes it accessible to the community without any technological or licensing hurdles.

Next section of the article Redis Data Types – Tutorial with Commands  is to learn about Use cases.

Redis Use Cases

Let us now look at some of the popular use cases of Redis.


Likewise, Redis ranks as one of the best caching solutions for applications and microservices. Markedly, it handles millions of requests per second, and by so doing, offers high availability and scalability. As a cache, it takes off the load on traditional databases.

Chat and messaging

Moreover, Redis supports the pub/sub model along with  data structures such as lists, hashes and sorted sets. This along with super fast read and write speeds make Redis a perfect solution for high performance chat rooms, social media feeds and real time comment streams. The list data structure enables it to underpin a lightweight queue.

Rich Media Streaming

It provides a remarkably fast and powerful in memory data store for live streaming. The data store is robust enough to store tons of metadata about users’ profiles, authentication information for millions of users, as well as manifest files to enable CDNs to stream videos to millions of user whether on desktop or mobile devic

Real Time Analytics

Redis has become an excellent solution for real time analytics. It delivers sub millisecond response times, which are ideal for fast and powerful real time applications in gaming, fintech, social media, gaming and IoT to mention a few.

We have arrived to the main part of the article Redis Data Types – Tutorial with Commands (Examples).

Redis Data Types

Redis  supports a myriad of data types. These include:

  1. Strings
  2. Sets
  3. Sorted Sets
  4. Lists
  5. Hashes
  6. HyperLogLogs

Let us go over each of these and see how you can store and retrieve data.


Strings are the most basic Redis data type. They store sequences of bytes such as text, binary arrays and serialized objects. The maximum size of  a string value is 512 MB. This accounts for both the key and the value  in the key value pair. Hence, string operations are quite fast since all the data is contained in a single object. 


Here, Redis uses the following syntax with strings.

					COMMAND  key value

Redis offers the following basic commands with strings: SET, GET and DEL

SET key value – Sets the value for a given key.

GET key – Retrieves the value for a set key.

DEL key – Removes or deletes the value for the given key.

Below are a few command examples of how you use Redis commands with strings in the Redis CLI.

As explained earlier, SET specifies the value of a key, GET retrieves the value assigned to the key and displays it. Lastly, DEL deletes the value of a key. If a key contains an empty value, running the GET command yields (nil) as the output.

Interestingly, strings are mostly used for caching static HTML pages, session IDs, configuration XML, and so much more.


Overall, in Redis, lists are lists of strings sorted by insertion order. You add elements to a list by either pushing items to the front or the back of the list using the LPUSH/RPUSH commands respectively. In addition you pop items from the front and back of the list with LPOP/RPOP.


Just like strings, Redis lists take the following format.

					COMMAND key value

Here are the basic commands that work with Redis lists.

LPUSH – Adds  a value at the left end or the start of a list.
RPUSH – Adds a value at the tail end of the list.
LRANGE – Displays/fetches the items in the list
LPOP – Removes an item at the beginning of the list
RPOP – Removes an item at the tail end of the list
LINDEX – Retrieves and displays a value from a specific position within the list


Each time you add values to a list with LPUSH/RPUSH commands, the output displays the current position of the item in the list. For example, (integer) 1 implies that the item is first on the list, and (integer) 2 shows the item is the second and so forth.

The LINDEX command enables you to retrieve a specific value from the list.

The LPOP/RPOP commands remove an item on the list and the output displays the item that has been removed.

When using LRANGE command to retrieve the values in the list, the integer 0 marks the start of the first index value while -1 signifies the last index item.

If you add the same item multiple times in a list, you will invariably end up with a single copy. In fact, the output of the SADD command is always be (integer) 0 indicating that the item was not added since it was a duplicate entry.

Here are some  examples of Redis commands with lists. The key or  list name is car-list.

Since lists are basically a linked list of strings, they are ideal for storing real time data such as logs, social media updates etc.


A set is an unordered collection of unique strings, Being unique, there’s no chance of a string appearing more than once. Particularly, sets are mostly used to track unique items such as ID numbers, IP addresses accessing a website and so on.


The command for Redis sets takes the following format.

					COMMAND key value

Here are some of the commands you can use to add, remove, retrieve, and inspect individual items of a set:

SADD – Adds or appends one or multiple items to a set.
SREM – Removes or purges an existing item from a set.
SMEMBERS – Displays all items from a set.
SISMEMBER – Probes to see if an item is part of a set.

Below is an example of Redis commands with sets. The set name in this example is ‘continent‘.

Since sets do not store duplicate values, they are ideal for storing unique values such as public IP addresses accessing a website, domain names etc.

Sorted Sets

Sorted sets are quite similar to ordinary sets.  The only difference is that the value part of the key value pair comprises  an item ( value ) called a score and a unique string element known as a member.  Each member is associated with a score. A score is a floating point number that provides a sorting order for the sorted set.

In order to get a better understanding of sorted sets, we have an imaginary table with students scores. The left column displays the student scores while the right indicates the names associated with the scores

We see that the above table has been sorted in ascending order based on the scores. Only the member in the score member pair is unique. Scores can be duplicated.

If two members shared the same score, Redis sorts them on based on a lexicographical order. For example, if Patricia and Alice have the same score, Redis places Alice before Patricia since ‘A’ comes before ‘P’ in the alphabetical order.


Sorted Sets take the following syntax

					COMMAND key score member

There are various ways of accessing items contained in a sorted set. You access them using the member, score values or sorted order. Here are basic commands that allows you to retrieve add, remove individual values, or retrieve items based on member values and score ranges.

ZADD – Adds a member with a score to the sorted set.

ZRANGE – Fetches elements in the sorted set based on their position in the sorted set. Use the withscores option to display the actual score values.

ZRANGEBYSCORE – Retrieves elements in the sorted list based on the specified score range. Invoke the withscores option to display the actual score values.
ZREM – Removes items from a sorted set.

In essence, sorted sets are used to update a real time leader board as players’ scores change.


Hashes in Redis are data structures used to store unordered key value pairs. In effect, a hash key is associated with a value which is a string that stores a key value pair. Basically, hashes are used to map string fields to string values. They are mutable, and hence , can be changed based as per your requirements.


In case of Redis, the hashes take the following syntax:

					COMMAND hash:key keyOne valueOne keyTwo valueTwo keyThree ValueThree ... 

Below are some of the basic hash commands that allow you to add, retrieve and modify individual or multiple fields.

HSET – Maps a value to a key in the hash.
HGET – Displays values associated with each key within the hash.
HGETALL – Displays all the keys and corresponding values in the hash
HDEL – Deletes or removes an existing key-value pair from a hash.

When adding an item to the hash with the HSET command, a return value (integer) is printed on the shell, informing you of the number of instances added. The same information is provided using the HDEL command.

Specifically, hashes store billions of object data very seamlessly. As such, they are ideal for storing a large collection of user data or individual objects.


Subsequently, Redis Hyperloglog is a probabilistic data structure that counts the number of unique elements in a set using a small, constant amount of memory. In so doing, it provides efficient space utilization with a standard error of only 0.81%.

The algorithm used by HyperLogLog works on uniformly distributed random numbers and estimates the count of unique items in a set.

Hyperloglog uses the following commands to add and manage items.

PFADD – Adds one or multiple items to a HyperLogLog.
PFCOUNT – Returns an estimate of the number of items in the HyperLogLog
PFMERGE – Merges two or more Hyperloglogs into one

Lastly, Hyperloglogs are used to aggregate and count queries or unique user interactions.

Thank you for reading Redis Data Types – Tutorial with Commands (Examples). We will summarize this article blog. 

Redis Data Types – Tutorial with Commands (Examples). Conclusion

Evidently, there are various data types ideal for different use cases. These data types offer the much needed flexibility for storing a myriad of data types and, by so doing, transform Redis into a formidable cache and in memory database for handling large and complex data sets.

To read more about Redis, please navigate to this section of our blog. 

Avatar for James Kiarie
James Kiarie

Hello everyone! My name is James, a certified Linux Administrator, and a tech enthusiast with over 5 years of experience in penning down high-quality guides on Linux and Cloud technologies. Outside work hours I enjoy working out, swimming, listening to music, and reading fiction novels.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x