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 on order-events within 15 seconds. Each record has the decimal order ID as its Kafka key. Each JSON value contains the matching order_id and event_id, with these transitions in order: "" to PLACED, PLACED to ACCEPTED, then ACCEPTED to PREPARING.
  • When an order is created and moved to ACCEPTED, its two records are published. Both corresponding outbox rows have non-NULL sent_at values. After a further three-second wait, order-events still contains exactly two records for that order.
  • When five unsent outbox rows are inserted for the same generated topic, with key k and payload seq values 0, 1, 2, 3 and 4, 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 sequence 0 and the later row carrying 1, 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 topic column.
  • 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_at after 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:

bash
make up
make test W=W3B6

Start 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.

Finished Exercise 3.15?

Kafka event publishing

3 items

  • 3.13Kafka partitions and groupsnot completed
  • 3.14Transactional outbox writesnot completed
  • 3.15Outbox relay to order-eventsnot completed

Reliable event consumers

Up next · Module 5 · 2 items · about 2 h 45 min

Start module 5

Implementation

Run locally

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

  1. Publish order events to order-events with the decimal order key and required JSON fields.
  2. Preserve the event transition order for each order.
  3. Mark published outbox rows with sent_at and avoid republishing them.
  4. Publish rows to their stored topics in ascending outbox ID order.
  5. Skip locked rows without blocking later unlocked rows.

Commands

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

Tests

  1. Order events are published to order-events in event order, keyed by the decimal order ID, with the required JSON fields.

    POST/v1/orders/{order_id}/events
    Headers
    AuthorizationBearer {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 the PLACED, ACCEPTED and PREPARING events.

    Expected Three records appear within 15 seconds with the order ID as their key. Their values contain the matching order_id and event_id, with transitions "" to PLACED, PLACED to ACCEPTED, and ACCEPTED to PREPARING, in that order.

    TestW3B6_EveryOrderEventIsPublishedInOrderWithItsOrderAsKey in backend/e2e/w3_events_test.go