Skip to main content
Redis Basics
CHAPTER 06 Intermediate

Redis Sets Tutorial | Building Real-Time Leaderboards

Updated: May 16, 2026
15 min read

# CHAPTER 6

Redis Sets and Sorted Sets

1. Introduction

Imagine you are building a website and want to track the IP addresses of every user who visits today. If you use a Redis List and a user refreshes the page 50 times, their IP address is added 50 times, ruining your analytics. You need a data structure that automatically rejects duplicates. Furthermore, imagine you are building a global gaming leaderboard. You need a structure that automatically sorts millions of players by their score in real-time. In this chapter, we will master Sets and Sorted Sets.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the mathematical properties of a Set.
  • Manage unique collections using SADD and SREM.
  • Understand Sorted Sets and the "Score" concept.
  • Build a real-time leaderboard using ZADD and ZREVRANGE.
  • Perform Set mathematics (Intersections).

3. Redis Sets (SADD and SMEMBERS)

A Redis Set is an unordered collection of Strings. It mathematically guarantees that no duplicates can exist.

Open your redis-cli:

bash
1
SADD unique_visitors "192.168.1.1"

*(Returns 1, meaning 1 item was added).*

bash
1
SADD unique_visitors "192.168.1.1"

*(Returns 0! Redis detected the duplicate and silently ignored the command. Your analytics are perfectly clean).*

To view all members of the Set:

bash
1
SMEMBERS unique_visitors

4. Fast Lookups with SISMEMBER

If a Set has 10 million IP addresses, and you want to check if a specific IP is banned, you do not need to download the whole list.
bash
1
SISMEMBER banned_ips "10.0.0.5"

*(Returns 1 if it is in the Set, 0 if it is not). This lookup happens in absolute microseconds.*

5. Redis Sorted Sets (ZADD)

A Sorted Set is exactly like a regular Set (no duplicates), but every single item is permanently tied to a floating-point number called a Score. Redis uses this score to keep the items mathematically sorted at all times.

Let's build a gaming leaderboard. The command is ZADD [key] [score] [value].

bash
123
ZADD global_leaderboard 1500 "Alice"
ZADD global_leaderboard 3000 "Bob"
ZADD global_leaderboard 500 "Charlie"

If Alice plays again and gets a higher score, you just run ZADD again. Redis will instantly overwrite her old score and visually move her up the leaderboard!

bash
1
ZADD global_leaderboard 4000 "Alice"

6. Reading the Leaderboard (ZRANGE)

To view the leaderboard, we use ZRANGE. By default, it sorts from Lowest to Highest.
bash
1
ZRANGE global_leaderboard 0 -1

*(Returns: Charlie, Bob, Alice).*

Usually, we want Highest to Lowest. We use ZREVRANGE (Reverse Range), and we can add WITHSCORES to see the actual points!

bash
1
ZREVRANGE global_leaderboard 0 -1 WITHSCORES

*(Returns: 1) "Alice" 2) "4000" 3) "Bob" 4) "3000" ...)*

7. Set Mathematics (SINTER)

Because Sets follow set theory, Redis can perform complex math on them instantly. Imagine you have two Sets: followers:alice and followers:bob. You want to find the "Mutual Friends" (people who follow both Alice AND Bob).
bash
1
SINTER followers:alice followers:bob

Redis compares the two lists and instantly returns only the names that exist in both Sets. Doing this in MySQL requires a horrific, CPU-intensive JOIN. In Redis, it is nearly instantaneous.

8. Mini Project: The Tagging System

Scenario: A blogging platform where users can filter articles by tags.
  1. 1. An article is published about "Databases" and "Redis".
  1. 2. Add the Article ID to a Set for each tag:
SADD tag:databases "Article_101" SADD tag:redis "Article_101"
  1. 3. A user clicks the "Redis" tag on the website.
  1. 4. Your backend simply runs SMEMBERS tag:redis and instantly retrieves all articles related to that topic!

9. Common Mistakes

  • Using SMEMBERS on massive sets: If your unique_visitors set has 50 million IP addresses, running SMEMBERS will force Redis to package all 50 million strings and send them to your terminal, likely crashing your application. Use the SSCAN command instead, which iterates through the set safely in small chunks.

10. Best Practices

  • Lexicographical Sorting: If you assign all items in a Sorted Set the exact same score (e.g., Score 0), Redis will automatically sort them alphabetically! This is an incredibly powerful trick for building "Auto-Complete" search boxes.

11. Exercises

  1. 1. What command do you use to add an item to a standard Set, ensuring duplicates are rejected?
  1. 2. In a Sorted Set, what is the numerical value called that Redis uses to automatically order the elements?

12. Redis Challenges

You are building an E-Commerce site and want to display a "Trending Products" list. Every time a product is viewed, its "View Count" goes up by 1. Describe the exact Redis data structure and commands you would use to maintain a real-time Top 10 list of the most viewed products. *(Answer: A Sorted Set. When a product is viewed, use ZINCRBY trending_products 1 "Product_ID". To display the Top 10 on the homepage, use ZREVRANGE trending_products 0 9).*

13. MCQ Quiz with Answers

Question 1

A web application tracks a massive list of malicious IP addresses. The backend must check if an incoming user's IP exists in this list in under 1 millisecond. Which Redis data structure and command provides the optimal, mathematically fastest solution?

Question 2

When architecting a real-time gaming leaderboard using a Redis Sorted Set (ZADD), what happens if a player who already exists in the Set earns a new, higher score?

14. Interview Questions

  • Q: Explain the mathematical concept of Set Intersection (SINTER). Provide a real-world software architecture example where utilizing Redis Sets for intersections is vastly superior to a Relational Database JOIN.
  • Q: Describe the internal architecture of a Redis Sorted Set (Skip Lists/Hash Tables). Why is it the undisputed industry standard for building real-time global leaderboards?

15. FAQs

Q: Are Sorted Sets limited to integer scores? A: No! The Score is a double-precision floating-point number. You can use decimals (e.g., ZADD leaderboard 99.5 "Alice"), which is perfect for incredibly precise timing or currency calculations.

16. Summary

You are now mastering advanced data architectures. By leveraging the deduplication power of Sets and the automatic, instantaneous ordering of Sorted Sets, you can build real-time analytics, tagging systems, and global leaderboards that SQL databases simply cannot handle.

17. Next Chapter Recommendation

Up to this point, we have been storing single strings or arrays. But what if we need to store a complex object, like a complete User Profile with a Name, Email, and Age? In Chapter 7: Redis Hashes and Object Storage, we will learn how to map structured data in memory.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·