Skip to content
Phuc's Blog
Go back

Understanding Message Brokers

Edit page

When building modern web applications, one of the biggest challenges is handling high traffic. If too many users hit your application at once, your servers can easily run out of memory, exhaust database connections, and crash.

This is where a Message Broker comes to the rescue. In this post, we’ll explain how message brokers work, how they protect your servers from crashing, and when to use them.

Message Broker

Table of contents

Open Table of contents

Problems & Solutions

1. Synchronous Communication

Diagram illustrating synchronous communication dependency between services

Photo by Phuc Dang ;))

In a traditional request-response model, Service A calls Service B and must wait (block) until Service B finishes and responds.

2. High traffic

Graph illustrating a sudden traffic spike and server overload

Photo by Phuc Dang ;))

A sudden surge of incoming requests (e.g., during a flash sale or viral marketing campaign) forces your servers to process everything at once.

3. Send multiple services

Diagram showing direct coupling when one service sends data to multiple other services

Photo by Phuc Dang ;))

Sometimes a single event needs to trigger actions in multiple services (e.g., user signup needs to trigger an email, update analytics, and notify the billing system).

What is a Message Broker?

Think of a message broker as a digital post office.

Instead of you delivering a letter directly to your friend’s hands (synchronous communication), you drop it in a mailbox. The post office holds the letter and delivers it to your friend when they are ready.

In software:


Best Use Cases: Preventing Server Crashes - The Power of Buffering (Load Leveling)

To understand how a message queue saves your server from crashing, let’s look at two scenarios:

Scenario A: Without a Message Broker (Direct HTTP)

Imagine you run an e-commerce store during a flash sale.

  [ Client Requests ]
          │
          │ (10,000 req/sec)
          ▼
    [ Web Server ] ──(Direct Blocking Calls)──► [ Database ]
          │
          ▼
  💥 CRASH! (OOM / Timeout)
  1. 10,000 users click “Buy Now” at the exact same second.
  2. Your web server tries to handle all 10,000 requests simultaneously. It has to charge credit cards, generate invoices, and write to the database.
  3. The server memory spikes, the database runs out of available connections, and the entire system crashes.
  4. Your users get a frustrating 502 Bad Gateway error.

Scenario B: With a Message Broker (Queue-Based Load Leveling)

Now, let’s introduce a message queue as a buffer.

  [ Client Requests ]
          │
          │ (10,000 req/sec)
          ▼
    [ Web Server ]
          │
          │ (Write Task to Queue)
          ▼
   [ Message Queue ]  ◄── (Stores & buffers the load)
          │
          │ (Steady stream of tasks)
          ▼
  [ Background Worker ] ──(Process 500 req/sec)──► [ Database ]
  1. The same 10,000 users click “Buy Now”.
  2. The web server does not process the orders immediately. Instead, the web server quickly pushes 10,000 task messages into the Message Queue and immediately responds to the user: “Your order is being processed!” (Response time: < 50ms). The Message Queue itself does not communicate with the user.
  3. The Message Queue safely stores all 10,000 messages.
  4. Your Background Workers pull and process messages from the queue at a steady, manageable pace (e.g., 500 orders per second).
  5. The database stays healthy, the server never runs out of memory, and no orders are lost.

This pattern is called Load Leveling or Traffic Shaving. It turns a massive, unpredictable spike of traffic into a smooth, constant stream of work.


Core Messaging Models

Most message brokers support two main ways of sending messages:

1. Point-to-Point (Queue)

A message is sent to a queue and is processed by exactly one worker. Once processed, it is deleted.

2. Publish-Subscribe (Pub/Sub)

In the Pub/Sub model, messages are broadcasted in a one-to-many fashion. The producer publishes a message to a Topic, and the broker duplicates and forwards that message to all services subscribed to that topic.

                       ┌──► [ Subscriber A: Email Service ]
                       │
  [ Publisher ] ──► ( Topic ) ──┼──► [ Subscriber B: Analytics Service ]
                       │
                       └──► [ Subscriber C: Billing Service ]

How it works:


RabbitMQ vs. Apache Kafka: A Quick Comparison

If you’re looking to adopt a message broker, you will most likely choose between these two:


Conclusion

Using a message broker is a cheat code for building reliable backend systems. By decoupling your web servers from heavy background processing, you protect your infrastructure from traffic spikes, prevent database crashes, and provide a much better user experience.


Edit page
Share this post:

Previous Post
Hangfire Background Job
Next Post
Optimistic vs Pessimistic Concurrency Control

Comments