Course contentsShow
← Cloud and Infrastructure
Lesson 554 of 2,98910. API Gateway and Event-Driven PatternsFree lesson

Message Visibility and Timeouts

Managing message visibility windows to prevent duplicate processing and handle long-running tasks.

Message Visibility and Timeouts

What you'll learn: How to prevent multiple consumers from processing the same message simultaneously and handle long-running tasks in message queues.

When a consumer retrieves a message from a queue, the message isn't immediately deleted. Instead, it becomes invisible to other consumers for a specified period called the visibility timeout. This window gives your consumer time to process and delete the message without other workers grabbing it too.

Think of it like borrowing a library book: when you check it out, it becomes unavailable to others. If you don't return it (delete the message) within the loan period (visibility timeout), it goes back on the shelf (returns to the queue) for someone else to borrow.

How It Works

  1. Consumer receives message → visibility timeout starts
  2. Message hidden from other consumers during timeout period
  3. Consumer processes and deletes message → done!
  4. If timeout expires before deletion → message reappears in queue

Common Scenarios

Short timeout, long task: If your processing takes 60 seconds but visibility timeout is 30 seconds, the message becomes visible again mid-processing. Another consumer might grab it, causing duplicate processing.

Solution: Set visibility timeout longer than your expected processing time, or extend it programmatically during processing.

Worker crashes: If your consumer dies mid-processing, the visibility timeout ensures the message automatically returns to the queue for retry. This is your safety net.

Tuning Considerations

  • Too short: risk of duplicate processing
  • Too long: delays retries when workers fail
  • Typical range: 30 seconds to 12 hours (provider-dependent)

Key Takeaway: Message visibility timeouts create a processing reservation window that prevents duplicate work while ensuring failed processing attempts don't lose messages—balance timeout length with your actual processing duration.