BBackend CourseLeroy Labs
0/0
Search the course

RabbitMQ, covering exchanges, queues, DLQs, retries & resilience

AMQP model (producer → exchange → binding → queue → consumer), exchange types, durability, acks, retry with backoff, dead-letter exchanges, poison messages, and RM's broker-down fallback.

● advancedReport-Manager-2IR3D (Kontentt)

The AMQP model

Producer --publish(routingKey)--> EXCHANGE --binding--> QUEUE --> Consumer(s)
                                     |                     \-- reject/expire --> DLX --> DLQ
Exchange type Routing
direct Exact routing-key match (email, push, export)
topic Wildcards (report.*.submitted, audit.#)
fanout Every bound queue gets a copy (broadcast)
headers Matches on header values
  • Durable queues and persistent messages survive a broker restart.
  • Acks: the consumer acknowledges after processing. If it dies before acking, the message is redelivered.
  • Competing consumers: several instances on one queue share the load.
  • Prefetch (spring.rabbitmq.listener.simple.prefetch) limits unacked messages per consumer, for fairness and memory.

Spring AMQP setup

@Bean DirectExchange exchange() { return new DirectExchange("report-manager.exchange"); }
@Bean DirectExchange dlx()      { return new DirectExchange("report-manager.dlx"); }

@Bean Queue emailQueue() {
    return QueueBuilder.durable("email.queue")
        .withArgument("x-dead-letter-exchange", "report-manager.dlx")
        .withArgument("x-dead-letter-routing-key", "email.dlq")
        .build();
}
@Bean Queue emailDlq() { return QueueBuilder.durable("email.dlq").build(); }
@Bean Binding emailBinding() { return BindingBuilder.bind(emailQueue()).to(exchange()).with("email"); }

@RabbitListener(queues = "email.queue")
public void onEmail(EmailMessage msg) { mailer.send(msg); }   // throw -> retry -> DLQ
spring.rabbitmq.listener.simple:
  retry: { enabled: true, max-attempts: 3, initial-interval: 1000, multiplier: 2 }
  default-requeue-rejected: false     # after retries are exhausted -> dead-letter, NOT an infinite requeue loop

Poison messages

A message that always fails (malformed, or referencing deleted data) would loop forever if requeued. Limited retries followed by the DLQ park it for inspection and let the queue keep moving.

RabbitMQ with retries and a dead-letter queueThe producer publishes to a direct exchange, which routes by key to the email, push and export queues. Consumers retry failures three times with backoff; messages that still fail are rejected without requeue and routed by the dead-letter exchange to a DLQ, where a dead-letter consumer marks the export job failed.Producerafter commitExchangedirectemail.queuepush.queueexport.queueConsumerretry ×3DLXexport.dlqDLQ consumerjob → FAILEDstill failing
With default-requeue-rejected: false, a poison message gets parked in the DLQ instead of looping forever and blocking the queue.

In your projects

Report-Manager-2shared/config/RabbitMQConfig.java · EmailConsumer · PushConsumer · ExportConsumer · ExportDeadLetterConsumer
  • A direct exchange report-manager.exchange plus a DLX.
  • Durable email.queue, push.queue and export.queue, each dead-lettering to its own .dlq.
  • Retry 3× with backoff, and default-requeue-rejected: false.
  • ExportDeadLetterConsumer reads the export DLQ and marks the export job FAILED, so the user sees “export failed” instead of an endless spinner.
  • ResilientPublisher: if publishing throws AmqpException (broker down), the handler runs in-process on a thread pool after commit (ResilientPublisherTest).
  • In dev, RabbitMQ runs with the management UI and the Prometheus plugin, and a Grafana rabbitmq-overview.json dashboard shows queue depth.
IR3D (Kontentt)shared/infrastructure/messaging/EmailQueueConfig · PushDispatchQueueConfig · BroadcastQueueConfig · DeadLetterService

Each module owns its queue config (email dispatch, push dispatch, broadcast batches), each with x-dead-letter-exchange. BroadcastBatchListener fans one admin broadcast out to many users in batches. DeadLetterService lets admins inspect and replay dead letters from the dashboard. Publishing is after-commit via TransactionalRabbitPublisher.

Alternatives

Broker Model Pick when
RabbitMQ (yours) Smart broker, queues, routing, per-message acks Task queues, RPC, complex routing
Kafka Distributed append-only log, consumer offsets, replay Event streaming, analytics, high throughput, event sourcing
AWS SQS / SNS Managed queue / pub-sub On AWS with no ops (IR3D could use it)
Redis Streams / BullMQ Lightweight You already run Redis
Postgres queue (SKIP LOCKED, pgmq) No new infra Moderate volume
NATS / Pulsar Modern alternatives Low latency or multi-tenant streaming
  • Delivery semantics. At-least-once is the norm, so consumers must be idempotent.
  • Message TTL and delayed retry. Use a TTL queue that dead-letters back to the main queue, or the delayed-message plugin, for exponential retry without blocking a consumer thread.
  • Message schema evolution. Version payloads, and keep consumers tolerant of unknown fields.
  • Monitoring. Watch queue depth, consumer count, unacked count and DLQ size. Alert when the DLQ is above 0.
  • Ordering holds only per queue with a single consumer; competing consumers break global order.

Check yourself

1. Why set default-requeue-rejected=false?
2. Event log with replay for many consumer groups → choose…
3. What does RM’s ExportDeadLetterConsumer do?