Node.js Course
Node.js
/
Advanced

Message Queues

Definition

A form of asynchronous service-to-service communication used in serverless and microservices architectures. Messages are stored on the queue until they are processed and deleted.

Explain Like I'm New

A To-Do List for your servers. Imagine an e-commerce site on Black Friday. 10,000 people buy a TV in one second. If the server tries to process 10,000 payments and send 10,000 emails instantly, it will explode. Instead, the server instantly writes 10,000 sticky notes ('Charge Bob', 'Charge Alice') and puts them in a basket (The Message Queue). A background worker grabs a sticky note, does the work, throws the note away, and grabs the next one. The system stays perfectly stable.

Real World Example

RabbitMQ, Apache Kafka, or AWS SQS. Used for background jobs like processing uploaded videos. The user uploads an MP4, the server puts a message in the queue 'Process Video #123', and returns 'Video uploading' to the user instantly.

Common Use Cases

  • •Asynchronous background processing
  • •Decoupling microservices
  • •Buffering massive traffic spikes

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • Does the user have to wait for the Message Queue to finish processing the message?

intermediate

  • What happens if the background worker crashes while processing a message from the queue?

Flash Cards

Question

Does the user wait?

Click to reveal answer
Answer

No! That is the point. The API instantly replies 'Request Received' as soon as the message hits the queue. The actual processing happens asynchronously in the background.

Question

What if the worker crashes?

Click to reveal answer
Answer

Most queue systems require an 'Acknowledgment' (ACK). If the worker crashes before sending the ACK, the Message Queue realizes it failed and puts the sticky note back into the basket so another worker can try again. No data is lost!