👽
Software Engineer Interview Handbook
  • README
  • Behavioral
    • Useful Links
    • Dongze Li
  • Algorithm
    • Segment Tree
    • Array
      • Product Of Array Except Self
      • Merge Strings Alternately
      • Increasing Triplet Subsequence
      • String Compression
      • Greatest Common Divisor Strings
      • Max Product Of Three
      • Find Duplicate Num
      • Valid Palindrome Ii
      • Next Permutation
      • Rearrange Array By Sign
      • Removing Min Max Elements
      • Find Original Array From Doubled
      • Reverse Words Ii
    • Backtracking
      • Letter Combination Phone Number
      • Combination Sum Iii
      • N Queens
      • Permutations
      • Combination Sum
    • Binary Search
      • Koko Eating Bananas
      • Find Peak Element
      • Successful Pairs Of Spells Potions
    • Binary Search Tree
      • Delete Node In BST
      • Validate Bst
      • Range Sum Bst
    • Binary Tree
      • Maximum Depth
      • Leaf Similar Trees
      • Maximum Level Sum
      • Binary Tree Right Side
      • Lowest Common Ancestor
      • Longest Zigzag Path
      • Count Good Nodes
      • Path Sum III
      • Maximum Path Sum
      • Move Zero
      • Diameter Binary Tree
      • Sum Root Leaf Number
      • Traversal
      • Binary Tree Vertical Order
      • Height Tree Removal Queries
      • Count Nodes Avg Subtree
      • Distribute Coins
      • Binary Tree Max Path Sum
    • Bit
      • Min Flips
      • Single Number
      • Pow
      • Find Unique Binary Str
    • BFS
      • Rotten Oranges
      • Nearest Exist From Entrance
      • Minimum Knight Moves
      • Network Delay Time
      • Minimum Height Tree
      • Knight Probability In Board
    • Design
      • LRU Cache
      • Get Random
      • LFU Cache
      • Moving Average
      • Rle Iterator
      • Design Hashmap
    • DFS
      • Reorder Routes Lead City
      • Evaluate Division
      • Keys And Rooms
      • Number Of Provinces
      • Disconnected Path With One Flip
      • Course Schedule Ii
      • Robot Room Cleaner
      • Word Break Ii
      • Number Coins In Tree Nodes
      • Maximum Increasing Cells
      • Number Coins In Tree Nodes
      • Detonate Maximum Bombs
      • Find All Possible Recipes
      • Min Fuel Report Capital
      • Similar String Groups
    • DP
      • Domino And Tromino Tiling
      • House Robber
      • Longest Common Subsequence
      • Trade Stock With Transaction Fee
      • Buy And Sell Stock
      • Longest Non Decreasing Subarray
      • Number Of Good Binary Strings
      • Delete And Earn
      • Minimum Costs Using Train Line
      • Decode Ways
      • Trapping Rain Water
      • Count Fertile Pyramids
      • Minimum Time Finish Race
      • Knapsack
      • Count Unique Char Substrs
      • Count All Valid Pickup
    • Greedy
      • Dota2 Senate
      • Smallest Range Ii
      • Can Place Flowers
      • Meeting Rooms II
      • Guess the word
      • Minimum Replacement
      • Longest Palindrome Two Letter Words
      • Parentheses String Valid
      • Largest Palindromic Num
      • Find Missing Observations
      • Most Profit Assigning Work
    • Hashmap
      • Equal Row Column Pairs
      • Two Strings Close
      • Group Anagrams
      • Detect Squares
    • Heap
      • Maximum Subsequence Score
      • Smallest Number Infinite Set
      • Total Cost Hire Workers
      • Kth Largest Element
      • Meeting Rooms III
      • K Closest Points Origin
      • Merge K Sorted List
      • Top K Frequent Elements
      • Meeting Room III
      • Num Flowers Bloom
      • Find Median From Stream
    • Intervals
      • Non Overlapping Intervals
      • Min Arrows Burst Ballons
    • Linkedlist
      • Reverse Linked List
      • Delete Middle Node
      • Odd Even Linkedlist
      • Palindrome Linkedlist
    • Monotonic Stack
      • Daily Temperatures
      • Online Stock Span
    • Random
      • Random Pick With Weight
      • Random Pick Index
      • Shuffle An Array
    • Recursion
      • Difference Between Two Objs
    • Segment Fenwick
      • Longest Increasing Subsequence II
    • Stack
      • Removing Stars From String
      • Asteroid Collision
      • Evaluate Reverse Polish Notation
      • Building With Ocean View
      • Min Remove Parentheses
      • Basic Calculator Ii
      • Simplify Path
      • Min Add Parentheses
    • Prefix Sum
      • Find The Highest Altitude
      • Find Pivot Index
      • Subarray Sum K
      • Range Addition
    • Sliding Window
      • Max Vowels Substring
      • Max Consecutive Ones III
      • Longest Subarray Deleting Element
      • Minimum Window Substring
      • K Radius Subarray Averages
    • String
      • Valid Word Abbreviations
    • Two Pointers
      • Container With Most Water
      • Max Number K Sum Pairs
      • Is Subsequence
      • Num Substrings Contains Three Char
    • Trie
      • Prefix Tree
      • Search Suggestions System
      • Design File System
    • Union Find
      • Accounts Merge
    • Multithreading
      • Basics
      • Web Crawler
  • System Design
    • Operating System
    • Mocks
      • Design ChatGPT
      • Design Web Crawler
      • Distributed Search
      • News Feed Search
      • Top K / Ad Click Aggregation
      • Design Job Scheduler
      • Distributed Message Queue
      • Google Maps
      • Nearby Friends
      • Proximity Service
      • Metrics monitoring and alert system
      • Design Email
      • Design Gaming Leaderboard
      • Facebook New Feed Live Comments
      • Dog Sitting App
      • Design Chat App (WhatsApp)
      • Design Youtube/Netflix
      • Design Google Doc
      • Design Webhook
      • Validate Instacart Shopper Checkout
      • Design Inventory
      • Design donation app
      • Design Twitter
    • Deep-Dive
      • Back of Envelope
      • Message Queue
      • Redis Sorted Set
      • FAQ
      • Geohash
      • Quadtree
      • Redis Pub/Sub
      • Cassandra DB
      • Collaborative Concurrency Control
      • Websocket / Long Polling / SSE
    • DDIA
      • Chapter 2: Data Models and Query Languages
      • Chapter 5: Replication
      • Chapter 9: Consistency and Consensus
  • OOD
    • Overview
    • Design Parking
  • Company Tags
    • Meta
    • Citadel
      • C++ Fundamentals
      • 面经1
      • Fibonacci
      • Pi
      • Probability
    • DoorDash
      • Similar String Groups
      • Door And Gates
      • Max Job Profit
      • Design File System
      • Count All Valid Pickup
      • Most Profit Assigning Work
      • Swap
      • Binary Tree Max Path Sum
      • Nearest Cities
      • Exployee Free Time
      • Tree Add Removal
    • Lyft
      • Autocomplete
      • Job Scheduler
      • Read4
      • Kvstore
    • Amazon
      • Min Binary Str Val
    • AppLovin
      • TODO
      • Java Basic Questions
    • Google
      • Huffman Tree
      • Unique Elements
    • Instacart
      • Meeting Rooms II
      • Pw
      • Pw2
      • Pw3
      • Expression1
      • Expression2
      • Expression3
      • PW All
      • Expression All
      • Wildcard
      • Free forum tech discussion
    • OpenAI
      • Spreadsheet
      • Iterator
      • Kv Store
    • Rabbit
      • Scheduler
      • SchedulerC++
    • [Microsoft]
      • Min Moves Spread Stones
      • Inorder Successor
      • Largest Palindromic Num
      • Count Unique Char Substrs
      • Reverse Words Ii
      • Find Missing Observations
      • Min Fuel Report Capital
      • Design Hashmap
      • Find Original Array From Doubled
      • Num Flowers Bloom
      • Distribute Coins
      • Find Median From Stream
Powered by GitBook
On this page
  • Clarifying Questions
  • Functional Requirements:
  • Non-functional Requirements:
  • Scale
  • High Level Architecture
  • DB Options
  • Scale Up
  • What if score service is down?
  • What if DB is down?
  • What if we grows DAU 100 times?
  1. System Design
  2. Mocks

Design Gaming Leaderboard

Clarifying Questions

  1. How real-time we want this gaming leaderboard to be updated?

  2. How's score calculated?

  3. Do we show top 10 players or show position of the current user, and show 5 more player above/below the current player.

  4. How many players? 5M DAU, 25M MAU,

  5. How many games are played on average during a day? 10 matches per day for each player.

  6. Is there a time segment associated with the leaderboard? Each month, a new rounament kicks off which starts a new leaderboard.

Functional Requirements:

  1. Able to show position of current user, show 5 more player above/below the current player.

  2. Able to show top 10 player.

Non-functional Requirements:

  1. Highly available

  2. Highly scalable

  3. Real time.

Scale

5M DAU, 10 matches per day -> 50M matches per day -> 600 QPS -> peak 600x5 = 3000 QPS.

Top 10 player -> 50 users/s -> 50 QPS if user only see leaderboard when first open the game.

High Level Architecture

DB Options

  1. Relational DB

New user:

INSERT INTO leaderboard (user_id, score) VALUES ('mary1934', 1);

Existing user:

UPDATE leaderboard set score=score+1 where user_id='mary1934';

Find user's leaderboard position:

SELECT (@rownum := @rownum+1) AS rank, user_id, score
FROM leaderboard
ORDER BY score DESC;

Pros:

  • Easy to implement.

Cons:

  • Query become slow when there are millions of rows: attempting to do a rank operation over millions of rows is taking 10 seconds, which not acceptable for real-time requirements.

  • Cache won't help since data are constantly changing.

  • Won't work on finding user rank because it needs a full table scan to determine rank.

  1. NoSQL DB

Pros:

  • Optimized for write, like Cassandra, DynamoDB

  • Efficiently sort items within the same partition by score.

  • Add index: use game_name#{year-month} as partition key and score as sort key.

  • Further partition by user_id % number of partition. Partition key becomes game_name#{year-month}#user_hash

Cons:

  • Need to figure out how much partition we need

  • Need to merge top 10 player results from multiple partitions.

  • Trade off between read complexity and number of partitions, need benchmarking.

  • No straight-forward solution to query relative rank of a user.

    • Can query percentile of a user's position instead. top 10%, 20% etc.

    • Assuming score distributions is similar across all shards. Run a cron job to analyzes the distribution of score to percentile and cache the result.

  1. Redis Sorted Set

A sorted set is implemented by: hash table and a skip list. hash table maps users to scores and the skip list maps scores to users. In sorted set, users are sorted by scores.

ZADD: insert user into set if they don't exist. Otherwise, update the score for user. It takes O(logn) to execute.

ZINCRBY: increment the score of the user by specified increment, O(logn) to execute.

ZRANGE/ZREVRANGE: fetch a range of users sorted by score. It takes O(logn+m), m is the number of entries to fetch.

ZRANK/ZREVRANK: fetch position of any user in ascending/descending order.

Add a point to user:

ZINCRBY leaderboard_feb_2021 1 'mary1934'

Fetches top 10 global leaderboard

ZREVRANGE leaderboard_feb_2021 0 9 WITHSCORES

Fetch user relative position on leaderboard

ZREVRANK leaderboard_feb_2021 'mary1934'

Fetch n positions above/below user's position

ZREVRANGE leaderboard_feb_2021 pos-5 pos+5

Storage requirements

24 character name: 24 bytes + score 4 bytes = 28 bytes.

one leaderboard entry per MAU = 28 bytes * 25 million = 700M bytes = 700MB

peak QPS is 3000 QPS, both are acceptable for a single Redis server.

Scale Up

What if score service is down?

Load balancer in front of score service and deploy service onto kubernetes. Use auto scaling.

What if DB is down?

Figure out read / write throughput?

If read throughput, then we need to add read replicas, add in-memory cache?

What if we grows DAU 100 times?

500M DAU -> data size: 65GB, QPS = 250,000

We need data sharding:

Fixed partition

We break up score by range. [1, 100], [101, 200], [201, 300] ... [901, 1000]

Need to make sure even distribution of scores across leaderboard.

When we insert/update score for a user, we need to know which shard they are in.

Option1:

calculating user current score from MySQL DB, not very performant.

Option 2

use a secondary cache to maintain mapping between user id and shard id.

Fetching top 10 player

fetch top 10 players from the shard with highest scores.

Fetching rank of a user

calculate rank within local shard + total number of players in shards with higher scores. We can use info keyspace command in O(1)

Hash partition

Redis cluster provides hash slot to shard data automatically across multiple Redis nodes. We can compute hash slot by: CRC16(key) % 16384

The first node contains hash slots [0, 5500], second: [5501, 11000], third: [11001, 16383]

Cons:

  1. For returning top k results, we need to use scatter-gather to merge the results. need to be sorted.

  2. If we have a lot of partitions, the query has to wait for the slowest partition.

  3. doesn't provide straightforward solution for determining the rank of a specific user.

PreviousDesign EmailNextFacebook New Feed Live Comments

Last updated 1 year ago

Drawing
Drawing