Backend
Rewriting CubeCraft's leaderboards in Rust, behind shadow reads
How I rebuilt Rocket's leaderboards as an in-memory Rust gRPC service and checked it against the old Java and PostgreSQL path on live traffic first.
- ~30 msto index a 250K-entry board in memory
- Millionsof players a month read these boards
The problem
Leaderboards are one of the most-read things on CubeCraft, a network with millions of players a month. Every read went through Java to PostgreSQL. I wanted them faster and cheaper to serve, but leaderboards are also very visible. If a player’s rank jumps around or a board comes back wrong, people notice fast.
The hard part was switching to it without anyone noticing.
What I built
- A Rust gRPC service that keeps boards resident in memory. Loads are single-flight, so a hundred requests for a cold board trigger one load, and boards fall out on an LRU with a TTL
- An exact top-K head for each board with a bucketed tail, so very large boards answer “what’s my rank” without sorting millions of rows
- Shadow reads: the Java path kept serving players while it sampled the same reads against the Rust service in the background, and recorded mismatches and latency as metrics
- A flag-gated cutover. Reads go through Rust and fall back to PostgreSQL when the board is cold, still loading, or erroring
- A multi-pod end-to-end test covering cold loads, writes showing up in reads, and killing the primary partway through
- Game serverasks for a board page
- Java serviceanswers from PostgreSQL
- Shadow samplesame read sent to Rust in the background
- Comparemismatch and latency metrics
- Read request
- Flag on?per board
- Rust servicein-memory board
- FallbackPostgreSQL when cold, loading or erroring
How a read works
fn read_page(board, offset, limit):
if not flag_enabled("leaderboards.rust", board):
return postgres.page(board, offset, limit)
match rust.page(board, offset, limit):
Ok(page) -> return page
Err(Cold | Loading | _) -> return postgres.page(board, offset, limit)
fn shadow(board, offset, limit, served_page):
if random() > SAMPLE_RATE: return
spawn_detached: # not tied to the caller's request
started = now()
candidate = rust.page(board, offset, limit)
metrics.latency("rust", now() - started)
if not equivalent(served_page, candidate):
metrics.mismatch(board, classify(served_page, candidate))
What the comparison caught
- PostgreSQL sorts
NaNstat values above everything, while the Rust service dropped them - Neither side broke ties the same way at the edge of a page
Both are counted apart from real mismatches now, so a genuine bug can’t hide behind them. The comparison also caught one of mine: shadow reads were being cancelled along with the incoming gRPC call, which quietly skewed the sample. Moving them onto a detached task fixed it.
Why it matters
Moving reads over is a flag flip backed by live data. The old path still answers whenever the new service errors or a board is cold or still loading, and sampled shadow reads flag disagreements in the metrics.