Concurrency and races · Chapter 15 of 15

Read-your-writes on a replica

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

  • Part: Time
  • 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: send the rider to the old flat on purpose

    Write reproduce() in repro.js. Make a stand-in database with createDatabase from replica-db.js, holding one customer, priya, at her old address (createDatabase({ priya: { address: 'Flat 4B, 5th Cross, Koramangala' } })), and profiles with createProfiles({ primary: db.primary, replica: db.replica }) from profiles.js. Save her new address, '22 Lake View Road, HSR Layout', with saveAddress, then, without calling db.catchUp(), resolve to what addressForRider gives for her.

    The check runs your reproduce() twice: beside the original profiles.js it must resolve to 'Flat 4B, 5th Cross, Koramangala' (the replica has not copied the change), and beside a fixed profiles.js to '22 Lake View Road, HSR Layout'. No real waiting: the replica stays behind until you say so, and reproduce() must finish within 2 seconds.

  2. Task 2

    profiles.js: the rider gets the address the customer just saved

    Fix createProfiles({ primary, replica }) in profiles.js so a user reads her own write. Right after saveAddress for a user, while the replica has not applied that write (replica.position() is below the position primary.write resolved to), addressForRider reads that user from the primary and gives the new address. Once the replica has caught up with her write, she is read from the replica again. Users who saved nothing are always read from the replica, never the primary, even right after someone else saved.

    Keep the two log lines, profile <id>: address saved and profile <id>: rider sent to <address>, and the exports as they are. The check uses its own users and addresses on the stand-in from replica-db.js, and counts which one served each read, so reading everything from the primary fails.

  3. Task 3

    profiles.test.js: a test that would have caught it

    Write a vitest test file, profiles.test.js, that would have caught the stale read. The check runs your file three times: beside the original profiles.js, where at least one test must fail; beside the fixed profiles.js, where every test must pass; and beside a wrong fix that reads every address from the primary, where at least one test must fail.

    So test both sides with the stand-in from replica-db.js: after saveAddress, before db.catchUp(), addressForRider gives the new address; and a user who saved nothing is read from the replica, so db.reads() shows the replica served it and the primary did not. Each test must finish within 3 seconds.

What you will learn

  • repro.js: send the rider to the old flat on purpose
  • profiles.js: the rider gets the address the customer just saved
  • profiles.test.js: a test that would have caught it

More in Time

Build "Read-your-writes on a replica" 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.