type
status
date
slug
summary
tags
category
icon
password

Functional Requirements

  • Display top 10 players on the leaderboard.
  • Show a user’s specific rank.
 

Non-functional Requirements

  1. Low latency
  1. Scalability, availability

API Design

  • POST /v1/scores
Field
Description
user_id
The user who wins a game.
points
The number of points a user gained by winning a game.
  • GET /v1/scores
  • GET /v1/scores/{:user_id} fetch the rank of a specific user
 

Design

  • game server responsible for handling the game logic (win is valid and call leaderboard service)
  • Leaderboard service responsible for updating the store.
  • Message queue might be used, if the scores are used in many other different places.
notion image

Data Model

Choices for leaderboard store include:
  1. Redis (standard)
    1. sorted set (key:score, value: user_id) is designed for leaderboard
    2. the command is easy to use (get rank, update score)
  1. Mysql
    1. Did perform well when data is large (sort to get the rank)
    2. Not suitable for frequent update
    3. Latency is high, when the request load is high
  1. NoSQL
    1. Optimized for writes (Dynamo, Cassandra)
    2. Need to be able to do sort efficiently.

Scale Redis

Prefer sharding with fixed partition.
  • Use redis cluster automatic sharding feature. There is no total order guarantee in this sharding method, because it is sharding based on hash value. (In order to get the top 10, need to combine every partitions)
  • Fixed partition size, the total order in each partition is fixed, to get the top 10, always get the top 10 from one certain partition.