accesspatterns.dev

social

Game Leaderboard

Per-game high scores, ranked with a secondary index on score.

leaderboardgsiranking

Scores live under the game partition, and a GSI re-keys them by score so the top of the table is a single descending Query on the index.

The model

Score

A player's best score in a game.

pk
GAME#<gameId>
sk
PLAYER#<playerId>

Attributes: gameId (S), playerId (S), player (S), score (N)

Access patterns

  • Query · GSI1Top scores for a game

    List the highest scores in a game, ranked.

  • GetItemGet a player's score

    Fetch one player's score in a game.

Design notes

A GSI to rank, not to look upwhy

The base table answers get-a-player's-score by key. Ranking needs a different sort order, so a GSI re-keys every score under one partition per game, and the whole leaderboard is a single Query on the index.

Negate the score to rank descendinghow

A Query reads a partition in ascending sort-key order, but a leaderboard wants the highest score first. So the index sort key holds the negated score: ascending order over -score puts the biggest score at the top. The real, positive score stays in its own attribute for display.

resolving the model…

The in-browser engine is a preview; behaviour is illustrative and not conformance-verified against AWS.