# Expiration Policy (/sdks/rust/how-to/queues/expiration-policy)



## Overview [#overview]

An **expiration policy** puts a hard time limit on how long a queue message may sit unconsumed. It solves a different problem than a dead-letter policy — this isn't about messages that fail processing, it's about messages that go *stale*: a price quote, a one-time code, a cache-invalidation signal, where late delivery is actively wrong, not just delayed. Instead of every consumer re-checking timestamps itself, the deadline lives on the message and the broker enforces it.

At the API level, `.expiration_seconds(30)` attaches a per-message TTL when you build the message via `QueueMessageBuilder`, and the clock starts the moment the broker accepts it via `send_queue_message`, not when a consumer picks it up. Let the TTL elapse unconsumed and the broker silently removes it — a later poll just comes back empty, no error, no trace.

**Gotchas:** expiration is silent — no DLQ routing, no event, just a message that vanishes — so pair it with monitoring if you need visibility into how much work is being dropped. The timer starts at send time, not when a consumer picks up the work, so a message can expire mid-backlog even while a consumer is actively polling. And setting the TTL too short for your real consumer lag just turns ordinary slowness into silent data loss.

## Prerequisites [#prerequisites]

* KubeMQ server running on `localhost:50000`
* Rust SDK installed (`cargo add kubemq`)

## Code [#code]

```rust title="main.rs"
use kubemq::prelude::*;
use kubemq::QueueMessageBuilder;

#[tokio::main]
async fn main() -> kubemq::Result<()> {
    let client = KubemqClient::builder()
        .host("localhost")
        .port(50000)
        .build()
        .await?;

    let msg = QueueMessageBuilder::new()
        .channel("rust-queues.expiration-policy")
        .body(b"expires-soon".to_vec())
        .expiration_seconds(30)
        .build();

    let result = client.send_queue_message(msg).await?;
    println!(
        "Sent with expiration: id={}, expiration_at={}",
        result.message_id, result.expiration_at
    );

    client.close().await?;
    Ok(())
}
```

## How It Works [#how-it-works]

* `expiration_seconds(30)` sets the message to expire after 30 seconds.
* Expired messages are automatically discarded by the server and not delivered.
* The `expiration_at` field in the result shows the Unix timestamp when the message expires.
* Review timeouts, channel names, and client IDs before running against shared environments.
* Run the program while the server from the prerequisites is available.

## Related [#related]

* [Queues overview](/learn/queues)
* [Rust SDK Reference](/sdks/rust/reference/queues)
* [Delay Policy](/sdks/rust/how-to/queues/delay-policy)
