Concurrency and races · Chapter 11 of 15

The webhook that came twice

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 two kitchen tickets on purpose

    Write reproduce() in repro.js. Make a table of handled events with createEventStore() from event-store.js, and a fake kitchen, an object whose send(orderId) counts the tickets it prints. Make a handler with createWebhookHandler({ events, kitchen }) from webhooks.js. Deliver one event, say { id: 'evt_9001', type: 'payment.succeeded', orderId: 5120 }, twice at the same time, wait for both, and resolve to how many tickets the kitchen printed.

    The check runs your reproduce() twice: beside the original webhooks.js it must resolve to 2 (one paid order, cooked twice), and beside a fixed webhooks.js it must resolve to 1. Copies delivered one after another never race, so start both before waiting for either. No real waiting: the stand-in answers on the next turn of the event loop, and reproduce() must finish within 2 seconds.

  2. Task 2

    webhooks.js: one kitchen ticket per event, however often it comes

    Fix handle(event) in createWebhookHandler({ events, kitchen }) in webhooks.js. The first copy of an event sends kitchen.send(event.orderId), logs webhook <id>: order <orderId> sent to the kitchen and resolves to 'done'. Every other copy, whether it arrives at the same moment or later, sends nothing, logs webhook <id>: duplicate, skipped and resolves to 'duplicate'. Different events are each handled once.

    When kitchen.send rejects, handle rejects with the same error, and the event must not stay marked as handled, so that the provider's next delivery of it sends the ticket. Keep the two log lines and the export as they are. The check uses its own table with the same methods as event-store.js, sends three copies together while it holds the kitchen back, and makes the kitchen fail once.

  3. Task 3

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

    Write a vitest test file, webhooks.test.js, that would have caught the double tickets. The check runs your file three times: beside the original webhooks.js, where at least one test must fail; beside the fixed webhooks.js, where every test must pass; and beside a wrong fix that claims the event but keeps the claim when the kitchen fails, where at least one test must fail.

    So test both sides with createEventStore() and a fake kitchen made with vi.fn: one event delivered twice at once prints one ticket; and when the kitchen rejects on the first delivery, handle rejects, and a second delivery of the same event resolves to 'done' and prints the ticket. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.js: send two kitchen tickets on purpose
  • webhooks.js: one kitchen ticket per event, however often it comes
  • webhooks.test.js: a test that would have caught it

More in Time

Build The webhook that came twice 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.