The Challenge

ShopStream is a fast-growing marketplace with hundreds of sellers competing for visibility. The platform needs a real-time seller leaderboard that ranks sellers by total revenue. Every order updates the rankings instantly, and the leaderboard resets each month.

Traditional databases are slow for this. Every query would scan thousands of seller records and compute ranks from scratch. Redis sorted sets are built for this exact problem: members with scores, automatically sorted, with O(log N) insert/query performance.

In this lesson, you'll build a SellerLeaderboard class that manages rankings using ZADD, ZINCRBY, and ZREVRANGE commands.

Key insight: Redis sorted sets store (member, score) pairs and automatically maintain them in sorted order. Querying the top-N sellers doesn't require sortingβ€”it's instant.

Redis Sorted Sets Crash Course

A Redis sorted set is like a set with scores. Each member has a unique key and a numeric score. Members are ordered by score automatically.

python

Building the SellerLeaderboard Class

Let's create a class that wraps Redis operations and provides a clean API for leaderboard management.

Part 1: Initialization and Basic Operations

python

Part 2: Advanced Features

Add methods for filtering, analytics, and management:

python

Export and Import Leaderboard Data

Save leaderboard snapshots to JSON and restore them later:

python

ShopStream Orders Integration

Integrate the leaderboard with order processing:

python

Running the Complete Demo

Put it all together with a full main block that simulates orders and displays the leaderboard:

python

Expected Output

=== Adding Sellers ===
Added 4 sellers

=== Simulating Orders ===
Processed 10 orders

=== Top Sellers ===
1. seller:5: $1850
2. seller:7: $1700
3. seller:3: $850

=== Seller Metrics ===
seller:5 - Rank: 1, Revenue: $1850

=== Leaderboard Size ===
Total sellers: 4

=== Sellers Above $1000 ===
seller:5: $1850
seller:7: $1700

=== Leaderboard Stats ===
total_sellers: 4
total_revenue: 5250
avg: 1312
min: 850
max: 1850

=== Revenue Distribution ===
seller:5: 35.2%
seller:7: 32.4%
seller:3: 16.2%
seller:2: 16.2%

=== Export to JSON ===
Leaderboard exported (first 100 chars):
{"timestamp": "2026-04-16T14:30:00.123456", "sellers": [{"seller_id": "seller:5"...

=== Reset Leaderboard ===
Deleted: 1
Remaining sellers: 0

Why Sorted Sets Are Perfect for Leaderboards

  • O(log N) insert: Adding revenue doesn't require re-sorting. ZINCRBY is instant.
  • O(log N) query: Getting the top-N sellers doesn't require scanning all sellers.
  • No storage overhead: Sorted sets are Redis' most efficient data structure for rankings.
  • Atomic operations: ZINCRBY is atomicβ€”no race conditions when multiple orders arrive.
  • Range queries: ZREVRANGEBYSCORE lets you filter by score (e.g., sellers above $1000).
  • TTL support: Set EXPIRE to auto-reset the leaderboard monthly.

Test Your Knowledge

Question 1 of 5
Why are Redis sorted sets perfect for leaderboards?
Question 2 of 5
How do you increment a seller's score in a sorted set?
Question 3 of 5
What's the difference between ZREVRANK and ZRANK?
Question 4 of 5
How would you implement a monthly leaderboard reset?
Question 5 of 5
What does ZREVRANGEBYSCORE do in a leaderboard query?
0
/5
Africa & India's Best AI Academy
πŸ€– AI-first πŸŽ“ Placement Assistance

You're building real-time leaderboards. Take the next step.

Join the Best AI Program: The AI-Era Data Engineer β€” 5 months, fully online, with placement assistance. Week 1 is free.

Explore The AI-Era Data Engineer β†’