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:
- 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.
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)
- 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.
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.
- 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.
- 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.
- 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 a very small memory space.