Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

Implement delayed retries with RabbitMQ for an order consumer: retry after 10 seconds, then 1 minute, then 10 minutes, then park the message. Describe a design with plain RabbitMQ features and the trade-offs against in-process retries.

With plain RabbitMQ we build delays from TTL and dead-lettering: one retry queue per delay, 10 seconds, 1 minute and 10 minutes, each with no consumers, a queue-level message TTL and a dead-letter exchange that routes expired messages back to the work queue. On failure the consumer republishes the message to the next retry queue and acknowledges the original, counting attempts in a header or from x-death, and after the last attempt sends it to a parking queue. Compared with in-process retries, which hold the message and the consumer thread during back-off, broker delays free the consumer and survive restarts, at the cost of more topology and lost ordering.

Background to this question

Retrying immediately is useless when a dependency is down for minutes, and retrying forever is dangerous. Real systems need increasing delays and a place to park messages that never succeed. RabbitMQ has no simple retry-after setting on a queue, so this question tests whether we can compose one from the features it does have, and whether we know the classic trap of per-message TTL. In this answer, we will compare in-process and broker-based retries, build the retry topology, explain why each delay needs its own queue, write the consumer that counts attempts and parks messages, and discuss the delayed-message exchange plugin.

1A Quick Recap: Retrying In the Process or On the Broker

Spring AMQP can retry inside the consumer. RetryInterceptorBuilder, or Spring Boot's spring.rabbitmq.listener.simple.retry properties, wraps the listener so a failed call is repeated after a back-off, within the same delivery. That is simple, but during the back-off the message stays unacknowledged on this consumer and the consumer thread sleeps. A 10 minute back-off would block a thread for 10 minutes, hold the message where nobody else can take it, and lose the retry state if the pod restarts.

Broker-based delays move the waiting into RabbitMQ. The consumer acknowledges the failed message after placing a copy in a queue that holds it for the delay, and the broker brings it back afterwards. Nothing in the application waits, and the delay survives restarts. The building blocks are TTL, the time a message may stay in a queue, and dead-lettering, which republishes an expired message to another exchange.

2The Retry Topology

We keep the work exchange orders and its queue orders. We add a retry exchange orders.retry with three queues, one per delay. Each retry queue has a queue-level x-message-ttl, no consumers, and a dead-letter exchange pointing back to orders with the original routing key, so expired messages return to the work queue. Finally, a parking queue orders.parking holds messages that used up all attempts.

Java
Queue retry10s = QueueBuilder.durable("orders.retry.10s")
.ttl(10_000) // x-message-ttl: 10 s for every message
.deadLetterExchange("orders") // back to the work exchange
.deadLetterRoutingKey("order")
.build();
Queue retry1m = QueueBuilder.durable("orders.retry.1m").ttl(60_000)
.deadLetterExchange("orders").deadLetterRoutingKey("order").build();
Queue retry10m = QueueBuilder.durable("orders.retry.10m").ttl(600_000)
.deadLetterExchange("orders").deadLetterRoutingKey("order").build();
Queue parking = QueueBuilder.durable("orders.parking").build();
DirectExchange retryEx = new DirectExchange("orders.retry");
// bindings: retryEx --"10s"--> retry10s, --"1m"--> retry1m, --"10m"--> retry10m, --"park"--> parking
Step throughOne message through the retries
  1. Consume from ordersattempt 1 fails
  2. Publish to orders.retry.10s, ack originalwaits 10 s, then dead-lettered back
  3. Attempt 2 failspublish to orders.retry.1m
  4. Attempt 3 failspublish to orders.retry.10m
  5. Attempt 4 succeeds?
    YesAck: doneNoPublish to orders.parking

5 steps in order. Play them, or step through them one at a time.

Notice that the retry queues are just waiting rooms. Their only job is to hold each message for exactly their TTL and then let the broker dead-letter it back to orders. Because the consumer explicitly chooses the next queue, we can have any number of steps with any delays.

3Why Each Delay Needs Its Own Queue

It is tempting to use a single retry queue and set a per-message expiration on each message: 10000 for the first retry, 600000 for the third. This fails because RabbitMQ only checks expiry at the head of the queue. A message is dead-lettered when it reaches the front and its time is up, not at the exact moment it expires. So if a message with a 10 minute expiration is at the head, a message behind it with a 10 second expiration waits the full 10 minutes too. This is head-of-line blocking.

One retry queue with per-message TTL
PositionMessageExpirationActually returns
headorder-1 (3rd retry)10 minutesafter 10 minutes
2order-2 (1st retry)10 secondsafter 10 minutes: blocked
3order-3 (2nd retry)1 minuteafter 10 minutes: blocked
Expiry is checked at the head, so short delays wait behind long ones.

With one queue per delay and a queue-level TTL, every message in a queue has the same delay, so they expire in the order they arrived and the head is always the next one due. The cost is one queue per delay step, which for three or four steps is entirely reasonable.

4Counting Attempts and Parking the Message

The consumer must know which attempt it is on. The simplest reliable way is our own header, x-retry-count, incremented each time we republish. The x-death header can also be used, since each pass through a retry queue adds to the count of its expired entry, but its structure is more awkward to read, so an explicit header is clearer.

Java
@RabbitListener(queues = "orders")
void onOrder(Message msg) {
try {
process(msg);
} catch (TransientException e) {
int attempt = (int) msg.getMessageProperties().getHeaders().getOrDefault("x-retry-count", 0);
String next = switch (attempt) {
case 0 -> "10s";
case 1 -> "1m";
case 2 -> "10m";
default -> "park"; // 4 attempts used
};
msg.getMessageProperties().setHeader("x-retry-count", attempt + 1);
rabbitTemplate.send("orders.retry", next, msg); // republish with confirms enabled
} // returning normally acks the original
}

Notice the order of operations: we republish first and acknowledge the original only after that, by returning normally. If the pod crashes between the two, the original is redelivered and might be retried twice, which an idempotent consumer tolerates; the reverse order could lose the message. With publisher confirms on the republish, we know the copy is safe before the ack. Permanent errors, such as validation failures, should skip the retries and go straight to parking.

5The Delayed-Message Exchange Plugin and the Trade-Offs

The community delayed-message exchange plugin offers an exchange type that holds a message for the time given in an x-delay header before routing it, so one exchange can serve any delay without head-of-line blocking. It is convenient, but delayed messages are stored on a single node and not replicated, and its support status and compatibility have changed across RabbitMQ versions, so check it for your broker version before depending on it.

Overall, broker delays cost more topology, four extra queues and an exchange per work queue, and they lose ordering, since a retried message returns behind newer ones. In exchange they release the consumer during the wait, spread retries across all consumers and survive restarts. In-process retries remain the right tool for short delays of a second or two, where blocking briefly is cheaper than a round trip through the broker.

6Follow-up questions

Why not reject the message and let the work queue's dead-letter exchange send it to a retry queue?

That works for a single fixed delay: reject without requeue, the DLX routes to one retry queue, and its TTL sends it back. But a queue has only one dead-letter exchange and routing key, so every retry would take the same delay. For increasing delays the consumer has to choose the next queue itself, which is why the design republishes explicitly.

What do you do with messages in the parking queue?

Alert on its depth, inspect the messages with their retry count and last error, which we can add as a header when parking, fix the cause, and replay them to the work exchange with the retry count reset. Parking queues often have no TTL, so nothing is lost while people investigate.

7Conclusion

In this answer, we built delayed retries from plain RabbitMQ features: retry queues with a queue-level TTL and a dead-letter exchange back to the work exchange, one queue per delay of 10 seconds, 1 minute and 10 minutes, because expiry is checked only at the head and mixed delays block each other. The consumer counts attempts in a header, republishes to the next delay before acknowledging, and parks the message after the last attempt. Compared with in-process retries, broker delays free consumer threads and survive restarts at the cost of more queues and lost ordering, and the delayed-message plugin is an alternative whose limits and support we check first.

What to say in the interviewAbout 100 seconds aloud

Open with

In-process retries, with Spring AMQP's RetryInterceptorBuilder or the Boot retry properties, are fine for short back-offs, but during the wait the message stays unacknowledged on that consumer and the thread is blocked, so a ten minute delay is not acceptable, and a restart loses the retry state.

Then, if they want more

So I would move the waiting into the broker using TTL and dead-lettering. I create three retry queues, for 10 seconds, 1 minute and 10 minutes. Each has no consumers, a queue-level message TTL, and a dead-letter exchange pointing back to the work exchange with the work routing key, so when a message expires it returns to the orders queue.

When processing fails with a transient error, the consumer reads an attempt counter from a header, increments it, republishes the message to the next retry queue with publisher confirms, and then acknowledges the original. After the third retry fails, it publishes to a parking queue instead, and we alert on that queue.

It has to be one queue per delay, because RabbitMQ only expires messages at the head of a queue: with per-message TTLs in a single queue, a 10 second retry would wait behind a 10 minute one. The trade-offs are more topology and loss of ordering, in exchange for free consumer threads and retries that survive restarts.

The delayed-message exchange plugin is an alternative, but it does not replicate delayed messages and its support status has changed, so I would check it for our version.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
RabbitMQ documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Implement delayed retries with RabbitMQ for an order consumer: retry after 10 seconds...": https://scrivio-interview-library.netlify.app/spring-boot/rabbitmq-and-jms/56f1ea983c/