Notifier worker with retries and DLQ

You build cmd/worker, which consumes order-events and records one notification for each event's order customer. The event_id marker and notification row must commit together so redelivery after a crash cannot duplicate a notification.

Ticket

Build a separate cmd/worker binary that consumes the order-events topic and creates one notification for the customer of each order event. Use the processed_events and notifications tables introduced in migration 0007.

Kafka provides at-least-once delivery, so the same event can arrive more than once. Your worker must act as an idempotent consumer while retrying failed sends and forwarding unreadable or exhausted events to the dead-letter topic. The next module adds graceful shutdown to this worker and the API so both stop without dropping work.

Acceptance criteria

  • When an order reaches ACCEPTED, every event for that order, including PLACED and ACCEPTED, creates exactly one row in notifications within 60 seconds. Each row has the matching event_id, the order ID, and the customer's user_id.
  • When the same PLACED event is delivered three additional times, its four total deliveries produce exactly one notification. The worker also processes an event for a second order.
  • With FAIL_EVERY=2, place four orders. Every event receives exactly one notification within 90 seconds, and at least one record for those orders appears on order-events.retry with attempt=2.
  • With FAIL_EVERY=1, place one order. Exactly one record for that order appears on order-events.dlq with attempt=3. Its records on order-events.retry have attempts [2 3], and the event checked has zero rows in notifications.
  • When the worker receives the value this is not an event on order-events, it forwards exactly one record to order-events.dlq with the original key and value within 30 seconds. No record with that key appears on order-events.retry within 3 seconds.
  • After placing 30 orders, wait until the third collected event has a notification, kill the worker with SIGKILL, and restart it. Every event receives exactly one notification within 90 seconds after the restart.

Constraints

  • Attempts start at 1. A failed send at attempt 1 or 2 goes to order-events.retry with the next attempt number. A failure at attempt 3 goes to order-events.dlq with attempt=3.
  • Invalid event JSON goes directly to the DLQ without a retry.
  • A redelivered event must not create an additional notification row.

Design direction

A layout that works well is an internal/notify package with a processor responsible for deduplication, loading the order's customer, sending the notification, and recording the notification row. Keep these operations in one MySQL Transaction so a failed send rolls back the processed-event marker and notification insert. A separate worker loop can poll Kafka, forward failed records, and commit offsets after handling the polled batch. Any layout that meets the contract passes.

Consider these trade-offs:

  • Keeping the database transaction open during the send increases its duration. It also prevents a failed send from leaving a processed-event marker or notification row behind.
  • Use separate consumer groups for order-events and order-events.retry. Let the retry consumer use the record timestamp and attempt number to decide when to process a record. Do not make the main consumer sleep while waiting for a retry.
  • Treat forwarding as part of handling the original record. Preserve its key and value, and set the attempt header. Do not commit the original offset until publishing to the retry topic or DLQ succeeds.

How to check your work

From the project root, run:

bash
cd backend && make up
cd backend && make test W=W3B7

Read the first failing assertion to find the earliest behaviour that violates the contract. Fix that behaviour before interpreting failures from later checks.

Finished Exercise 3.17?

Reliable event consumers

2 items

  • 3.16Idempotency and retry handlingnot completed
  • 3.17Notifier worker with retries and DLQnot completed

API and worker shutdown

Up next · Module 6 · 2 items · about 1 h 15 min

Start module 6

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Add migration 0007 with processed_events and notifications.
  2. Build the processor to create one notification per order event.
  3. Skip sends for event IDs that have already been processed.
  4. Retry failed sends with the correct attempt header.
  5. Forward exhausted failures and unreadable records to order-events.dlq.
  6. Recover from a worker crash without losing or duplicating notifications.

Commands

terminal
cd backend && make up
terminal
cd backend && make test W=W3B7

Tests

  1. Every event for an accepted order creates exactly one notification for that order's customer.

    Input Start the worker and create an order that reaches ACCEPTED, producing its PLACED and ACCEPTED events.

    Expected Each event gets exactly one row in notifications, with the expected customer user_id and order ID, within 60 seconds.

    TestW3B7_EveryEventGetsExactlyOneNotificationForTheCustomer in backend/e2e/w3_events_test.go