← Blog'a Dön
ARCHITECTURE

What Is a Message Queue? Async Communication with RabbitMQ & Kafka

F. Çağrı Bilgehan15 Şubat 202612 dk okuma
message queuerabbitmqkafkaasync

What Is a Message Queue? Async Communication Guide

Are your microservices waiting on each other? Losing data under heavy traffic? Message queues decouple your services, making them resilient, independent, and scalable.

What Is a Message Queue?

A message queue is a middleware that enables asynchronous communication between applications. The producer places a message in the queue; the consumer retrieves and processes it when ready.

Sync:   Service A ──────→ Service B (wait!) ──────→ Response
Async:  Service A ──→ Queue ──→ Service B (when ready)
                        ↑
                   Message is safe

Why Use Message Queues?

1. Decoupling

Services aren't directly dependent. If one crashes, the others keep running.

2. Load Leveling

During traffic spikes, messages accumulate in the queue. Consumers process at their own pace.

3. Durability

Messages are persisted. Even if a consumer crashes, the message survives.

4. Scalability

Add more consumers to increase processing capacity horizontally.

Core Messaging Patterns

Point-to-Point (Queue)

Each message is processed by exactly one consumer:

Producer → [Queue] → Consumer A
                  → Consumer B (gets the next message)

Pub/Sub (Publish/Subscribe)

Each message is delivered to all subscribers:

Publisher → [Topic] → Subscriber A (gets a copy)
                   → Subscriber B (gets a copy)
                   → Subscriber C (gets a copy)

RabbitMQ vs Apache Kafka

| Feature | RabbitMQ | Apache Kafka | |---------|----------|-------------| | Model | Traditional queue | Log-based stream | | Throughput | ~10K msg/s | 1M+ msg/s | | Retention | Delete after ack | Indefinite retention | | Ordering | Queue-level | Partition-level | | Use case | Task queues, RPC | Event streaming, ETL | | Protocol | AMQP | Custom binary |

RabbitMQ Example

// Producer
const amqp = require('amqplib');
const conn = await amqp.connect('amqp://localhost');
const channel = await conn.createChannel();

await channel.assertQueue('orders');
channel.sendToQueue('orders', Buffer.from(JSON.stringify({
  orderId: 123,
  items: ['laptop', 'mouse'],
  total: 150
})));
// Consumer
channel.consume('orders', (msg) => {
  const order = JSON.parse(msg.content.toString());
  console.log('Order received:', order.orderId);
  processOrder(order);
  channel.ack(msg);
});

Kafka Example

const { Kafka } = require('kafkajs');
const kafka = new Kafka({ brokers: ['localhost:9092'] });

// Producer
const producer = kafka.producer();
await producer.send({
  topic: 'user-events',
  messages: [
    { key: 'user-42', value: JSON.stringify({ event: 'purchase', amount: 500 }) }
  ]
});

// Consumer
const consumer = kafka.consumer({ groupId: 'analytics-group' });
await consumer.subscribe({ topic: 'user-events' });
await consumer.run({
  eachMessage: async ({ message }) => {
    const event = JSON.parse(message.value.toString());
    console.log('Event:', event);
  }
});

Common Use Cases

  1. Email sending — Don't block the user waiting for the email to send
  2. Order processing — Orchestrate payment, inventory, and shipping steps
  3. Logging & analytics — Stream application logs into analysis pipelines
  4. Notifications — Send push notifications, SMS, and emails asynchronously

Best Practices

  1. Write idempotent consumers — Handle duplicate messages gracefully
  2. Use Dead Letter Queues (DLQ) — Isolate messages that fail processing
  3. Add retry mechanisms — Retry transient failures with backoff
  4. Monitor queue depth — Alert on growing backlogs
  5. Keep messages small — Send references, not large payloads

Conclusion

Message queues are foundational to modern distributed systems. They provide async communication, fault tolerance, and scalability. Choose RabbitMQ for simple task queues, Kafka for large-scale event streaming.

Learn message-driven architecture interactively on the LabLudus platform.

İlgili Yazılar

What Is Software Architecture? A Comprehensive Guide

What is software architecture, why does it matter, and how do you learn it? A deep dive into architectural patterns, quality attributes, and the architect's career path.

What Are Architecture Decision Records (ADRs)?

Architecture Decision Records explained: what they are, why every team needs them, and how ArchLog makes creating them effortless.