User activity ranking system
In TechPi, a user activity leaderboard is provided. While a blog community would usually rank authors, we chose to highlight user activity to encourage greater participation. We provide daily and monthly leaderboard variants.
User activity score calculation rules:
- Visiting a new page: +1 point
- Liking or bookmarking an article: +2 points
Canceling a like/bookmark: −2 points
- Commenting on an article: +3 points
- Publishing an approved article: +10 points
Design
The leaderboard business logic is relatively straightforward, making data structure design simple as well.
Data model for a leaderboard entry:
long userId; // user identifier
long rank; // user's rank in the leaderboard
long score; // user's accumulated activity score
Initial data structure consideration:
A LinkedList
was considered since rankings are continuous and changes in position don’t require costly array copying. However, it has several downsides:
Problems with LinkedList
- Retrieving a user’s rank is inefficient (O(n))—random access is slow.
- Concurrency issues arise when multiple users update scores simultaneously.
Rather than building a custom structure from scratch, Redis provides an elegant and efficient solution using its ZSet (sorted set).
Redis-Based Approach
Redis’s ZSet (Sorted Set) is perfect for this use case:
- Ensures uniqueness of elements (users)
- Each element (user) has a score (activity)
- Maintains elements sorted by score
By using ZSet, we store user scores directly, and Redis handles the ranking automatically.