Manual ACK Alone Won't Stop Duplicate Consumption: A Five-Layer Defense for Message Queues
Any team running message-driven payment, inventory, or coupon flows faces the same ACK-then-crash duplicate, and the standard interview answer (manual ACK) leaves a real money-losing hole. This layered design shows where the window is and what actually closes it.
Manual ACK only confirms that a message was received, not that the business logic ran exactly once. The failure window sits between completing business logic and sending the ACK: a node crash in that instant makes the broker redeliver the message, so the same deduction or inventory operation executes twice even though the ACK log shows one successful confirmation.
A practical defense stacks five layers. First, classify operations as reentrant (query sync, status refresh) or non-reentrant (payment, stock, coupon), and only enforce idempotency on the latter. Second, build idempotent business logic with unique business IDs, pre-consumption validation, state machine checks, and a database unique index as the final deterministic backstop. Third, reorder consumption to validate before executing and submitting ACK, split batches to avoid amplified redelivery, and cap retries with dead-letter queues. Fourth, isolate core and non-core consumer groups, and use rate limiting to prevent consumer overload from triggering redeliveries. Fifth, trace the full production-to-consumption path with message IDs, monitor duplicate-interception and dead-letter backlog metrics, and maintain a global idempotency-enforcement switch for peak traffic or incidents.
The commonly taught manual-ACK answer solves only message loss, not business idempotency, which is why interview answers diverge from production reality.
Relying on a single idempotency mechanism is fragile: caching can expire and state machines can be bypassed by concurrency, leaving the database unique index as the only truly authoritative guard.
Consumer overload itself is a major trigger for message redelivery, so rate limiting is not only about protecting downstream services but also about preventing the consumer from creating its own duplicates.
Separating core and non-core consumer groups is often treated as over-engineering until a non-core bug freezes the whole pipeline, as the author's incident shows.
The distinction between reentrant and non-reentrant operations saves effort: not every message needs idempotency, and forcing it everywhere wastes compute and complexity.
Changing the order to validate first, then execute, then ACK, is the right direction. Three additions: don't mix message ID or timestamp into the idempotency key; use stable fields like business order number + action, otherwise the key itself changes on redelivery; when a unique index collides, look up and return the existing result, directly throwing an exception will cause the upstream to keep redelivering, turning idempotency into error rate; for monitoring, besides intercepted volume and dead-letter backlog, add an idempotency key hit rate (intercepted / total consumption), which usually appears earlier than duplicate data.