https://metaplay.io logo
Building a global leaderboard
# questions-and-feedback
j
Hi! Looking for advice on building a global guild leaderboard on Metaplay, mainly the scaling/sharding side. Setup: SDK R37.1, we already have Metaplay Guild framework (custom GuildActor/GuildModel), plus Leagues for a separate division-based Raid gameplay feature in use. Design specs/requirements: - All guilds in one global leaderboard. A few hundred guilds at release, but we want a design that scales to millions - Guild score = sum of members' power (already synced into GuildMember) - Player sees the top 20 guilds + their own guild's position (exact rank near the top, "Top X%" for the rest) - Updates can lag by minutes Current thoughts: 1. GuildActor writes its score to a custom IPersistedItem table (GuildScore, indexed by score) when it wakes up or its members change, throttled 2. A singleton LeaderboardActor rebuilds a snapshot every few minutes (top K + count of guilds per score range) 3. The client fetches the snapshot with a MetaRequest via SessionActor Questions: 1. Is there an existing pattern or SDK primitive for a global ranking across all DB shards, or is something like the above the way to go? 2. I guess querying millions of rows across all DB shards every few minutes is something to avoid. What's the recommended way to keep a global ranking up to date instead? 3. Should the score rows be spread across all DB shards by guild ID, or kept on a single shard since the leaderboard reads all guilds sorted by score? 4. For serving reads, is a singleton enough, or should we copy the snapshot to every node, like LiveOpsTimelineManager → LiveOpsTimelineProxy or LeagueManager → LeagueStateProxyActor (CreateDynamicService)? <- This a recommended pattern? Any tips from games that have done global leaderboards on Metaplay apriciated Thanks!
j
You correctly have indentified the hard part. What makes this design challenging is the knowing each Guilds Rank needs knowing all Guild Scores and this can become a scaling bottleneck. What I would do is start simple and increase complexity as demand increases, as you suggest here.
1. There is no general global ranking system, exactly because this can become a scalability trap. Additionally, each system has its own tricks such as expirations or versions or domains, so it's hard to create a primite covering all while being efficient.
2. Yes, that sounds like it can cause a lot of load in the large system. I would start by just fetching all rows in the database, and then incrementally make it more scalable. The main issue is that at millions of rows, this is a lot of database work. However, we only need the Top X to be precise. How I would do this is to split the problem to two parts. Maintaining the Top K and the Statistics. Maintaining Top K can be done by, well, either querying from database top K. And this is the initial simple solution, just poll it (remember to have an index :) ). To make it better, we can make the Manager keep it up-to date when changes happen. The manager can keep a list of 2*K guilds, and whenever a running Guild sees it should be in the 2K list but is not, it tells manager a correction of its current score. Manager adds it to the list, removes the entries at rank >2K. So this can be just a check every few minutes on Guild actor, or maybe on start. This Top K list is more important than the statistics since you usually want to show more data about these guilds, like their Name and Emblem, Leaders or something flavorful. For the Statisics, we only need the ID and the Score.
And yes, then the statistics. This can also start as full table scans where you just check in DB how many larger scores there are. Expensive but allows you to test this fast. Then again to make it better, we can make the Manager cache some statistics, i.e. split the score range to 100-1000 buckets and keep that in memory. When Guild moves from one range to another, it tells Manager to update statiscis by removign one from one bucket and adding one to another. There is a slight chance of drifting here as Guild could crash after telling manager, before saving its state, so it would inform manager again. This may or may not be a problem, since the drift can be so small it doesnt matter. If it doesnt matter, we can actually drop the whole Database part of this system. The manager only holds the Top K (bounded size) and Statistics (constnat size) so scaling is fixed. But if ends up mattering, the rows needs to stored in the DB but then each bucked can be refreshed by counting the the bucket ranges from DB. This can happen rarely (or not at all if not considered relevant). (There are probably more robust statistics models and keeping-in-sync methods than this int[1000] i'm describing, but I havent researched them. For example there could be N buckets per "shard" to limit drift per shard...)
3. Sharding helps distribute the load. So if the design is writing or reading a lot, it would be helpful to do this. NonSharded data is easier to deal with, but i would only use it in the beginning if we are convinced we are actually dropping whole DB storage altogether and going with Top K + Statistics approach. So this kinda comes to the design. When in doubt, go sharded. 4. This is something I would leave for later. In the beginning, its completely fine to ask the Manager. And when load increases, switch it so that callers fetch from Proxies, and Manager keeps the Proxies up-to-date. Here you specifically need to figure out if you want Manager to send Proxies the whole snapshot of the Top K + statistics, or deltas of those. If you want realish-time, you cannot send whole snapshots on every change. Same for updates. Initially it probably makes sense to make Guilds inform Manager directly, but for less overhead in larger system, the guilds should inform their onw Proxy which then flushes aggregate changes. This is mainly for Statistics as that affects all guilds. Top K is limited to K players, so its load is bounded by design. (But yes, that is a convenient pattern. Manager owns data, publishes copies to Proxies that are per node. Proxies publish via global variable. Callers can read or poll the global variable.)
So what I would say. Yes, start simple. Then do some research if the Statistics-based model can satisfy your games needs. If it can, we are golden (Remember to think about if we need to invalidate some guilds, like inactive. How to do that and with what response time). But if we need all the ranks, figure out how to minimize database usage. Writing scores is O(N) to the number of live Guilds, which is the same as their normal persistence DB writes, so this isn't impossible to deal with. Maybe just need to ensure scans (from read-replica DB) dont get too large and frequent. (What is too large or too frequent. When going prod or load testing, remember to add sufficient Prometheus metrics in places to be able to monitor this)