---
title: "Ephemeral queues for fast request and reply"
description: "Ephemeral queues live in memory, outside the log: a requester sends a replyTo inbox and waits on it with a timeout. Fast, and lost on a crash by design."
---

> Queen MQ documentation, for AI agents
> Complete self-contained summary of Queen MQ: https://queenmq.com/llms-brief.txt
> Fetch that first when the question is about the product rather than about this page.
> Index of all pages: https://queenmq.com/llms.txt

# Ephemeral queues for fast request and reply

Ephemeral queues live in memory, outside the log, for request/reply. The worker answers on whatever inbox a request names; the caller makes a private inbox and waits on it. There is no transaction or replay here, and a crash loses what is in flight.

The worker:

JavaScript:

```js
const { messages } = await queen.ephemeral.pop('price-requests', { group: 'pricers', wait: true })
for (const m of messages) {
  await queen.ephemeral.push(m.payload.replyTo, [{ sku: m.payload.sku, price: 59.9 }])
}
await queen.ephemeral.ack('price-requests', messages, { group: 'pricers' })
```

Python:

```python
result = await queen.ephemeral.pop("price-requests", group="pricers", wait=True)
for m in result["messages"]:
    await queen.ephemeral.push(m["payload"]["replyTo"], [{"sku": m["payload"]["sku"], "price": 59.9}])
await queen.ephemeral.ack("price-requests", result["messages"], group="pricers")
```

Go:

```go
batch, err := client.Ephemeral().Pop(ctx, "price-requests",
	queen.EphemeralPopOptions{Group: "pricers", Wait: true})
if err != nil {
	log.Fatal(err)
}
for _, m := range batch.Messages {
	var req struct{ Sku, ReplyTo string }
	_ = json.Unmarshal(m.Payload, &req)
	_, _ = client.Ephemeral().Push(ctx, req.ReplyTo,
		[]map[string]interface{}{{"sku": req.Sku, "price": 59.9}})
}
_, _ = client.Ephemeral().Ack(ctx, "price-requests", batch.Messages,
	queen.EphemeralAckOptions{Group: "pricers"})
```

Rust:

```rust
let eph = queen.ephemeral();
let batch = eph.pop("price-requests").group("pricers").wait(true).send().await?;
for m in &batch.messages {
    let reply_to = m.payload["replyTo"].as_str().unwrap_or_default();
    eph.push(reply_to, json!({ "sku": m.payload["sku"], "price": 59.9 })).send().await?;
}
eph.ack(&batch).await?;
```

PHP:

```php
$result = $queen->ephemeral()->pop('price-requests', ['group' => 'pricers', 'wait' => true]);
foreach ($result['messages'] as $m) {
    $queen->ephemeral()->push($m['payload']['replyTo'], [['sku' => $m['payload']['sku'], 'price' => 59.9]]);
}
$queen->ephemeral()->ack('price-requests', $result['messages'], ['group' => 'pricers']);
```

C++:

```cpp
queen::EphemeralPopOptions take;
take.group = "pricers";
take.wait = true;
json batch = client.ephemeral().pop("price-requests", take);
for (const json& m : batch["messages"]) {
    json answer = {{"sku", m["payload"]["sku"]}, {"price", 59.9}};
    client.ephemeral().push(m["payload"]["replyTo"], json::array({answer}));
}
queen::EphemeralAckOptions done;
done.group = "pricers";
client.ephemeral().ack("price-requests", batch["messages"], done);
```

The caller:

JavaScript:

```js
const inbox = `inbox-${crypto.randomUUID()}`           // a private reply queue
await queen.ephemeral.push('price-requests', [{ sku: 'A-1', replyTo: inbox }])
const reply = await queen.ephemeral.pop(inbox, { wait: true, timeout: 5000, commitOnDelivery: true })
console.log(reply.messages[0]?.payload)                // { sku: 'A-1', price: 59.9 }
```

Python:

```python
inbox = f"inbox-{uuid.uuid4()}"                # a private reply queue
await queen.ephemeral.push("price-requests", [{"sku": "A-1", "replyTo": inbox}])
reply = await queen.ephemeral.pop(inbox, wait=True, timeout=5000, commit_on_delivery=True)
print(reply["messages"][0]["payload"])         # {'sku': 'A-1', 'price': 59.9}
```

Go:

```go
inbox := "inbox-" + queen.GenerateUUID() // a private reply queue
_, err = client.Ephemeral().Push(ctx, "price-requests",
	[]map[string]interface{}{{"sku": "A-1", "replyTo": inbox}})
if err != nil {
	log.Fatal(err)
}
reply, err := client.Ephemeral().Pop(ctx, inbox,
	queen.EphemeralPopOptions{Wait: true, TimeoutMillis: 5000, CommitOnDelivery: true})
if err != nil {
	log.Fatal(err)
}
fmt.Println(string(reply.Messages[0].Payload)) // {"price":59.9,"sku":"A-1"}
```

Rust:

```rust
let eph = queen.ephemeral();
let inbox = format!("inbox-{}", queen_mq::uuid::uuidv7()); // a private reply queue
eph.push("price-requests", json!({ "sku": "A-1", "replyTo": inbox })).send().await?;
let reply = eph.pop(&inbox).wait(true).poll_timeout(Duration::from_secs(5))
    .commit_on_delivery(true).send().await?;
println!("{}", reply.messages[0].payload); // {"price":59.9,"sku":"A-1"}
```

PHP:

```php
$inbox = 'inbox-' . bin2hex(random_bytes(8));      // a private reply queue
$queen->ephemeral()->push('price-requests', [['sku' => 'A-1', 'replyTo' => $inbox]]);
$reply = $queen->ephemeral()->pop($inbox, ['wait' => true, 'timeout' => 5000, 'commitOnDelivery' => true]);
print_r($reply['messages'][0]['payload']);          // [sku => A-1, price => 59.9]
```

C++:

```cpp
auto inbox = "inbox-" + queen::util::generate_uuid_v7();      // a private reply queue
json request = {{"sku", "A-1"}, {"replyTo", inbox}};
client.ephemeral().push("price-requests", json::array({request}));
queen::EphemeralPopOptions reply_wait;
reply_wait.wait = true;
reply_wait.timeout_millis = 5000;
reply_wait.commit_on_delivery = true;
json reply = client.ephemeral().pop(inbox, reply_wait);
std::cout << reply["messages"][0]["payload"] << "\n";      // {"price":59.9,"sku":"A-1"}
```

**Why:** a request that waits for an answer ("what is the price of A-1, now?") needs speed more than history. Writing it to disk, replicating it and keeping it for days would cost time for nothing. Ephemeral queues keep the same groups, leases and acks, and skip the log. Use a regular queue for anything that must survive a crash.

**Figure.** The caller mints a private inbox queue, inbox-<uuid>, and pushes a request to price-requests with replyTo set to it. A worker of the group pricers pops the request, pushes the answer to the inbox and acks. The caller's pop on its inbox, waiting with a timeout, returns the answer. Everything stays in memory, outside the log.

Request and reply over ephemeral queues, with a private inbox.

## See also

- [Request and reply](/patterns/request-reply/): a quote with a timeout and a fallback.
- [HTTP API](/reference/http/): the `/api/v1/ephemeral` endpoints.

Source: https://queenmq.com/writing-applications/ephemeral/index.mdx
