Redis · Chapter 10 of 26

Caching the match centre

By the end of this chapter you can write a cache-aside read in Node.js: ask Redis first, build from the database only on a miss, keep the copy as JSON with a TTL, keep a "not found" too, delete the copy when the data changes, and stop fifty misses at once from becoming fifty database calls. 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

In chapter 1, lab page showed the idea: the match centre took 808 ms from GullyXI's database and 0.7 ms from Redis. Now it has to live in the app. Every app server runs Node.js, and every fan who opens the match centre calls one function. Rhea wants that function to touch the database once for two lakh fans, not two lakh times.

You already have the pieces. Chapter 3 gave keys a TTL, and chapter 9 sent commands from Node.js with ioredis. What is missing is the pattern that puts them together, and the three ways it goes wrong on a real night: a page stored as the wrong text, a page that still says "live" after the match ended, and fifty fans who miss Redis in the same millisecond and all go to the database together.

What you build: 4 tasks

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

  1. Task 1

    match-centre.js: the match centre, from Redis when it can be

    In match-centre.js, write getMatchCentre(redis, db, matchId) as a cache-aside read. Ask Redis for the key page:match:<matchId> (page:match:2001 for match 2001). On a hit, return what it holds as an object. On a miss, get the page with db.getMatchCentre(matchId), store it under that key as JSON text (a string) with a TTL of 30 seconds in the same command (the check accepts 25 to 30), and return the page as an object.

    Two opens of match 2001 make one database call (the check counts db.calls.getMatchCentre). A page already in Redis is returned without asking the database, and once the key is deleted the next open asks the database again, so keep the copy in Redis, not in your program.

  2. Task 2

    contest.js: a contest, and one that does not exist

    In contest.js, write getContest(redis, db, contestId), cache-aside like Task 1, with db.getContest(contestId), which answers null for a contest that does not exist. The key is cache:contest:<contestId>.

    A contest is stored as its JSON with a TTL of 60 seconds (the check accepts 55 to 60). A missing contest is stored too, as the JSON of null (the text null), with a TTL of 10 seconds (the check accepts 5 to 10). Return the contest as an object, or null.

    Two reads of contest 71 make one database call, and two reads of contest 99 (it does not exist) make one more, not two. Once cache:contest:71 is deleted, the next read asks the database again.

  3. Task 3

    stampede.js: fifty opens at once, one database call

    In stampede.js, write getMatchCentre(redis, db, matchId) again: the same cache-aside read as Task 1 (page:match:<matchId>, JSON, a TTL of 30 seconds, the check accepts 25 to 30, returns the page as an object), but fifty opens of the same match at the same moment must make one database call between them, inside this one program.

    While a build of a page is running, a new open of that page waits for the same build instead of starting another. Once the build has finished, forget it: after the key is deleted, fifty more opens make one more database call. If the database throws, that open rejects with the error, and the next open tries the database again.

  4. Task 4 · interview question

    Interview: stale-page.js, the page that said live

    Match 2001 finished, but fans saw live on the match centre for an hour. In stale-page.js, getMatchCentre keeps page:match:<matchId> for 3600 seconds, and matchChanged(redis, matchId), which GullyXI's admin tool calls right after it changes a match in the database, does nothing.

    Fix both: after matchChanged(redis, 2001), the next open of match 2001 shows the change from the database, while other matches' pages (page:match:2002) stay in Redis. Give the page a TTL between 10 and 60 seconds, a safety net for a change no one announced.

    Farhan will ask: why delete the copy instead of writing the new page into Redis, and how do you choose the TTL?

What you will learn

  • Cache-aside: read Redis; on a miss read the database and keep a copy.
  • JSON in and out: Redis keeps strings, so objects travel as JSON text.
  • SET with EX from ioredis: the copy and its TTL in one command.
  • db.calls: proving that a hit cost the database nothing.
  • Caching a miss: keeping "not found" for a few seconds.
  • Invalidation and stampedes: deleting a stale copy, and sharing one build between many opens.

More in Redis from Node.js

Build "Caching the match centre" 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.