CHAPTER 03
Intermediate
Redis Data Structures Explained | Strings, Lists, Sets & Hashes
Updated: May 16, 2026
15 min read
# CHAPTER 3
Understanding Redis Data Structures
1. Introduction
If Redis was just a simple dictionary that storedKey="name", Value="John", it would be useful, but not revolutionary. The true power of Redis lies in the fact that it is a Data Structure Server. The Value you store doesn't have to be a simple string of text. The value can be a massive List, a Hash Map, or a mathematically Sorted Set. By storing complex data structures directly in RAM, developers can solve massive computational problems instantly. In this chapter, we will survey the 5 foundational structures of Redis.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define the concept of a Data Structure Server.
- Understand Redis Strings.
- Understand Redis Lists (Queues).
- Understand Redis Sets (Unique Collections).
- Understand Redis Hashes (Objects).
- Match specific software problems to the correct data structure.
3. Structure 1: Strings
This is the most basic and common data type. A String in Redis is binary safe, meaning it can contain anything—text, numbers, or even a JPEG image—up to 512 Megabytes in size!-
Use Case: Caching a massive block of HTML for a website, or storing a simple counter (e.g.,
page_views = 1500).
-
Example:
SET user:1:name "Alice"
4. Structure 2: Lists
A Redis List is a linked list of strings. You can push new elements to the absolute front (head) or the absolute back (tail) of the list in microseconds, regardless of whether the list has 10 items or 10 million items.- Use Case: Building a Background Job Queue (e.g., a list of emails waiting to be sent), or storing the latest 100 comments on a blog post.
-
Example:
LPUSH recent_visitors "Alice", thenLPUSH recent_visitors "Bob". (Bob is now at the front).
5. Structure 3: Sets
A Set is an *unordered* collection of strings. The defining mathematical feature of a Set is that it does not allow duplicates. If you try to add "Alice" to a Set 50 times, the Set will still only contain one "Alice". Furthermore, checking if an item exists inside a Set is blisteringly fast.- Use Case: Tracking unique website visitors. (e.g., Add IP addresses to a Set. If the IP is already there, Redis ignores it, leaving you with a mathematically perfect count of unique humans).
-
Example:
SADD unique_ips "192.168.1.1"
6. Structure 4: Sorted Sets
A Sorted Set is exactly like a regular Set (no duplicates allowed), but every item in the set is associated with a number called a Score. Redis automatically uses this score to keep the set perfectly sorted from lowest to highest at all times.-
Use Case: Real-time Gaming Leaderboards. (The item is the "Username", the Score is their "Points"). As players earn points, Redis instantly re-sorts the global ranking without any complex SQL
ORDER BYqueries!
-
Example:
ZADD global_leaderboard 5000 "Gamer123"
7. Structure 5: Hashes
A Hash is a map between string fields and string values. It is exactly like a JSON object or a PHP Associative Array. Instead of storing a user's data as one giant string, you store it as fields inside a single Redis Key.- Use Case: Storing User Profiles or User Sessions.
-
Example: Key:
user:99. Fields:name="Alice",age="25",status="Online". You can update *just* the age without downloading the whole object!
8. The Memory Model (Pointers)
Why are these structures so fast? In a traditional database, sorting a leaderboard requires reading rows off a hard drive and comparing them. In Redis, a Sorted Set is maintained using advanced in-memory C-language pointers (Skip Lists). When a score changes, Redis just shifts a pointer in RAM. It requires almost zero CPU power.9. Common Mistakes
-
Using Strings for Everything: A developer wants to store a user's profile. They encode the data into a giant JSON string:
{"name":"John", "age":30}and store it as a Redis String. Later, they want to change the age to 31. They must download the entire JSON string, decode it in their backend, change the number, re-encode it, and send it back to Redis.
HINCRBY user:1 age 1 to instantly change the age directly inside the database!
10. Best Practices
-
Namespace Your Keys: Redis does not have "Tables". All keys are thrown into one giant bucket. Therefore, you must use colons
:to organize your keys. Instead of naming a keyalice, name ituser:101:profile. This creates a logical, readable hierarchy.
11. Exercises
- 1. Which Redis data structure automatically prevents duplicate entries?
- 2. Which Redis data structure is explicitly designed to maintain a real-time ranked list based on a numerical score?
12. Redis Challenges
You are building an E-Commerce site. You need a system to track a "Shopping Cart". The cart must hold multiple items (like "Laptop", "Mouse"), but it shouldn't hold the same item twice (if they add a second mouse, you just update a quantity, not the item list). Order doesn't matter. Which data structure is best? *(Answer: A Redis Hash. The Key is thecart:user_id. The fields are the product_ids, and the values are the quantities. e.g., HSET cart:1 laptop 1)*
13. MCQ Quiz with Answers
Question 1
What is the fundamental difference between a regular Redis Set and a Redis Sorted Set?
Question 2
When architecting an application that requires a Background Job Queue (e.g., a system where web requests are placed in a line, and a background worker processes them one by one in the exact order they arrived), which Redis data structure is the optimal choice?
14. Interview Questions
- Q: Explain why storing a JSON object as a single Redis String is generally considered an anti-pattern compared to utilizing a Redis Hash, specifically regarding network bandwidth and atomic updates.
- Q: You are tasked with building a real-time Top 10 Leaderboard for a mobile game with 5 million active players. Explain exactly which Redis data structure you would use and why it mathematically outperforms a SQL database for this specific task.
15. FAQs
Q: Does Redis support geospatial data (GPS coordinates)? A: Yes! Redis has aGEO API. You can add longitudes and latitudes, and ask Redis to instantly find "all restaurants within a 5-mile radius." Behind the scenes, the GEO API actually uses a Sorted Set to do the math!