Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active December 8, 2024 20:25
Show Gist options
  • Select an option

  • Save wilmoore/46c2825b82f8b2c8b37314fb7a3060f2 to your computer and use it in GitHub Desktop.

Select an option

Save wilmoore/46c2825b82f8b2c8b37314fb7a3060f2 to your computer and use it in GitHub Desktop.
Software Engineering :: Database :: Redis :: Data Structure :: Sorted Sets (ZSet)

Software Engineering :: Database :: Redis :: Data Structure :: Sorted Sets (ZSet)

⪼ Made with 💜 by Polyglot.

A sorted set is an ordered collection of unique members and an associated score.

  • Priority Queues
  • Low-Latency Game Leaderboard (i.e. experience points)
  • Secondary Indexing

Benefits

  • Responsive
  • In-Memory Structure (Low-Latency)

Commands

  • ZADD: Adds all the specified members with the specified scores to the sorted set stored at key.
  • ZINCRBY: Increments the score of member in the sorted set stored at key by increment.
  • ZRANK: Returns the rank of member in the sorted set stored at key, with the scores ordered from low to high. The rank (or index) is 0-based, which means that the member with the lowest score has rank 0.
  • ZREVRANK: Returns the rank of member in the sorted set stored at key, with the scores ordered from high to low. The rank (or index) is 0-based, which means that the member with the highest score has rank 0.
  • ZRANGE: Returns the specified range of elements in the sorted set stored at <key>.
  • ZSCORE: Returns the score of member in the sorted set at key.
ZADD <KEY> <SCORE> <MEMBER>
ZINCRBY <KEY> <INCREMENT> <MEMBER>
ZRANGE <KEY> <START> <END> REV BYSCORE
ZRANK <KEY> <MEMBER> [WITHSCORE]
ZREVRANK <KEY> <MEMBER> [WITHSCORE]
ZSCORE <KEY> <SCORE>

Leaderboard Goals

  • Display the TOP 10 players
  • See a player's rank
  • See a player's score

YouTube Research


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment