Daniel Barral
Daniel Barral

Reputation: 4096

Show top 10 scores for a game using DynamoDB

I created a table "scores" in DynamoDB to store the scores of a game.

The table has the following attributes:

Now I want to query the "top 10" scores for the game (leaderboard).

In DynamoDB, it is not possible to create an index without a partition key.

So, how do I perform this query in a scalable manner?

Upvotes: 2

Views: 2118

Answers (1)

Adi H
Adi H

Reputation: 738

No. You will always need the partition key. DynamoDB is able to provide the required speed at scale because it stores records with the same partition key physically close to each other instead of on 100 different disks (or SSDs, or even servers).

If you have a mature querying use case (which is what DynamoDB was designed for) e.g. "Top 10 monthly scores" then you can hash datetime into a derived month attribute like 12/01/2017, 01/01/2018, and so on and then use this as your partition key so all the scores generated in the same month will get "bucketized" into the same partition for the best lookup performance. You can then keep score as the sort key.

You can of course have other tables (or LSIs) for weekly and daily scores. Or you can even choose the most granular bucket and sum up the results yourself in your application code. You'll prob need to pull a lot more then 10 records to get close enough to 100% accuracy on the aggregate level... I don't know. Test it. I wouldn't rely on it if I were dealing with money/commissions but for game scores who cares :)

Note: If you decide to use this route then instead of using "12/10/2017" etc. as the partition values I'd use integer offsets e.g. UNIX epoch (rounded off to represent the first midnight of the month) to make it easier to compute/code against. I used the friendly dates to better illustrate the approach.

Upvotes: 2

Related Questions