Advertisement
Advanced Redis, Sets & Sorted Sets in Redis

How would you use sorted sets to implement a simple tag based content recommendation system, finding content similar to a given item?

Short answer

You could store each piece of content's tags in a set, and to find similar content, calculate the intersection between a target item's tag set and the tag sets of other candidate items, using the size of that intersection as a similarity score, then use a sorted set to rank…

Save Redis mock interview
Advertisement
AModel answer
You could store each piece of content's tags in a set, and to find similar content, calculate the intersection between a target item's tag set and the tag sets of other candidate items, using the size of that intersection as a similarity score, then use a sorted set to rank candidates by how many tags they share with the target item.
Example

SINTERSTORE temp_similarity item1_tags item2_tags ZADD similarity_scores <intersection_size> 'item2'

Real-world example
A content platform recommends articles to readers by calculating tag overlap between the article they just read and other available articles, ranking recommendations using a sorted set based on how many tags each candidate shares with the original.
Likely follow-ups
  • How does this simple tag based approach compare to more sophisticated recommendation algorithms?
  • How would you scale this approach for a catalog with millions of items?
Related topics