Concurrent outbox relay

You build a concurrent outbox relay that claims unsent rows from MySQL and publishes them to Kafka. Competing relays must not publish the same row twice, and a relay that cannot reach Kafka must leave rows available to healthy relays.

Ticket

Your Week 3 API already publishes events from the transactional outbox to Kafka. Extend that relay so two API instances can process the same outbox table concurrently without publishing the same row twice.

A relay that cannot reach Kafka must leave its rows available for another instance. This keeps order events moving when one API instance has a broker connectivity problem. Week 5's event-driven features depend on these events reaching Kafka.

Acceptance criteria

  • With two API instances running and 200 outbox rows available for a test topic, Kafka receives every sequence value from 0 through 199 exactly once. The topic has zero outbox rows remaining with sent_at IS NULL.
  • When an additional API instance runs with KAFKA_BROKERS=127.0.0.1:1 while a healthy API instance is running, the healthy instance publishes all 50 outbox records for the test topic. Kafka receives exactly 50 records, and the topic has zero outbox rows remaining with sent_at IS NULL.

Constraints

  • A failed publish must leave its rows unsent and unlocked.
  • Concurrent relays must not publish the same outbox row more than once.

Design direction

Keep the relay's claim, publish and mark-sent work in the existing outbox relay. Before opening a claim transaction, ping Kafka with a short timeout. If the ping fails, return without claiming any rows.

When Kafka is reachable, claim rows inside a READ COMMITTED Transaction. Use SELECT FOR UPDATE with SKIP LOCKED so a relay skips rows held by another relay instead of waiting. READ COMMITTED avoids the gap-lock behaviour under REPEATABLE READ that can interfere with new outbox inserts.

Keep the claim, publish and mark-sent operations in the same transaction. If publishing fails, roll back so the rows remain available to another relay. Any package layout that meets the contract passes.

Consider these trade-offs:

  • Lock duration: Publishing while holding row locks coordinates the publish attempt with the database update, but a slow broker holds those locks longer. Keep the batch bounded.
  • Isolation: READ COMMITTED avoids the gap-lock behaviour that can block order placement from inserting new outbox rows.
  • Delivery guarantees: Kafka can accept a record before the database transaction commits. If the process fails in that gap, the row can be published again after rollback. This is at-least-once delivery, not global exactly-once delivery. Consumers need idempotent handling of duplicate events.

How to check your work

From the project root, run:

bash
cd backend && make up
cd backend && make test W=3O2

If a test fails, read its first failing assertion. It identifies whether rows were duplicated, remained unsent, or were stranded by the relay with an unreachable broker.

Finished Exercise 3.23?

Implementation

Run locally

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

  1. Run two API instances with relays sharing the outbox table.
  2. Publish all 200 rows exactly once across the two instances.
  3. Check Kafka reachability before claiming rows.
  4. Leave rows available when a relay cannot reach Kafka.
  5. Confirm the healthy relay publishes all 50 rows.

Commands

terminal
cd backend && make up
terminal
cd backend && make test W=3O2

Tests

  1. Two API instances can relay 200 outbox rows without publishing any row more than once.

    Input Start the API instances and insert 200 outbox rows for a test topic, with sequence values from 0 through 199.

    Expected Kafka receives every sequence value exactly once, and the topic has zero outbox rows remaining with sent_at IS NULL.

    TestW3O2_TwoAPIInstancesPublishEveryRowExactlyOnce in backend/e2e/w3_o2_test.go