Transcript
A match ends. Nova finishes with 48,210 points. Ten million people play this game. So what's her rank? The game wants the answer before the next screen loads. The obvious way: sort all 10 million scores, then look her up. But scores change thousands of times a second. You'd be sorting forever. Counting everyone above her isn't better. That reads up to 10 million rows, every single time. So big games don't sort. They keep the list always sorted. Redis, a popular in-memory database, does this with a sorted set. Inside is a skip list. At the bottom, every player in one line, highest score first. On top, express lanes. Each player has a one in four chance to also stop on the lane above. So each lane has about a quarter of the stops below it. The top lane has only a handful. William Pugh published this idea in 1990. Now let's find Nova. Start on the top lane. Ride right while the next stop still beats 48,210. The next one is lower, so drop down a lane. Ride again, drop again, and again. One more step, and there she is. A few dozen hops, not 10 million. But where's the rank? Here's the clever part. Every express link remembers how many players it skips. Add up the skips along the path, and you get 1,204,381. Her rank, and nobody was counted. Next match, a new score. Nova is unhooked and slotted back in, again in a few dozen steps. The top ten is even easier: just read the first ten in line. One catch: ties. Zoe also has 48,210. Redis breaks the tie by comparing names, not by who got there first. A common fix: fold the time into the score, so the earlier player ranks higher. So remember it like this: don't sort the list, keep it sorted, and let the express lanes do the counting. Want hands-on IT practice like this? Try it free at seclern.com, and follow for more.