C++ Message Queues: RabbitMQ and Kafka Integration Guide [#50-7]

C++ Message Queues: RabbitMQ and Kafka Integration Guide [#50-7]

이 글의 핵심

Async workflows with message brokers: RabbitMQ task queues vs Kafka event logs, C++ client patterns, JSON/Protobuf payloads, and production tuning.

Introduction: “Sync calls are the bottleneck”

If service A calls B over HTTP and waits, A pays B’s full latency (DB, external APIs, heavy CPU). Message queues let A publish work and return quickly—decoupling producers and consumers and buffering spikes.

Topics:

  • RabbitMQ (AMQP) — exchanges, queues, acks (e.g. SimpleAmqpClient patterns)
  • Kafka — topics, partitions, consumer groups (e.g. librdkafka)
  • Serialization — JSON, Protobuf
  • Errors, tuning, production patterns

Environment: C++17+.


When queues help

  1. Order pipeline — user gets immediate ACK; payment, stock, email, analytics run asynchronously.
  2. Traffic spikes — queue absorbs bursts; workers drain at sustainable rate.
  3. Microservices — publish OrderCreated; downstream services subscribe independently.
  4. Centralized logs/metrics — Kafka-style log aggregation.
  5. Heavy jobs — image resize, encoding, fan-out to worker pools.

RabbitMQ vs Kafka (rule of thumb)

RabbitMQKafka
ModelClassic broker, routingDurable log / streaming
RetentionOften delete after ackTime/size retention
ThroughputVery highExtremely high for logs
OrderingPer queuePer partition
FitsRPC-ish, work queuesEvent logs, analytics

Architecture sketch

flowchart LR
    P[C++ producer] -->|AMQP| RQ[RabbitMQ]
    P2[C++ producer] -->|Kafka protocol| K[Kafka]
    RQ --> C[C++ consumer]
    K --> C2[C++ consumer]

Implementation notes

  • Acknowledgements: acknowledge after successful processing; tune prefetch for fairness.
  • Idempotency: consumers may see duplicates—design handlers accordingly.
  • Serialization: versioned Protobuf for evolution; JSON for simplicity.
  • Errors: DLQs, retry limits, poison message handling.

  • Kafka C++
  • RabbitMQ C++

Summary

Message queues turn synchronous chains into async, resilient pipelines. Choose RabbitMQ for flexible routing and classic messaging; Kafka for high-volume log-style streams and replay.