When I built the global leaderboard for Math Blitz, I was pretty pleased with myself. A shared high score table across all players, stored in a real database, served through a serverless function. For someone who doesn't have a background in this stuff, getting that working felt like a genuine accomplishment.
Then someone pointed out that the top 10 list was showing positions 1 through 5, then 1 through 5 again.
What was happening
The leaderboard works like this: when a game ends, the score gets sent to a Netlify serverless function, which stores it and returns the current top scores. The frontend displays whatever comes back, numbered in order.
The frontend was designed to show 10 entries. The serverless function had a constant at the top called MAX_ENTRIES set to 5. So the function only ever stored 5 scores and only ever returned 5 scores, no matter how many players submitted.
When the frontend asked for 10 and got back 5, something in the rendering logic displayed the same 5 twice. Positions 1 through 5, then 1 through 5 again. A perfect leaderboard for a parallel universe where only 5 people exist.
The fix
const MAX_ENTRIES = 10; // was 5
That was it. One number. The function had been faithfully doing exactly what it was told the entire time.
Why it took a while to find
When I saw the duplicate entries I assumed the frontend was broken. The rendering logic, the numbering, the way the list was being built โ I went through all of it looking for something that was looping incorrectly or appending twice. Nothing looked wrong because nothing was wrong. The frontend was displaying what it received exactly as intended.
It didn't occur to me to check the function itself, because the function was working. Scores were being submitted, scores were being returned, everything looked operational. The idea that it was silently capping the list at half the required size while appearing completely healthy took a while to land.
The leaderboard works correctly now. You can see it at the end of a Math Blitz run. Whether anyone other than me has ever made the top 10 is a separate question I try not to think about too hard.