Redis · Chapter 25 of 26

Capstone: match night

By the end of this chapter you have built match night for GullyXI's opener, match 2001, in five modules: the cached match centre, the login limit and sessions, the join that never oversells the last spots, the ball feed with its scorers and live scores, and a replay of all 120 balls of Mumbai's innings that drives it end to end. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Tested
  • 5 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

Rhea has one sentence for this season: the opener must hold. At 7:20 pm two lakh fans open the match centre, log in, and rush the last spots of Winner Takes All. At 7:30 the first ball is bowled, and from then on every ball must reach every phone and move every leaderboard within a second. Venkat will check that every paisa of the entry fees adds up, and Gurpreet will be watching the dashboard you built in chapter 23.

You have built every piece on its own. A real match night is all of them at once, and the hard parts are where they meet: one database call for a crowd, a session that slides, a last spot that only one fan gets, a ball that is scored once even with two workers, and points that come out exactly right after the last ball. Farhan has one test for the whole thing: replay the innings and compare the boards with the book.

What you build: 5 tasks

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

  1. Task 1

    centre.js: the match centre

    In centre.js, getMatchCentre(redis, db, matchId) returns page:match:<matchId> parsed from JSON when Redis has it. Otherwise it calls db.getMatchCentre(matchId), saves the page as JSON with EX 30 and returns it; a match that does not exist (null) returns null and saves nothing.

    50 opens of one match at the same moment cost one database call, two different matches opened at the same moment each get their own page, and a later open costs none. The Check reads the TTL (25 to 30 seconds).

  2. Task 2

    fans.js: logins and sessions

    In fans.js, allowLogin(redis, fanId, now) allows at most 5 logins a fan in each minute (the minute is now / 60000 rounded down), counted in login:<fanId>:<minute>, which expires within 60 seconds; it returns true or false.

    createSession(redis, fanId, now) makes a random id, the hash session:<id> with the fields fan and created (now) and EX 1800, adds the id to the set fan:<fanId>:sessions, and returns the id. getSession(redis, id) returns { fan, created } with created as a number, or null, and sets the session's TTL back to 1800; reading a missing session makes no key. logoutEverywhere(redis, fanId) deletes all of the fan's sessions and the set, and returns how many sessions it ended (0 for a fan with none).

  3. Task 3

    join.js: the last spots of Winner Takes All

    In join.js, joinContest(redis, contestId, fanId, feePaise) uses contest:<id>:spots_left (a count), contest:<id>:fans (a set), contest:<id>:pool (paise) and the hash wallet:<fanId> (field balance, in paise). It returns already when the fan is in the set, full when no spots are left, funds when the balance is below the fee or there is no wallet (nothing changes in those three), and otherwise joined: one spot fewer, the fan in the set, the fee taken from the balance and added to the pool.

    The Check sets 10 spots, sends 20 fans with 50000 paise each at the same moment with a fee of 19900: exactly 10 join, 10 are told full, and every wallet and the pool add up. It also tries a fan with too little money, a fan with no wallet, and a fan who joins twice.

  4. Task 4

    feed.js: the ball feed and the scorers

    In feed.js, publishBall(redis, ball) adds a ball of balls.json to the stream match:2001:balls with an id Redis makes and the fields seq, over, batter, bowler, runs, wicket (1 or 0), total and wickets in that order, then PUBLISHes the ball as JSON on match:2001:live, and returns the stream id.

    scoreBalls(redis, entries, consumer, count) (count 20 when left out) reads up to count new balls for consumer in the group scorers (made at 0 if it is missing, with no BLOCK), and for each ball adds the batter's points (1 a run, 4 more for a four, 6 more for a six) and the bowler's (25 for a wicket) to the sorted set points:match:2001, and to board:contest:<contest> for every fan whose team (entries.json) has that player, 2x for its captain and 1.5x for its vice-captain. It acknowledges each ball it scored and returns how many (0 when none are left). Two workers run at once, and each ball is scored once.

  5. Task 5

    replay.js: the whole innings, end to end

    In replay.js, replayInnings(redis, balls, entries) publishes every ball in order with your publishBall, scores them with your scoreBalls until it returns 0, and returns { balls, top }: balls is how many it published, and top is the top 3 of board:contest:71 as [{ fan, points }] in the order a reverse ZRANGE gives, points as numbers.

    The Check listens on match:2001:live (all 120 balls must arrive), compares points:match:2001 with points.json, compares every contest's board with its own sums, and checks that no ball is left pending in the group scorers.

What you will learn

  • centre.js (chapters 10 and 20): cache-aside with one database call for a crowd.
  • fans.js (chapters 11 and 12): a fixed-window login limit and sliding sessions.
  • join.js (chapters 8, 18 and 19): the last spots, as one script.
  • feed.js (chapters 13, 15 and 16): a stream of balls, live scores over pub/sub, and scorers in a consumer group.
  • replay.js: the 120 balls end to end.

More in Tested

Build "Capstone: match night" 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.