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, includingPLACEDandACCEPTED, creates exactly one row innotificationswithin 60 seconds. Each row has the matchingevent_id, the order ID, and the customer'suser_id. - When the same
PLACEDevent 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 onorder-events.retrywithattempt=2. - With
FAIL_EVERY=1, place one order. Exactly one record for that order appears onorder-events.dlqwithattempt=3. Its records onorder-events.retryhave attempts[2 3], and the event checked has zero rows innotifications. - When the worker receives the value
this is not an eventonorder-events, it forwards exactly one record toorder-events.dlqwith the original key and value within 30 seconds. No record with that key appears onorder-events.retrywithin 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 attempt1or2goes toorder-events.retrywith the next attempt number. A failure at attempt3goes toorder-events.dlqwithattempt=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-eventsandorder-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
attemptheader. 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:
cd backend && make up
cd backend && make test W=W3B7Read the first failing assertion to find the earliest behaviour that violates the contract. Fix that behaviour before interpreting failures from later checks.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Reliable event consumers
2 items
- 3.16Idempotency and retry handlingnot completed
- 3.17Notifier worker with retries and DLQnot completed
Up next · Module 6 · 2 items · about 1 h 15 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Add migration
0007withprocessed_eventsandnotifications. - Build the processor to create one notification per order event.
- Skip sends for event IDs that have already been processed.
- Retry failed sends with the correct
attemptheader. - Forward exhausted failures and unreadable records to
order-events.dlq. - Recover from a worker crash without losing or duplicating notifications.
Commands
cd backend && make upcd backend && make test W=W3B7Tests
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 itsPLACEDandACCEPTEDevents.Expected Each event gets exactly one row in
notifications, with the expected customeruser_idand order ID, within 60 seconds.TestW3B7_EveryEventGetsExactlyOneNotificationForTheCustomerinbackend/e2e/w3_events_test.go