3) Smart Routing (Direct / Topic)
Send messages only to the right workers. This reduces waste and keeps hot paths fast.
flowchart LR P1[Order Service] -->|order.us.created| T((Topic exchange: orders)) P2[Admin Tool] -->|order.eu.cancelled| T T -->|bind = order.us.*| QUS[Queue: us-orders] T -->|bind = order.#| QALL[Queue: all-orders] QUS --> C1[Consumer: US] QALL --> C2[Consumer: All]
Direct = exact match.
Topic = patterns (*
for one word, #
for many).
Tips
- Design clear routing keys (e.g.,
order.<region>.<event>
). - Avoid hot partitions (one key that gets all traffic). If needed, shard by hash, suffix, or use multiple queues.
4) Request / Reply (RPC) — use with care
Sometimes a service needs a direct answer. You can emulate RPC over RabbitMQ using reply-to
and correlation-id
.
sequenceDiagram participant Client participant Broker as RabbitMQ participant Svc as Worker Service Client->>Broker: publish (corr_id=123, reply-to=tempq) Broker->>Svc: deliver request Svc-->>Broker: publish reply (corr_id=123, to=tempq) Broker-->>Client: deliver reply (match corr_id)
Best practice
- Use only for short, read-style calls.
- Set timeouts and fallbacks.
- Prefer async events for heavy work.
5) Retries, Backoff, and DLQs
Not all work succeeds on the first try. Plan for safe retries and isolation of bad messages.
flowchart TD Q[(Main queue)] --> C[Consumer] C -- "fail" --> R{retry < max?} R -- "yes" --> DQ[Delay/Retry queue] DQ -->|"TTL expires"| Q R -- "no" --> DLX((Dead-letter exchange)) --> DLQ[(Dead-letter queue)]
Options
- Immediate retry:
Nack(requeue=true)
(careful: can hot-loop). - Delayed retry: send to a delay/TTL queue, then back to main.
- After N tries: dead-letter to a DLQ for inspection.
Store retry count in message headers (e.g., x-retry
) and use exponential backoff.
Category: RabbitMQ