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

Message Retention and Expiration

Configuring retention periods and TTL settings to manage message lifecycle and prevent queue bloat.

Message Retention and Expiration

You'll learn: How to configure message lifecycle settings to automatically clean up old messages and prevent your queues from becoming cluttered with stale data.

What Is Message Retention?

Message retention defines how long messages remain in a queue before being automatically deleted. Think of it like a mailbox with a built-in shredder—letters (messages) that sit unread for too long get destroyed to make room for new ones.

When you send a message to a queue, it doesn't live there forever. Cloud message services let you set a retention period (also called TTL: time-to-live) that determines the maximum age of any message. After this period expires, the message vanishes even if it was never processed.

Why It Matters

Without retention limits, queues can become bloated with:

  • Messages from failed consumers that will never retry
  • Outdated requests that are no longer relevant
  • Test messages left behind during development

Queue bloat wastes storage, increases costs, and makes monitoring harder. Proper retention settings act as automatic housekeeping.

Common Configuration Patterns

Short retention (minutes to hours): For real-time events where old data loses value quickly (e.g., stock price updates, sensor readings)

Medium retention (days): For business transactions that need time for processing and retries but eventually become irrelevant

Long retention (weeks): For audit trails or scenarios where messages might need extended processing windows

Most queue services default to 4-14 days, but you can typically configure anywhere from 1 minute to 14 days depending on your provider.

Key Takeaway: Message retention periods automatically delete old messages to prevent queue bloat, reduce costs, and ensure your queues only contain relevant, actionable data—configure them based on how long messages stay valuable in your system.