Note: True end-to-end exactly-once across systems is impossible (Two Generals). Implement effectively-once via idempotency.
4. Idempotent Consumer Pattern
Strategy
Detail
Dedup Table
Record event ID with side-effect in same tx
Natural Idempotency
Operation safe to repeat (e.g., SET x = 5)
Conditional Update
UPDATE WHERE version = expected
Upsert
INSERT ... ON CONFLICT DO NOTHING
Example: Idempotent Insert
INSERT INTO processed_events (event_id, processed_at)VALUES ($1, now())ON CONFLICT (event_id) DO NOTHING;-- If 0 rows affected → duplicate; skip side effect
5. Message Acknowledgment Pattern
Mode
Detail
Auto-Ack
Broker marks delivered on send; risk of loss
Manual Ack
Consumer explicitly acks after success
Negative Ack (NACK)
Reject + requeue or send to DLQ
Batch Ack
Ack range of offsets (Kafka commit)
6. Dead Letter Queue Pattern
Aspect
Detail
Purpose
Park messages that fail repeatedly
Trigger
Max retries exceeded, deserialization fail, validation fail
Insert event ID into inbox table in same tx as side-effect
Duplicate
Insert fails (PK conflict) → skip processing
Cleanup
Periodic delete of old inbox rows
Example: Inbox Atomic Processing
BEGIN;INSERT INTO inbox(event_id, received_at) VALUES ($1, now()); -- fails on duplicateUPDATE accounts SET balance = balance + $2 WHERE id = $3;COMMIT;
10. Priority Queue Pattern
Approach
Detail
Multiple Queues
Separate queue per priority; consumers prefer high
Native Priority
RabbitMQ x-max-priority; ActiveMQ priority
Fairness
Use weighted polling to avoid starving low priority
Note on Kafka
No native priority; use multiple topics or partitioning