🟢 Node.js Intermediate

What is a message queue and why is it used in Node.js applications?

Answer

A message queue is a communication mechanism where producers add messages to a queue and consumers process them asynchronously, decoupling the sender from the receiver in time and space. Benefits: (1) Async processing — handle heavy work (email sending, PDF generation, payment processing) in background workers without blocking the HTTP request; (2) Reliability — if a worker fails, the message stays in the queue for retry; (3) Load leveling — absorb traffic spikes by queuing excess work; (4) Decoupling — services don't need to know about each other directly. Node.js solutions: BullMQ (Redis-based, most popular for Node.js — reliable, feature-rich with delayed jobs, priority, repeatable jobs); RabbitMQ with amqplib (AMQP protocol, advanced routing); Apache Kafka with kafkajs (high-throughput event streaming). Example: user registers → API immediately returns 200 → background job sends welcome email. This prevents slow email sending from delaying the API response.