Design a Matchmaking System
medium 45 minPut ten people who are waiting into one fair game, near enough to a server, before they give up.
See it run
Follow a ticket into the pool, ten players into a game, a party, a replacement, and the choice itself.
Operations
Open a ticket
Ask, look up, store, wait for news.
Step 1 of 6
Ask for a game
us-west
25 ms
us-east
85 ms
The app sends the mode and its measured delays to the regions.
Step 1: Ask for a game
def open_ticket(player, mode, delays): rating = ratings.get(player, mode) if not redis.set(f"player:{player}", new_id, nx=True, ex=300): raise Conflict("already waiting") tickets.save(new_id, rating, delays, joined=now())- Time
- under 100 ms
- Work
- 1 KB
The player key is set only if absent. It is what keeps one player in one ticket.
In the real world
The same design a matchmaking system doing real jobs. Pick one and step through it.
A failed promise must cost only one player.
Step 1 of 5
Nine thumbs
- Accepted
- 9 of 10
The deadline is near.
Step 1: Nine thumbs
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Trying every group
critical100 candidates have 17 trillion groups of ten.
Wrongfor combo in combinations(candidates, 10): score(combo)Rightanchor = oldest(pool)group = nearest(anchor, 10)Popping players one by one
criticalA crash loses them, and two workers hold half a group each.
Wrongten = [redis.zpopmin(queue) for _ in range(10)]Rightstore.reserve(ten, match_id, lease_number)Weights that do not add up to one
majorThe party lands on another scale, far above everyone.
Wrongstrength = 1.2 * best + 0.8 * meanRightstrength = 0.6 * best + 0.4 * meanSending the nine to the back
majorSomeone else's decline costs them their wait.
Wrongticket.joined = now()Rightticket.state = "waiting" # joined is unchangedBelieving the client's rating
criticalAnybody would get weak opponents.
Wrongrating = body["rating"]Rightrating = ratings.get(player, mode)
Interview tips
Size it first
- Rate times wait: 312,000.
- 312 MB is one machine.
- 830,000 games are the cost.
Show the trade
- Strict first, wider with the wait.
- Both ranges must agree.
- Say when fast beats fair.
Keep promises
- Reserve, confirm, remove.
- Lease numbers.
- One clock for the deadline.
Think of people
- Parties as one.
- Wait shown as a range.
- Protect late joiners.