Outbox relay to order-events
You build an outbox relay that reads committed, unsent MySQL rows and publishes Kafka records to each row's stored topic. It preserves event order, marks published rows as sent, and skips rows locked by another transaction.
Ticket
Build an outbox relay inside the API process. It reads committed, unsent rows from MySQL and publishes each row's topic, key and payload to Kafka. For order events, use the decimal order ID as the Kafka key.
W3B4 and W3B5 already write outbox rows in the same transaction as their order changes. Those events must reach Redpanda so downstream services can react to order activity. W3B7's notifier consumes the order-events topic, and Week 5's live status updates use the same stream.
Acceptance criteria
- When an order is created and moved to
PREPARING, three records appear onorder-eventswithin 15 seconds. Each record has the decimal order ID as its Kafka key. Each JSON value contains the matchingorder_idandevent_id, with these transitions in order:""toPLACED,PLACEDtoACCEPTED, thenACCEPTEDtoPREPARING. - When an order is created and moved to
ACCEPTED, its two records are published. Both corresponding outbox rows have non-NULLsent_atvalues. After a further three-second wait,order-eventsstill contains exactly two records for that order. - When five unsent outbox rows are inserted for the same generated topic, with key
kand payloadseqvalues0,1,2,3and4, five records appear on that topic with sequence values[0 1 2 3 4]in ascending outbox ID order. - When another transaction locks an earlier outbox row with
SELECT id FROM outbox WHERE id = ? FOR UPDATE, the relay publishes a later unlocked row without waiting. With the earlier row carrying payload sequence0and the later row carrying1, the topic contains only[1]while the lock is held. After the lock is released, the topic contains two records.
Constraints
- Publish each outbox row to the topic stored in its
topiccolumn. - Preserve ascending outbox ID order for rows published to the same topic when they are available to the relay.
- Use the decimal order ID as the Kafka key for order events.
- Set
sent_atafter successful publication, and do not publish rows again once they have been marked sent.
Design direction
A useful layout is an outbox relay component that owns the claim and publish cycle, with API startup responsible for creating the Kafka producer and managing the relay's lifecycle. Keep MySQL operations separate from the Kafka producer setup. Any layout that meets the contract passes.
Consider these trade-offs:
- Claim unsent rows in a Transaction using
FOR UPDATE SKIP LOCKED. This lets the relay move past a row held by another transaction instead of waiting for its lock. Order the selected rows by ID to preserve their order when they are available together. - Publish before marking rows as sent. If the process stops after Kafka accepts a record but before the database transaction commits, that record can be published again. This is At-least-once delivery delivery, not exactly-once delivery.
- Use the order ID as the Kafka key so events for one order go to the same Partition. Stop the relay before closing the producer, so the relay cannot try to publish through a closed client.
How to check your work
From the backend directory, run:
make up
make test W=W3B6Start with the first failing assertion. It identifies whether the problem is in the topic, key, event payload, publication order, sent_at state or lock handling.
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.
Kafka event publishing
3 items
- 3.13Kafka partitions and groupsnot completed
- 3.14Transactional outbox writesnot completed
- 3.15Outbox relay to order-eventsnot completed
Up next · Module 5 · 2 items · about 2 h 45 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Publish order events to
order-eventswith the decimal order key and required JSON fields. - Preserve the event transition order for each order.
- Mark published outbox rows with
sent_atand avoid republishing them. - Publish rows to their stored topics in ascending outbox ID order.
- Skip locked rows without blocking later unlocked rows.
Commands
cd backend && make upcd backend && make test W=W3B6Tests
Order events are published to
order-eventsin event order, keyed by the decimal order ID, with the required JSON fields.POST/v1/orders/{order_id}/eventsHeadersAuthorization Bearer {restaurant token} Body raw JSON{ "event_id": "e-accept-1", "event": "ACCEPT" }Response 200 OK{ "id": 41, "restaurant_id": 1, "status": "ACCEPTED", "version": 2, "total_paise": 55800, "created_at": "2026-01-05T10:00:00.000Z" }Input Create an order and move it to
PREPARING, producing thePLACED,ACCEPTEDandPREPARINGevents.Expected Three records appear within 15 seconds with the order ID as their key. Their values contain the matching
order_idandevent_id, with transitions""toPLACED,PLACEDtoACCEPTED, andACCEPTEDtoPREPARING, in that order.TestW3B6_EveryOrderEventIsPublishedInOrderWithItsOrderAsKeyinbackend/e2e/w3_events_test.go