Multiplayer Matchmaking & Live Leaderboards — Part B
Definitions
Sorted set
- In plain English
- A sorted set is a key-value store structure that keeps members ordered by score, so top-N and rank lookups are fast.
- Think of it like
- It is like a scoreboard that re-sorts itself every time a score changes.
Search widening
- In plain English
- Search widening is gradually relaxing match criteria the longer a player waits.
- Think of it like
- Like a dance teacher accepting a slightly different level when no exact partner turns up.
The brief
You will balance fair matches against waiting time, serve ranked views, and recover queues without duplicate assignments.
What it must do
- Publish global, personal, and friends rankings.
How well it must do it
- Match 90% of players within 30 seconds.
- Refresh leaderboards within five seconds of completed matches.
- Recover queues after matchmaker failures.
Out of scope
- In-game physics and anti-cheat.
Design walkthrough
Deep dive 1: fair matches without endless waiting
Group nearby ratings within each region and mode. Use the longest-waiting player as the next match candidate.
A waiting player starts with a fifty-point range, then moves to one hundred and two hundred after ten and twenty seconds.
Check the completed group against a defined rating-spread limit. Overlapping buckets alone do not guarantee a fair ten-player match.
Break equal choices by original join time. Measure waiting percentiles and rating spread using Module 2 measurement.
Low-population regions may miss the target even after widening. Tell players rather than silently moving them to distant servers.
Deep dive 2: rankings for millions
Use one global sorted set per season and regional sets for regional views. This extends the key-value store from Module 3.
ZADD season:9:global 1540 player:42
ZRANGE season:9:global 0 99 REV WITHSCORES
ZREVRANK season:9:global player:42
ZMSCORE season:9:global player:42 player:77- ZADD sets an absolute score; repeating the same value does not add points.
- ZRANGE with REV returns the highest scores first; positions zero through 99 contain at most 100 players.
- ZREVRANK returns a zero-based descending position; add one for the displayed rank. Its lookup takes logarithmic work.
- ZMSCORE fetches selected friends scores; sort that small list for friends-only positions, not global ranks.
Equal scores use the store's deterministic member ordering. Explain that displayed positions are distinct, even when scores tie.
Committed ratings flow through an event stream to sorted sets; an API reads ranks directly and top lists through a short cache.
Check versions and replace scores in one atomic store operation. Rebuild from a consistent ratings snapshot, then replay later updates.
Deep dive 3: one result, one rating change
The rating service inserts the match ID and updates ratings in one transaction, while duplicate delivery skips changes before acknowledgement.
Use a unique processed-match ID. Lock affected rating rows in a consistent player order; conflicting matches must not overwrite each other.
This is Module 5 idempotency. A separate marker written before or after the rating transaction leaves a crash gap.
Store pending rating events in that transaction. A retrying publisher sends absolute ratings with versions; consumers reject older versions.
What breaks, and what happens
| Failure | Effect | Mitigation |
|---|---|---|
| Matchmaker dies | Its queue stops forming matches. | Elect a replacement with a lease; storage rejects old fencing tokens. Restore join times and assignment state. |
| Game server dies mid-match | No valid final result exists. | Void the match without rating changes; requeue players who choose to continue. |
| Leaderboard node fails | Rank reads become stale or fail. | Promote a replica or rebuild from durable ratings and replayed events. |
| Region loses cross-region connectivity | Global rank updates arrive late. | Continue local play with healthy local dependencies; label global rankings with freshness. |
Leadership and monitoring
Module 5 leases allow replacement, while fencing stops an old leader from assigning players. Gateways resend queue entries with original IDs.
Reconcile those entries against durable match assignments before requeueing. A connected player may already belong to an allocated match.
Failover includes detection, election, and restoration time. Open gateway connections alone do not guarantee recovery within a few seconds.
- Track time-to-match percentiles by region and mode, including abandoned waits.
- Measure rating spread, queue length, duplicate assignments, and game allocation failures.
- Track result-to-leaderboard delay; stream delay plus cache age must stay within five seconds.
At ten times the scale
Five million concurrent players would mean 5,000,000 ÷ 10 = 500,000 simultaneous matches under the same assumptions.
Add regional capacity and skill-band shards. Coordinate neighbouring bands so widening can still find players without double assignment.
Score-range leaderboard shards can merge their local top lists into an exact global top 100. Global rank needs counts from higher ranges.
If those counts are delayed, label the rank approximate. Do not imply sharding alone preserves instant exact global ranks.
Design decisions
How far should the skill search widen?
Our choice
Widen in timed steps and measure wait percentiles. This applies Module 2 measurement.
Why not the alternatives
- Keep a fixed narrow range foreverSmall queues can leave players waiting indefinitely.
How do rankings stay fast?
Our choice
Use sorted sets and a one-second top cache. This builds on Module 3 stores and Module 4 caching.
Why not the alternatives
- Scan all ratings for every top listRepeated full scans waste work on the same popular answer.
How do consumers handle repeated results?
Our choice
Commit match IDs with rating changes. This uses Module 5 idempotency and Module 4 replay.
Why not the alternatives
- Assume events arrive onceA lost acknowledgement can cause delivery again.
Who owns a regional queue after failure?
Our choice
Use a lease and storage-enforced fencing from Module 5.
Why not the alternatives
- Let both leaders assign players during recoveryAn old leader could assign players already claimed by its replacement.
What happens to rankings during a partition?
Our choice
Serve labelled stale global rankings where replicas remain reachable. This is the Module 5 availability trade-off.
Why not the alternatives
- Require fresh global agreement for every rank readA cross-region outage would unnecessarily block local ranking views.
Trade-offs
Pros
- Widening reduces long waits.
- Sorted sets support fast ranked views.
- Durable results allow rebuilds.
Cons
- Wider matches can have larger skill differences.
- Cached ranks lag behind results.
- Safe queue recovery requires persistent assignment state.
Use when
- Large games balancing wait targets and match quality.
Avoid when
- Small games where a database query already meets ranking targets.
Quick check
Takeaways
- Widen searches gradually and measure both wait and fairness.
- Use sorted sets and cache the hottest top list.
- Process results idempotently and fence former queue leaders.