Concurrency and races · Chapter 5 of 15

Cache stampede

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Two requests at once
  • 3 tasks
  • JavaScript
  • In the all labs plan

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

What you build: 3 tasks

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

  1. Task 1

    repro.js: start a stampede on purpose

    Write reproduce() in repro.js. Make a fake loadMenu, an async function that counts its calls and resolves to a menu (any value), and a pretend clock: a variable t starting at 0 and a now function that returns it. Make a cache with createMenuCache({ loadMenu, now, ttlMs: 60_000 }) from menu-cache.js. Get the menu once and wait for it. Then move t past the TTL (say to 61_000), start 20 gets at the same time, wait for all of them, and resolve to how many times loadMenu ran in all.

    The check runs your reproduce() twice: beside the original menu-cache.js it must resolve to 21 (one load at the start, then one per request), and beside a fixed menu-cache.js it must resolve to 2. Gets made one after another never race, so start all 20 before waiting for any. No real waiting: time moves only when you change t, and reproduce() must finish within 2 seconds.

  2. Task 2

    menu-cache.js: one reload, and nobody waits for it

    Fix createMenuCache({ loadMenu, now, ttlMs }) in menu-cache.js with single flight and stale-while-revalidate. With no menu yet, every get waits for one shared loadMenu call and resolves to its menu. With a fresh menu (younger than ttlMs by now()), get serves it with no load. With an expired menu, get resolves to the old menu at once while exactly one reload runs behind it, however many gets arrive; once that reload has landed, get serves the new menu, which counts as fresh from the time it landed.

    When a reload fails, get keeps serving the old menu, and the next get after the failure starts a new reload. Keep the log line menu cache: loading the menu from the database, once per load, when it starts. The check uses its own clock and menus, holds the database's answers back, and looks at what every get resolved to before releasing them.

  3. Task 3

    menu-cache.test.js: a test that would have caught it

    Write a vitest test file, menu-cache.test.js, that would have caught the stampede. The check runs your file three times: beside the original menu-cache.js, where at least one test must fail; beside the fixed menu-cache.js, where every test must pass; and beside a wrong fix that never reloads once it has a menu, where at least one test must fail.

    So use a fake loadMenu and a pretend clock: load the menu once, move time past the TTL, start many gets at once and expect exactly one more load; then let that reload land and expect get to give the new menu. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.js: start a stampede on purpose
  • menu-cache.js: one reload, and nobody waits for it
  • menu-cache.test.js: a test that would have caught it

More in Two requests at once

Build "Cache stampede" yourself

Tapri's backend grew busy, and Kabir keeps handing you incidents that only happen when two things run at once. Each is a race interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

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