← build log
reliabilitypattern 02Reliability & contracts

The idempotent inbox: surviving at-least-once delivery

RabbitMQ promises every message arrives at least once. "At least" is the part that breaks counters.

The problem

RabbitMQ’s delivery guarantee is at-least-once, never exactly-once. A consumer that crashes after processing a message but before acknowledging it will see that message again on redelivery — and so will a consumer restarting mid-replay of a backlog. ingestion-service consumes post.liked, post.commented, and half a dozen other event types into ClickHouse; if it applies the same post.liked event twice, a post’s like count in the analytics store is now wrong, silently, with nothing in any log flagging it.

Approaches considered

Trust the broker. Assume redeliveries are rare enough not to matter. They aren’t — a single consumer restart during a backlog replay is enough to double-count a chunk of events, and it happens on ordinary deploys, not exotic failures.

Manual ack discipline. Ack only after the write succeeds, never before. This narrows the window but doesn’t close it — the write can succeed and the ack can still be lost on the way back to RabbitMQ, producing a redelivery of an already-applied event.

Exactly-once via a different broker. Kafka’s idempotent producer plus transactional consumers gets closer, but it’s a different piece of infrastructure to run and operate for one property, not a fix within the stack already in place.

Idempotent inbox. Give every event a unique ID, record IDs that have been applied, and check that record before applying a new one.

Trade-offs

An inbox check is one extra lookup per event, and the inbox table itself needs its own retention policy — it can’t grow forever. It also only protects the consumer that implements it; every consumer with side effects still needs its own inbox.

The decision

Idempotent inbox, keyed on event ID, checked before the event’s effects are applied and written after.

Why

At-least-once delivery is a property of RabbitMQ that isn’t going away, so the consumer has to be the one that makes duplicates harmless. Deduplicating at the point of effect — right before the ClickHouse insert or the Neo4j write — means it doesn’t matter how many times a message gets redelivered upstream; the second and third copies become no-ops instead of double-counts.

Implementation

// ingestion-service — every consumed event is checked against
// an inbox table before its effects are applied.
func (c *Consumer) handlePostLiked(ctx context.Context, msg amqp.Delivery) error {
	var evt PostLikedEvent
	if err := json.Unmarshal(msg.Body, &evt); err != nil {
		return msg.Nack(false, false)
	}

	seen, err := c.inbox.AlreadyProcessed(ctx, evt.EventID)
	if err != nil {
		return err
	}
	if seen {
		return msg.Ack(false) // duplicate delivery, effects already applied
	}

	return c.db.WithTx(ctx, func(tx *sql.Tx) error {
		if _, err := tx.ExecContext(ctx,
			`INSERT INTO post_events (post_id, type, ts) VALUES (?, 'liked', ?)`,
			evt.PostID, evt.Timestamp,
		); err != nil {
			return err
		}
		return c.inbox.MarkProcessed(ctx, tx, evt.EventID)
	})
}