Redis · Chapter 13 of 26

Leaderboards

By the end of this chapter you can run a live contest leaderboard in Redis from Node.js: score every fantasy team into a sorted set with one ZADD, move teams up with ZINCRBY as points arrive, read the top ten with their names in two commands, and show a fan their rank and the fans just above and below them. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Redis from Node.js
  • 4 tasks
  • Redis
  • In the all labs plan

The first 2 chapters of Redis are free. This one comes with the all labs plan.

Why you need this

Every fantasy team in a contest is scored from its eleven players' points, and the leaderboard is what fans watch all evening. After every ball, thousands of fans in the Mega Contest pull to refresh. Built from GullyXI's database, one refresh of contest 74's top ten (the database's entries, all the points, a sort, ten names) took this long in the lab:

from the database: 713 ms, database calls: 12

Two lakh fans refreshing after every ball is a database that never rests, and a board that is two thirds of a second old before anyone sees it. Rhea wants the board to move the moment a six is hit.

Chapter 7 met the tool: a sorted set keeps its members in score order all the time, so reading the top ten is reading the first ten, with no sort. This chapter puts a whole contest in one, from Node.js, and keeps it live.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    build-board.js: build a contest's leaderboard

    In build-board.js, write buildBoard(redis, db, contestId). Get the contest's teams with db.listEntries(contestId) (each has user, players, captain and vice_captain) and the points with db.getPoints() (an object of player ids to points; a player not in it has 0), one call each.

    A team's score is the sum of its eleven players' points, the captain's counted twice and the vice-captain's 1.5 times (u1056's team in contest 71 scores 588, u1395's 459.5). Store every team in the sorted set board:contest:<contestId>: the members are the fans' ids (user), scored with their team's score, teams on 0 included. Send all the teams in one ZADD, and start the board again from nothing first (an old board may hold fans who are not in the contest, like u9999). Return the number of teams: 240 for contest 71, 180 for contest 73. Each contest has its own board.

  2. Task 2

    live-points.js: points as the match is played

    In live-points.js, write addPoints(redis, entries, contestId, playerId, points). entries are the contest's teams, as db.listEntries gives them. Every team that picked the player gains the points on board:contest:<contestId>: twice for a team whose captain is the player, 1.5 times for its vice-captain, once otherwise. Teams without the player stay as they are.

    Add to the scores inside Redis (the check fails if you read scores with ZSCORE). Return how many teams changed: the number of teams that picked the player, or 0 for a player no team picked (P999). A six by P124 is 12 points: a team with P124 as captain gains 24.

  3. Task 3

    around-me.js: a fan's rank, and the fans around them

    In around-me.js, write aroundMe(redis, contestId, fanId) on board:contest:<contestId>, highest score first. Return { rank, points, around }: rank is the fan's place (1 for the top fan), points their score as a number, and around up to five rows in board order (the two fans above, the fan, the two below), each { rank, fan, points } with points a number. Near the top or the bottom there are fewer rows: the top fan gets ranks 1 to 3. Fans on equal points keep the order the board gives them (do not sort again). For a fan who is not on the board (u1400), return null.

  4. Task 4 · interview question

    Interview: leaderboard-page.js, the leaderboard on every refresh

    In leaderboard-page.js, topTen(redis, db, contestId) builds the top ten from the database on every refresh (676 ms in the lab). Fix it so a refresh makes no database calls (the check counts db.calls.total).

    board:contest:<contestId> is already built (the check builds it; Task 1 builds it in your Redis), and each fan's name is in Redis under fan:<fanId>:name (the check stores them; node setup.js does in your Redis). Read the ten fans with their scores in one command, and the ten names in one MGET (the check counts the commands). Return the first ten in the board's order: [{ rank, fan, name, points }, ...], rank 1 to 10, points a number.

    Farhan will ask: why a sorted set, and not the database's ORDER BY on every refresh?

What you will learn

  • Scoring a team: its players' points, the captain's twice, the vice-captain's one and a half times.
  • ZADD with many members: a whole board in one command.
  • ZINCRBY: points added inside Redis as the match is played.
  • ZRANGE with REV and WITHSCORES from ioredis: the top ten as a flat list, and turning it into rows.
  • MGET for names: ten names in one round trip.
  • ZREVRANK and a window of ranks: a fan's place, and the fans around them.

More in Redis from Node.js

Build "Leaderboards" yourself

GullyXI, a Pune fantasy cricket app, falls over on match night. You make it fast with Redis. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Redis are free. This one comes with the all labs plan.