Marketing Manager at ScaleGrid with +10 years of experience working with high-tech startups.
Redis, short for Remote Dictionary Server, is a BSD-licensed, open-source in-memory key-value data structure store written in C language by Salvatore Sanfillipo and was first released on May 10, 2009. Depending on how it is configured, Redis can act like a database, a cache or a message broker. It’s important to note that Redis is a NoSQL database system. This implies that unlike SQL (Structured Query Language) driven database systems like MySQL, PostgreSQL, and Oracle, Redis does not store data in well-defined database schemas which constitute tables, rows, and columns. Instead, Redis stores data in data structures which makes it very flexible to use. In this blog, we outline the top Redis use cases by the different core data structure types.
Let’s have a look at some of the data types that Redis supports. In Redis, we have strings, lists, sets, sorted sets, and hashes, which we are going to cover in this article. Additionally, we have other data types such as bitmaps, hyperloglogs and geospatial indexes with radius queries and streams. While there are some Redis GUI tools written by the Redis community, the command line is by far the most important client, unlike popular SQL databases users which often prefer GUI management systems, for instance, phpMyAdmin for MySQL and PgAdmin for PostgreSQL.
Let us take a closer look at the data types that exist in Redis.
Redis Strings are the most basic type of Redis value leveraged by all other data structure types, and are quite similar to strings in other programming languages such as Java or Python. Strings, which can contain any data type, are considered binary safe and have a maximum length of 512MB. Here are a couple useful commands for Redis strings:
To store a string ‘john’ under a key such as ‘student’ in Redis, run the command:
SET “student” “john”
To retrieve the string, use the GET command as shown:
To delete the string contained in the key use the DEL command:
Lists contain strings that are sorted by their insertion order. With Redis Lists, you can add items to the head or tail of the lists, which is very useful for queueing jobs. If there are more urgent jobs you require to be executed, these can be pushed in front of other lower priority jobs in the queue. We would use the LPUSH command to insert an element at the head, or left of the string, and the RPUSH command to insert at the tail, or right of our string. Let’s look at an example:
LPUSH list x # now the list is "x"
LPUSH list y # now the list is "y","x"
RPUSH list z # now the list is "y","x","z" (notice how the ‘z’ element was added to the end of the list by RPUSH command)
Learn how to build your own Twitter feed in our Caching tweets using Node.js, Redis and Socket.io blog post.
Redis Sets are powerful data types that support powerful operations like intersections and unions. They are not in any order and are usually used when you want to perform an audit and see relationships between various variables. Sets are reasonably fast, and regardless of the number of elements you have stored, it will take the same time to add or remove items in a set. Furthermore, sets do not allow duplicate keys or duplicate members, so a key added multiple times in a set will simply be ignored. This is driven by a function called SADD which avoids duplication of multiple similar entries. The SADD attribute can be employed when checking unique values, and can also for scheduling jobs running in the background, including cron jobs which are automated scripts.
These are particularly helpful for analyzing real-time customer behavior for your online shopping site. For instance, if you’re running an online clothing store, Redis Sorted Sets employ relationship matching technique such as unions, intersections, and subtractions (commonly applied in Venn diagrams) to give an accurate picture of customer behavior. You can retrieve data on shopping patterns between genders, which clothes products sell more the most, and which hours record the highest sales.
As the name suggests, Redis Sorted Sets are a collection of strings that assign an order to your elements, and are one of the most advanced data structures in Redis. These are similar to Redis Sets, only that Sets have no order while Sorted Sets associate every member with a score. Sorted Sets are known for being very fast, as you can return ordered lists and access elements in the shortest time possible.
Redis Hashes are maps between string fields and string values. This is the go-to data type if you need to essentially create a container of unique fields and their values to represent objects. Hashes allow you to store a decent amount of fields, up to 232 – 1 field-value pairs(more than 4 billion), while taking up very little space. You should use Redis Hashes whenever possible, as you can use a small Redis instance to store millions of objects. You can use basic hash command operations, such as get, set, exists, in addition to many advanced operations.
Redis has found a huge market share across the travel and hospitality, community forums, social media, SaaS, and ecommerce industries to name just a few. Some of the leading companies who use Redis include Pinterest, Uber, Slack, Airbnb, Twitter, and Stack Overflow. Here are some stats on Redis popularity today:
Create your free account to unlock your custom reading experience.