Skip to content

Part 9 · Blueprint 59

Real-Time Gaming Leaderboard

11 min reading · Sources · Editorial review pending

A local shard rank is not a global rank without counts and a shared observation.

More
20px
Reading list
Page details

Published date · Last edited:

Draft edition. Editorial review and supplementary reading recommendations are still being completed.

Format: Architecture walkthrough. Prerequisites: the beginner bootcamp. Chapter prerequisites: Indexes & Storage Engines: B-Trees vs. LSM Trees, Database Sharding & Consistent Hashing, Real-Time Stream Processing Engine (Apache Flink Internals).

Sixty-second mental model

Problem: A local shard rank is not a global rank without counts and a shared observation.

Use: Define rank semantics before choosing score or player partitioning.

Design boundary: Return ranks under an explicit tie, update and snapshot contract. Global rank over sharded scores requires more than merging each shard’s top few entries for arbitrary users.

Layer 1 · Intuition

Hypothetical scenario: Northwind is designing leaderboards. Maya represents product requirements, Dev implementation and Priya reliability. The team must agree what one score update means and which component owns the score update authority.

Think of a ranked score sheet with explicit rules for ties. Separate the requester from score update authority, then identify the retained evidence needed after failure.

Authority
The entitled state owner.
Invariant
A rule preserved across concurrency and failures.

Architecture and worked reasoning

Design boundary: Return ranks under an explicit tie, update and snapshot contract. Global rank over sharded scores requires more than merging each shard’s top few entries for arbitrary users.

Responsibilities on the request path

Leaderboards: responsibilities and request directionReturn ranks under an explicit tie, update and snapshot contract. Global rank over sharded scores requires more than merging each shard’s top few entries for arbitrary users.Score eventsScore authorityRank indexSnapshot query
Return ranks under an explicit tie, update and snapshot contract. Global rank over sharded scores requires more than merging each shard’s top few entries for arbitrary users.

Workload and limiting resource

Hypothetical scores 100, 90, 90, 80 yield competition ranks 1, 2, 2, 4, but dense ranks 1, 2, 2, 3. Both differ from unique consecutive positions with an identity tie-breaker. One million eight-byte scores use 8 MB raw before member identifiers and ranking structures.

Keep the units attached to each intermediate result. The assumptions above describe a hypothetical exercise, not observed vendor capacity. A real capacity decision also needs a measured service curve, skew, recovery traffic and an explicit admission policy for overload.

Mutation or publication path

Leaderboards: mutation boundaryAuthenticate score-producing evidence. Then: Apply a monotonic/versioned update under the game rule. Then: Update the rank projection and publication version.Step 1
Authenticate score-producing evidence.
Step 2
Apply a monotonic/versioned update under the game rule.
Step 3
Update the rank projection and publication version.
Authenticate score-producing evidence. Then: Apply a monotonic/versioned update under the game rule. Then: Update the rank projection and publication version.
  1. Authenticate score-producing evidence.
  2. Apply a monotonic/versioned update under the game rule.
  3. Update the rank projection and publication version.

Observation and recovery path

Leaderboards: observation boundaryQuery a stable snapshot or declare live-rank behavior. Then: Apply the specified tie-breaker. Then: Distinguish regional/friend/global populations.Step 1
Query a stable snapshot or declare live-rank behavior.
Step 2
Apply the specified tie-breaker.
Step 3
Distinguish regional/friend/global populations.
Query a stable snapshot or declare live-rank behavior. Then: Apply the specified tie-breaker. Then: Distinguish regional/friend/global populations.
  1. Query a stable snapshot or declare live-rank behavior.
  2. Apply the specified tie-breaker.
  3. Distinguish regional/friend/global populations.

Failure that changes the decision

Hypothetical scenario: A delayed older score replaces a newer personal best. Compare event/version semantics rather than accepting arrival order as score authority.

Attempt this decision before reading the explanation

Can shard top-100 lists answer any player’s exact global rank?

No. They omit most lower-scoring players. Exact rank requires additional counting/order-statistic information or a different query strategy.

Supporting mechanism analysis

Hypothetical scenario: Northwind. Maya represents product, Dev implementation and Priya reliability. The following workflow develops the underlying mechanism.

Hypothetical scenario. A seller withdraws Northwind product P and receives confirmation. A buyer in another region still sees it as available. Dev explains that replicas converge; Maya asks whether that statement is enough for this buyer’s action.

Define permitted read histories against the selected failure model. Keep price age separate from withdrawal eligibility and distinguish the needs of different readers. One database label cannot substitute for the contract of each end-to-end operation.

Before you continue: May a permitted old price also authorize an otherwise forbidden eligibility decision?

That transfers permission between different decisions. The browsing price rule does not establish the authority needed to decide whether a withdrawn item can be acted on.

This makes the guarantee reviewable. Trace which confirmed changes a reader must see and what response is honest when the required communication is unavailable.

The product has several readers. A seller checking an accepted edit may require read-your-writes. A browsing page can permit an explicitly bounded descriptive view. An action deciding whether an item remains eligible needs the authority specified for that action. One storage deployment can therefore participate in several different end-to-end contracts.

Write the required history in ordinary language first. If a withdrawal completes before this eligibility read begins, can that read return available? If the answer is no, routing it through an arbitrary stale cache violates the promise even when the database itself provides a stronger option. The caller observes the whole path, including projections, middleware and caches.

Hypothetical scenario. Writer A completes quantity 9, then reader B begins and returns old quantity 7. The existing completed-history example violates a linearizable register because real-time precedence is part of that contract. An eventual replica can allow the old value under a weaker, explicitly stated rule.

A session token requiring a particular source position can provide read-your-writes to its holder when routing or waiting enforces it. That does not automatically order another client's reads or make a multi-record transaction strictly serializable. A snapshot can give a coherent point-in-time view while intentionally lagging the latest completed external write.

For a multi-record invariant, describe a transaction history rather than borrowing single-register vocabulary. Serializability admits an equivalent serial order; strict serializability also constrains that order by real-time precedence. During a partition, identify which operations must wait or become unavailable to preserve the chosen contract. An available old description cannot silently reauthorize a withdrawn product.

PostgreSQL’s transaction-isolation documentation specifies snapshot behavior and the anomalies excluded by its isolation levels. It provides a concrete example of why a successful read needs an interpretation. A database snapshot rule does not by itself promise a current read from an asynchronously updated regional projection.

Layer 3 · Internals

A leaderboard must specify whether equal scores share a rank, are ordered by a tie-breaker, or occupy consecutive positions under a deterministic composite order. Redis sorted sets maintain members ordered by score and documented tie behavior. A floating-point score is not an unlimited container for an integer score plus arbitrary timestamp bits; the precision budget must be proved before packing fields.

Sharding by player identity makes updates easy to place but global top-K requires merging shard candidates. Sharding by score ranges can support rank calculations using counts below the player's range, but moving scores across boundaries needs a consistent update protocol. A count observed at a different snapshot from the player's score can yield a rank that never existed. Distinguish an approximate live rank from an exact rank as of a published snapshot. Version each player's updates so an old retry cannot lower a newer accepted score unless the product explicitly permits that operation.

Trade-offs and limits

ApproachProvidesRequires
Player-partitioned scoresPlaces writes by a stable identity.Global rank requires a merge or separate published view.
Score-range partitionsCan use range counts to narrow rank work.Boundary movement and snapshot-consistent counts require coordination.

Recovery decision

Hypothetical scenario: A delayed older score replaces a newer personal best. Compare event/version semantics rather than accepting arrival order as score authority.

No. They omit most lower-scoring players. Exact rank requires additional counting/order-statistic information or a different query strategy.

Write your design before revealing the review

Can shard top-100 lists answer any player’s exact global rank?

Evidence you can explain in your own design

Drafts and self-review are stored only in this browser. They are separate from reading completion.

Compare your decision

No. They omit most lower-scoring players. Exact rank requires additional counting/order-statistic information or a different query strategy.

A delayed older score replaces a newer personal best. Compare event/version semantics rather than accepting arrival order as score authority.

Check your understanding

Can shard top-100 lists answer any player’s exact global rank?

No. They omit most lower-scoring players. Exact rank requires additional counting/order-statistic information or a different query strategy.

How would you recover from this failure: A delayed older score replaces a newer personal best. Compare event/version semantics rather than accepting arrival order as score authority.

Start with the exact boundary that failed. Authenticate score-producing evidence. Apply a monotonic/versioned update under the game rule. Update the rank projection and publication version. Then recover evidence through the observation path: Query a stable snapshot or declare live-rank behavior. Apply the specified tie-breaker. Distinguish regional/friend/global populations. Preserve the distinction between a confirmed state change and an unavailable response.

Which assumption in the worked calculation would a burst or concentrated tenant invalidate first?

Hypothetical scores 100, 90, 90, 80 yield competition ranks 1, 2, 2, 4, but dense ranks 1, 2, 2, 3. Both differ from unique consecutive positions with an identity tie-breaker. One million eight-byte scores use 8 MB raw before member identifiers and ranking structures. Recompute the affected resource rather than multiplying unrelated storage and network budgets by the same factor. Return ranks under an explicit tie, update and snapshot contract. Global rank over sharded scores requires more than merging each shard’s top few entries for arbitrary users.

Which component in this topology could be removed from a smaller first deployment?

Score events, Score authority, Rank index, Snapshot query name responsibilities, not a requirement for separate processes. Co-locate responsibilities when their transaction, trust and capacity boundaries permit it. Authenticate score-producing evidence.

What evidence must a caller have before relying on the observation path?

Query a stable snapshot or declare live-rank behavior. Apply the specified tie-breaker. Distinguish regional/friend/global populations. No. They omit most lower-scoring players. Exact rank requires additional counting/order-statistic information or a different query strategy.

45-minute interview rubric

  1. 00–05: scope score update and score update authority.
  2. 05–12: identify the limiting resource and derive the domain workload above, retaining units and assumptions.
  3. 12–28: draw score update authority, numbered writes and reads.
  4. 28–40: challenge recovery: Define rank semantics before choosing score or player partitioning.
  5. 40–45: defend player_id placement, observations and remaining assumptions.

Go deeper

Decision log

Define rank semantics before choosing score or player partitioning.

Preserve score update identity and score update authority ownership.

Revisit changed workload, observations or recovery objectives.