Message Visibility and Acknowledgment
You'll learn how queues use visibility timeouts and acknowledgments to ensure reliable message processing without duplicates or data loss.
What Is Message Visibility?
When a consumer retrieves a message from a queue, the message isn't deleted immediately—it becomes invisible to other consumers for a set period called the visibility timeout. This prevents multiple workers from processing the same message simultaneously.
Think of it like checking out a library book: once you take it, others can't borrow it until you return it or your checkout period expires. If you don't finish reading (acknowledge completion), the book becomes available again.
How Visibility Timeouts Work
- Consumer receives message: Queue starts visibility timeout (e.g., 30 seconds)
- Message becomes invisible: Other consumers can't see this message
- Processing happens: Consumer works on the task
- Two outcomes:
- Success: Consumer sends acknowledgment (ACK), message permanently deleted
- Timeout expires: No ACK received, message becomes visible again for retry
Acknowledgment Patterns
Explicit Acknowledgment
Consumer must manually confirm successful processing. Safest approach—message survives crashes during processing.
Auto-Acknowledgment
Message deleted immediately upon delivery. Risky—failures lose messages.
Negative Acknowledgment (NACK)
Consumer explicitly rejects a message, returning it to the queue immediately rather than waiting for timeout.
Preventing Duplicate Processing
Visibility timeout too short: Message becomes visible again while still processing, causing duplicates.
Idempotent operations: Design handlers so processing the same message twice produces identical results (e.g., using unique message IDs to track completion).
Heartbeat/extend visibility: Long-running tasks can request timeout extensions while actively processing.
Key Takeaway: Visibility timeouts paired with explicit acknowledgments create a reliable contract between queue and consumer—messages remain available until confirmed processed, but aren't processed simultaneously by multiple workers, balancing reliability against duplicate risk.