intermediate
Queues
Smooth spikes and decouple slow work with queues, consumer concurrency, retries, dead-letter handling, and idempotent jobs.
Queues decouple producers from slow consumers—smoothing spikes, retrying work, and isolating failure domains. Examples: SQS, RabbitMQ, BullMQ, Kafka (log-oriented).
| Concern | Practice | |---------|----------| | Concurrency | Limit parallel consumers per queue | | Retries | Exponential backoff + max attempts | | DLQ | Park poison messages for inspection | | Idempotency | Handlers safe on duplicate delivery |
API → enqueue job → Worker pool → side effects (email, PDF, webhook)
Monitor **queue depth** and **age of oldest message**—lag is a scalability signal. Scale consumers when lag grows, not only when CPU is high.
On interviews: at-least-once vs exactly-once; ordering guarantees; backpressure when queue grows unbounded; idempotent payment handlers.
Common pitfalls: unbounded queue hiding overload; no DLQ; consumers faster than downstream DB can handle.
The trade-off is resilience and elasticity versus eventual consistency and operational monitoring.
Checklist:
- Bounded retries with DLQ.
- Idempotent consumers with dedupe keys.
- Alert on lag and oldest-message age.
- Match consumer concurrency to downstream capacity.