AlgoViz

Design a Matchmaking System

medium 45 min

Put 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

Our systemPlayerTicket APIPush gatewayRatingsPostgreSQLTicket storeRedisPool matcher

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

Our systemTen playersPush gatewayMatch servicePool matcherTicket storeRedisFleet managerGame server
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

    critical

    100 candidates have 17 trillion groups of ten.

    Wrong
    for combo in combinations(candidates, 10):    score(combo)
    Right
    anchor = oldest(pool)group = nearest(anchor, 10)
  • Popping players one by one

    critical

    A crash loses them, and two workers hold half a group each.

    Wrong
    ten = [redis.zpopmin(queue) for _ in range(10)]
    Right
    store.reserve(ten, match_id, lease_number)
  • Weights that do not add up to one

    major

    The party lands on another scale, far above everyone.

    Wrong
    strength = 1.2 * best + 0.8 * mean
    Right
    strength = 0.6 * best + 0.4 * mean
  • Sending the nine to the back

    major

    Someone else's decline costs them their wait.

    Wrong
    ticket.joined = now()
    Right
    ticket.state = "waiting"   # joined is unchanged
  • Believing the client's rating

    critical

    Anybody would get weak opponents.

    Wrong
    rating = body["rating"]
    Right
    rating = 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.