Top Redis Use Cases by Core Data Structure Types
Tags: redis, database, cache, webdev
Published: 2019-08-30 17:29:04

---
title: Top Redis Use Cases by Core Data Structure Types
published: true
description: ScaleGrid Blog: Top Redis use cases by core data structure type, including Redis Strings, Lists, Sets, Sorted Sets and Hashes. Used as a database, cache, message broker
tags: redis, database, cache, webdev,
canonical_url: https://scalegrid.io/blog/top-redis-use-cases-by-core-data-structure-types/
---
<link rel="canonical" href="https://scalegrid.io/blog/top-redis-use-cases-by-core-data-structure-types/" />

<a href="https://scalegrid.io/blog/top-redis-use-cases-by-core-data-structure-types/"><img src="https://scalegrid.io/blog/wp-content/uploads/2019/08/Top-Redis-Use-Cases-by-Core-Data-Types-ScaleGrid-Blog.jpg"></a>

<p style="text-align: justify;"><a style="color: #2da964;" href="https://scalegrid.io/redis.html">Redis</a>, 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 <a style="color: #2da964;" href="https://github.com/antirez/disque">message broker</a>. 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 <a style="color: #2da964;" href="https://scalegrid.io/blog/top-redis-use-cases-by-core-data-structure-types/">top Redis use cases</a> by the different core data structure types.</p>

<h2 style="padding-top: 10px;">Data Structures in Redis</h2>
<p style="text-align: justify;">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 <a style="color: #2da964;" href="https://scalegrid.io/blog/introduction-to-redis-data-structure-bitmaps/">bitmaps</a>, 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 <a style="color: #2da964;" href="https://www.eurovps.com/blog/best-mysql-gui-tools-roundup/">prefer GUI management systems</a>, for instance, phpMyAdmin for MySQL and PgAdmin for PostgreSQL.</p>
<p style="text-align: justify;">Let us take a closer look at the data types that exist in Redis.</p>

<h3 style="padding-top: 10px;">Redis Strings</h3>
<p style="text-align: justify;">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:</p>
<p style="text-align: justify;">To store a string ‘<b>john</b>’ under a key such as ‘<b>student’ </b>in Redis, run the command:</p>
<p style="text-align: justify; padding-left: 30px;"><tt>SET “student” “john”</tt></p>
<p style="text-align: justify;">To retrieve the string, use the GET command as shown:</p>
<p style="text-align: justify; padding-left: 30px;"><tt>GET “student”</tt></p>
<p style="text-align: justify;">To delete the string contained in the key use the DEL command:</p>
<p style="text-align: justify; padding-left: 30px;"><tt>DEL “student”</tt></p>

<h4 style="padding-top: 20px; font-size: 20px; padding-bottom: 5px;">Redis Strings Use Cases</h4>
<ol>
	<li style="text-align: justify; padding-bottom: 10px;"><b>Session Cache:</b> Many websites leverage Redis Strings to create a <a style="color: #2da964;" href="https://redis.io/topics/data-types-intro">session cache</a> 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.</li>
	<li style="text-align: justify; padding-bottom: 10px;"><b>Queues:</b> Any application that deals with traffic congestion, messaging, data gathering, job management, or packer routing should consider a <a style="color: #2da964;" href="https://www.infoworld.com/article/3230455/how-to-use-redis-for-real-time-metering-applications.html">Redis Queue</a>, as this can help you manage your queue size by rate of arrival and departure for resource distribution.</li>
	<li style="text-align: justify;"><b>Usage &amp; Metered Billing:</b> A lesser known use case for Redis Strings is the real-time metering for <a style="color: #2da964;" href="https://www.infoworld.com/article/3230455/how-to-use-redis-for-real-time-metering-applications.html">consumption-based pricing models</a>. 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.</li>
</ol>
<h3 style="padding-top: 10px;">Redis Lists</h3>
<p style="text-align: justify;">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 <a style="color: #2da964;" href="https://redis.io/commands/lpush">LPUSH</a> command to insert an element at the head, or left of the string, and the <a style="color: #2da964;" href="https://redis.io/commands/rpush">RPUSH</a> command to insert at the tail, or right of our string. Let’s look at an example:</p>
<p style="text-align: justify; padding-left: 30px;"><tt>LPUSH list x   # now the list is "x"</tt></p>
<p style="text-align: justify; padding-left: 30px;"><tt>LPUSH list y   # now the list is "y","x"</tt></p>
<p style="text-align: justify; padding-left: 30px;"><tt>RPUSH list z   # now the list is "y","x","z"</tt> (notice how the ‘z’ element was added to the end of the list by RPUSH command)</p>

<h4 style="padding-top: 20px; font-size: 20px; padding-bottom: 5px;">Redis List Use Cases</h4>
<ol>
	<li style="text-align: justify; padding-bottom: 10px;"><b>Social Networking Sites: </b>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.</li>
	<li style="text-align: justify; padding-bottom: 10px;"><b>RSS Feeds: </b>Create news feeds from custom sources where you can pull the latest updates and allow interested followers to subscribe to your RSS feed.</li>
	<li style="text-align: justify;"><b>Leaderboards:</b> Forums like Reddit and other voting platforms leverage Redis Lists to add articles to the leaderboard and sort by most voted entries.</li>
</ol>
<p style="text-align: justify;">Learn how to build your own Twitter feed in our <a style="color: #2da964;" href="https://scalegrid.io/blog/caching-tweets-using-node-js-redis-and-socket-io-2/">Caching tweets using Node.js, Redis and Socket.io</a> blog post.</p>

<h3 style="padding-top: 10px;">Redis Sets</h3>
<p style="text-align: justify;"><a style="color: #2da964;" href="https://scalegrid.io/blog/introduction-to-redis-data-structures-sets/">Redis Sets</a> 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 <a style="color: #2da964;" href="https://redis.io/commands/sadd">SADD</a> which avoids duplication of multiple similar entries. The <a style="color: #2da964;" href="https://scaleyourcode.com/blog/article/25">SADD attribute</a> can be employed when checking unique values, and can also for scheduling jobs running in the background, including cron jobs which are automated scripts.</p>
<p style="text-align: justify;">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.</p>

<h4 style="padding-top: 20px; font-size: 20px; padding-bottom: 5px;">Redis Sets Use Cases</h4>
<ol>
	<li style="text-align: justify; padding-bottom: 10px;"><b>Analyzing Ecommerce Sales:</b> Many <a style="color: #2da964;" href="https://scalegrid.io/blog/introduction-to-redis-data-structures-sets/">online stores</a> 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.</li>
	<li style="text-align: justify; padding-bottom: 10px;"><b>IP Address Tracking:</b> 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.</li>
	<li style="text-align: justify;"><b>Inappropriate Content Filtering:</b> For any app that collects user input, it’s a good idea to implement <a style="color: #2da964;" href="https://dzone.com/articles/how-to-use-redis-for-content-filtering">content filtering</a> 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.</li>
</ol>
<h3 style="padding-top: 10px;">Sorted Sets</h3>
<p style="text-align: justify;">As the name suggests, <a style="color: #2da964;" href="https://scalegrid.io/blog/introduction-to-redis-data-structures-sorted-sets/">Redis Sorted Sets</a> 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 <a style="color: #2da964;" href="https://developpaper.com/tips-for-using-redis-to-rank/">score</a>. Sorted Sets are known for being very fast, as you can return ordered lists and access elements in the shortest time possible.</p>

<h4 style="padding-top: 20px; font-size: 20px; padding-bottom: 5px;">Redis Sorted Sets Use Cases</h4>
<ol>
	<li style="text-align: justify; padding-bottom: 10px;"><b>Q&amp;A Platforms:</b> Many Q&amp;A platforms like <a style="color: #2da964;" href="https://redis.io/topics/whos-using-redis">Stack Overflow</a> 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.</li>
	<li style="text-align: justify; padding-bottom: 10px;"><b>Gaming App Scoreboards: </b><a style="color: #2da964;" href="https://www.ionos.com/community/hosting/redis/how-to-implement-a-simple-redis-leaderboard/">Online gaming apps</a> 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.</li>
	<li style="text-align: justify; padding-bottom: 10px;"><b>Task Scheduling Service:</b> Redis Sorted Sets are a great tool for a <a style="color: #2da964;" href="https://medium.com/@ApsOps/migrating-redis-sorted-sets-without-losing-data-f9e85f6549c5">task scheduling service</a>, 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.</li>
	<li style="text-align: justify;"><b>Geo Hashing:</b> The Redis geo indexing API uses a Sorted Set for the <a style="color: #2da964;" href="https://redis.io/topics/indexes">Geo Hash</a> technique which allows you to index locations based on latitude and longitude, turning multi dimensional data into linear data.</li>
</ol>
<h3 style="padding-top: 10px;">Redis Hashes</h3>
<p style="text-align: justify;"><a style="color: #2da964;" href="https://scalegrid.io/blog/introduction-to-redis-data-structures-hashes/">Redis Hashes</a> 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 <a style="color: #2da964;" href="https://redis.io/topics/data-types">232 - 1 field-value pairs</a> (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 <a style="color: #2da964;" href="https://redis.io/commands#hash">hash command</a> operations, such as get, set, exists, in addition to many advanced operations.</p>

<h4 style="padding-top: 20px; font-size: 20px; padding-bottom: 5px;">Redis Hashes Use Cases</h4>
<ol>
	<li style="text-align: justify; padding-bottom: 10px;"><b>User Profiles:</b> 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.</li>
	<li style="text-align: justify; padding-bottom: 10px;"><b>User Posts:</b> Social platforms like <a style="color: #2da964;" href="https://instagram-engineering.com/storing-hundreds-of-millions-of-simple-key-value-pairs-in-redis-1091ae80f74c">Instagram</a> 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.</li>
	<li style="text-align: justify;"><b>Storing Multi-Tenant Metrics: </b><a style="color: #2da964;" href="https://divinglaravel.com/introduction-to-redis-hashes">Multi-tenant applications</a> 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 a very small memory space.</li>
</ol>
<h3 style="padding-top: 10px;">Who uses Redis?</h3>
<p style="text-align: justify;">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 <a style="color: #2da964;" href="https://stackshare.io/redis">who use Redis</a> include Pinterest, Uber, Slack, Airbnb, Twitter, and Stack Overflow. Here are some stats on Redis popularity today:</p>

<ul>
	<li style="text-align: justify;">4,107 companies reported using Redis on <a style="color: #2da964;" href="https://stackshare.io/redis">StackShare</a></li>
	<li style="text-align: justify;">8,759 developers stated using Redis on <a style="color: #2da964;" href="https://stackshare.io/redis">StackShare</a></li>
	<li style="text-align: justify;">38,094 <a style="color: #2da964;" href="https://github.com/antirez/redis">GitHub</a> users have starred Redis</li>
	<li style="text-align: justify; padding-bottom: 20px;">#8 ranked database on <a style="color: #2da964;" href="https://db-engines.com/en/ranking">DB-Engines</a> with a score of 144.08</li>
</ul>