[Fixed] Architecture: Creating a time filterable leaderboard system with mongoose

Issue

This question is more of a architecture based one.
I have a website which has 3 PVP (player vs player) games. Each game has it’s own mongoDB collection
and it’s documents have properties such as timestamp, amount won (points) and players involved.

I want to create a leaderboard system that retrieves data from all these 3 games, and shows who has won the most in a top 10 kind of style. This system will be accessed most likely through a HTTP end point. And I’d also like this leaderboard to be filterable by time: top 10 from the last week/month/year/all time

Problems:

As the user database has grown and more games have been created, Computing the table each time the end point is hit takes longer and longer. Page load times take a super long.

Initial Idea
enter image description here

Technologies

Mongoose, Express, Nuxt(Vue), Socket.io

Solution

I would suggest some sort of caching scheme. Two basic method I would consider:

  • Create a service that automatically tabulates the leaderboard and caches it or saves it to another mongo object. Clients are then served the cached version. This option is nice as it creates a historical record which could make for fun features in the future.
  • Cache the response in your express service and update it only on some frequency. As explained here: https://medium.com/the-node-js-collection/simple-server-side-cache-for-express-js-with-node-js-45ff296ca0f0 The risk with this method is if you have concurrent requests when the leaderboard is being generated it could hit your mongo server hard.

Without knowing all the details, I would go with the first option as it is immune to concurrent requests and could be extended in the future with some sort of historical leaderboard feature.

As for filtering, I’d recommend using the tables in vue-bootstrap. Data is easily represented in tables and sorting is built-in.

Leave a Reply

(*) Required, Your email will not be published