Open PortfolioOpen Portfolio.
← Back to Blog

How to Use Message Brokers for Async Processing

May 7, 2026at 2:01 PM UTCBy Pocket Portfolio TeamTechnology
How to Use Message Brokers for Async Processing
#message brokers#async processing#distributed systems

In modern distributed systems, handling asynchronous processing efficiently is crucial for scalability and resilience. Message brokers are a fundamental tool in achieving this, allowing you to decouple your applications and manage communication between different parts of your system.

Problem

In a synchronous system, tasks are processed one at a time, which can lead to bottlenecks and high latency. For example, if a web application needs to send an email confirmation after a user registration, waiting for the email service to finish can delay the response time to the user. This is where message brokers come in—enabling asynchronous processing to avoid these delays and improve performance.

Solution with Code

Message brokers like RabbitMQ, Apache Kafka, and Amazon SQS facilitate communication between services by sending messages through queues. Here's a simple example using RabbitMQ to handle a user registration event asynchronously.

Setting Up RabbitMQ

First, ensure RabbitMQ is installed and running. You can use Docker for a quick setup:

docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3-management

Producer: Sending Messages

The producer will send a message to a queue whenever a user registers.

const amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost', (error0, connection) => {
  if (error0) throw error0;
  connection.createChannel((error1, channel) => {
    if (error1) throw error1;
    const queue = 'user_registrations';
    const msg = JSON.stringify({ username: 'newuser', email: 'user@example.com' });

    channel.assertQueue(queue, { durable: false });
    channel.sendToQueue(queue, Buffer.from(msg));
    console.log(" [x] Sent %s", msg);
  });
});

Consumer: Receiving Messages

The consumer will process messages from the queue, such as sending a confirmation email.

const amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost', (error0, connection) => {
  if (error0) throw error0;
  connection.createChannel((error1, channel) => {
    if (error1) throw error1;
    const queue = 'user_registrations';

    channel.assertQueue(queue, { durable: false });
    console.log(" [*] Waiting for messages in %s", queue);

    channel.consume(queue, (msg) => {
      const user = JSON.parse(msg.content.toString());
      console.log(" [x] Received %s", user);
      // Simulate sending email
      console.log(`Sending email to ${user.email}`);
    }, { noAck: true });
  });
});

Key Concepts

  • Decoupling: Message brokers help separate the producer and consumer, allowing them to scale independently.
  • Reliability: By storing messages in a queue, brokers ensure that no messages are lost if a consumer is unavailable temporarily.
  • Scalability: As demand grows, you can add more consumers to process messages concurrently, enhancing throughput.

Message brokers are vital in building resilient and scalable systems, making them a cornerstone of modern application architecture. By implementing async processing, you can significantly improve your application's performance and user experience.

How to Use Message Brokers for Async Processing | Open Portfolio Blog | Open Portfolio