March 14, 2024
-
3
Min Read

When to use sorted sets vs Momento Leaderboards

Learn how sorted sets or Momento Leaderboards can be used for a variety of use cases.
Anita Ruangrotsakun
Headshot of the blog author
by
Anita Ruangrotsakun
,
,
by
Anita Ruangrotsakun
by
green squirrel logo for momento
Anita Ruangrotsakun
,
,
Leaderboards

What exactly is the difference between Momento Leaderboards and sorted sets? Why does Momento offer both collection types?

The table below illustrates the main differences between Momento Leaderboards and sorted sets. Leaderboards are more optimized for being a highly scalable, longer-lived collection compared to sorted sets as we can see from the unlimited time to live (TTL) and leaderboard size. There are also minor differences in the APIs, such as Leaderboards requiring numeric IDs and sorted sets providing an increment method, but there are workarounds for these differences. The key difference is in the expected longevity and growth of the collection.

chart comparing leaderboards and sorted sets

Note: at this time, these are temporary architectural limitations and we eventually expect to merge the capabilities of these two collection types. In the meantime, we should be discerning about selecting the best collection type for the use case.

Let’s walk through some use cases to get a feel for when sorted sets or Leaderboards will be most fitting.

Match Summaries

Say you run a gaming company with a popular competitive multiplayer game. Each match is capped to a limited number of players and at the end of each round, a match summary is displayed. Sorted sets would be perfectly suited to the engineering requirements for displaying these match summaries. The sorted set TTL need only span the length of the match and some buffer time after the game and the number of elements in the set will only ever be as large as the player cap for each match. Temporary ranked collections for a finite set of elements fall perfectly in the wheelhouse of sorted sets!

Season Rankings

In addition to the game, your company also publishes season rankings to encourage competition and reward the best players. The data collected for season rankings may span potentially millions of players over the course of several weeks. In this case, Leaderboards would be much better suited as they can handle the records for millions of players with TTLs much higher than for sorted sets. 

Online Voting

Now let’s say you run an entertainment company with an online voting system to engage your audience and allow them to cast votes for your company’s annual awards ceremony. In terms of engineering requirements, you want vote-count increments for a limited set of contestants for a limited duration of time. 

In this case, a sorted set would work perfectly! Contestants can be uniquely identified by their names and the increment API provides a low-latency method for all of your users to cast their votes. Once the contest ends, the sorted set can safely expire. Leaderboards would be a less natural fit for this use case as there is no element increment API and Leaderboards uses numeric IDs rather than string IDs.

chart displaying ranks for musicians with number of votes in favor

“Trending” Lists

Now consider another popular facet of your entertainment company: “trending” lists. These lists provide your audience with a snapshot of the hottest music, shows, podcasts, books, games, and other media across your industry. You determine the “trending score” of each item using a formula that takes a variety of factors into account (e.g. number of streams or sales) and regularly update the “trending” lists you publish online. 

In this scenario, Momento Leaderboards would be better suited for the job than sorted sets. A long-lived collection that can scale to tens of millions of records is really important here because new media is released every day and there’s no telling how the rankings may change over the course of weeks or months. Additionally, Leaderboards uniquely identify elements using a numeric ID instead of a string, so you’ll also avoid the problems around media items with the same name. 

Conclusion

In this article, we walked through a few sample use cases to illustrate the different strengths of Leaderboards and sorted sets. Sorted sets are great for a wide variety of applications involving temporary data, but Leaderboards excel when there is a need for longer-lived, highly scalable collections. 

Let us know your thoughts on how sorted sets or Leaderboards serve your use case!

Check out Momento Leaderboards and sorted sets on the docs website to learn more, and let us know what you think on Discord

Anita Ruangrotsakun
by
Anita Ruangrotsakun
,
,
by
Anita Ruangrotsakun
by
green squirrel logo for momento
by
Anita Ruangrotsakun
,
,
Author
Anita Ruangrotsakun

Anita is a software engineer on the Developer Ecosystem team at Momento, working to provide an excellent developer experience and help people build serverless applications. When she’s not working, Anita enjoys reading, building dynasties in the Sims 3, and finding new foodie spots in the PNW.

Author
Author
Open