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. ScaleGrid’s hosting for Redis™* allows you to automate your time-consuming operations for a variety of different Redis use cases. In this blog, we outline the top Redis use cases by the different core data structure types.
Data Structures in Redis
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.
Here are some of our introductory posts in the Redis data structures series:
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:
Redis Strings Use Cases
- Session Cache: Many websites leverage Redis Strings to create a session cache to speed up their website experience by caching HTML fragments or pages. Since data is stored temporarily in the RAM, this attribute makes Redis a perfect choice as a session cache. It is able to temporarily store user-specific data, for instance, items stored in a shopping cart in an online store, which is crucial in that your users do not lose their data in the event they log out or lose connection.
- Queues: Any application that deals with traffic congestion, messaging, data gathering, job management, or packer routing should consider a Redis Queue, as this can help you manage your queue size by rate of arrival and departure for resource distribution.
- Usage & Metered Billing: A lesser known use case for Redis Strings is the real-time metering for consumption-based pricing models. This allows SaaS platforms that bill based on actual usage to meter their customers activity, such as in the telecommunications industry where they may charge for text messages or minutes.
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)
Redis List Use Cases
- Social Networking Sites: Social platforms like Twitter use Redis Lists to populate their timelines or homepage feeds, and can customize the top of their feeds with trending tweets or stories.
- RSS Feeds: Create news feeds from custom sources where you can pull the latest updates and allow interested followers to subscribe to your RSS feed.
- Leaderboards: Forums like Reddit and other voting platforms leverage Redis Lists to add articles to the leaderboard and sort by most voted entries.
Learn how to build your own Twitter feed in our Caching tweets using Node.js, Redis and Socket.io blog post.Top #Redis Use Cases by Core Data Structure TypesClick To Tweet
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 techniques 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 the most, and which hours record the highest sales.
Redis Sets Use Cases
- Analyzing Ecommerce Sales: Many online stores use Redis Sets to analyze customer behavior, such as searches or purchases for a specific product category or subcategory. For example, an online bookstore owner can find out how many customers purchased medical books in Psychology.
- IP Address Tracking: Redis Sets are a great tool for developers who want to analyze all of the IP addresses that visited a specific website page or blog post, and to be able to ignore all of the duplicates for unique visitors with their SADD function.
- Inappropriate Content Filtering: For any app that collects user input, it’s a good idea to implement content filtering for inappropriate words, and you can do this with Redis Sets by adding words you’d like to filter to a SET key and the SADD command.
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 Sorted Sets Use Cases
- Q&A Platforms: Many Q&A platforms like Stack Overflow and Quora use Redis Sorted Sets to rank the highest-voted answers for each proposed question to ensure the best quality content is listed at the top of the page.
- Gaming App Scoreboards: Online gaming apps leverage Redis Sorted Sets to maintain their high score lists, as scores can be repeated, but the strings which contain the unique user details cannot.
- Task Scheduling Service: Redis Sorted Sets are a great tool for a task scheduling service, as you can associate a score to rank the priority of a task in your queue. For any task that does not have a score noted, you can use the WEIGHTS option to a default of 1.
- Geo Hashing: The Redis geo indexing API uses a Sorted Set for the Geo Hash technique which allows you to index locations based on latitude and longitude, turning multi-dimensional data into linear data.
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 Hashes Use Cases
- User Profiles: Many web applications use Redis Hashes for their user profiles, as they can use a single hash for all the user fields, such as name, surname, email, password, etc.
- User Posts: Social platforms like Instagram leverage Redis Hashes to map all the archived user photos or posts back to a single user. The hashing mechanism allows them to look up and return values very quickly, fit the data in memory, and leverage data persistence in the event one of their servers dies.
- Storing Multi-Tenant Metrics: Multi-tenant applications can leverage Redis hashes to record and store their product and sales metrics in a way that guarantees solid separation between each tenant, as hashes can be encoded efficiently in very small memory space.
Who uses Redis?
Redis has found a huge market share across the travel and hospitality, community forums, social media, SaaS, and e-commerce 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:
- 4,107 companies reported using Redis on StackShare
- 8,759 developers stated using Redis on StackShare
- 38,094 GitHub users have starred Redis
- #8 ranked database on DB-Engines with a score of 144.08