5. Transactional outbox and idempotent consumers¶
- Status: accepted
- Date: 2026-11-18
Context¶
The services talk through RabbitMQ. Two failures must never lose or double an incident:
- Dual write. The
incidentservice changes its database and then publishes an event. If the process dies between the two, either the event is lost (database committed, nothing published) or it describes a change that never happened (published, database rolled back). - Redelivery. RabbitMQ delivers at least once. After a crash or a lost acknowledgement, the same message arrives again, and the same alert can also be sent many times by Alertmanager.
On top of that, many copies of the same alert can arrive at the same moment, and they must end up as one incident.
Decision¶
- Transactional outbox. Every state change writes its event to an
outboxtable in the same database transaction. A relay reads unpublished rows withSELECT ... FOR UPDATE SKIP LOCKED LIMIT 50, publishes each with publisher confirms, and setspublished_at. Rows are published in order and a batch stops at the first failure. - Idempotent consumers. A consumer inserts the event's
eventIdintoprocessed_eventin the same transaction as its work. If the insert finds the id already there, the event was handled before, and the message is acknowledged without doing anything. - One open incident per fingerprint is enforced by the database:
CREATE UNIQUE INDEX ... ON incident (fingerprint) WHERE status <> 'RESOLVED', withINSERT ... ON CONFLICT DO NOTHINGand then a locked read of the open incident. The Redis filter iningestonly marks repeats to save work; correctness does not depend on it. - Poison messages. Consumer queues are quorum queues with a delivery limit. A message that fails 5 times is moved to a dead-letter queue instead of blocking the queue.
- Ingest answers
202only after RabbitMQ confirms every message, and503withRetry-Afterotherwise, so Alertmanager retries instead of the alert being lost.
Alternatives considered¶
- Publish directly after commit: simple, but loses events when the process dies between commit and publish.
- Distributed transactions (XA) across PostgreSQL and RabbitMQ: RabbitMQ does not support XA, and XA is slow and hard to operate.
- Change data capture (Debezium) instead of a polling relay: no polling delay and no extra queries, but needs Kafka Connect or Debezium Server and logical replication; too heavy for this project.
- Exactly-once delivery: not offered by RabbitMQ; at-least-once plus idempotency gives the same result for the user.
Consequences¶
- Events are delivered at least once and may be delivered twice (for example after a crash between a
broker confirm and the
published_atcommit). Every consumer must useprocessed_event. - Events reach consumers up to one poll interval (500 ms) after the change.
outboxandprocessed_eventgrow; a cleanup job for old rows is needed later.- Several
incidentreplicas can run the relay at the same time;SKIP LOCKEDkeeps them from publishing the same row. - The guarantees are covered by integration tests against real PostgreSQL and RabbitMQ: 100 copies of one alert make one incident, redelivered events count once, a publisher that dies mid-batch loses nothing, two relays never publish the same row, and a poison message ends in the dead-letter queue after 5 attempts.