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
- Order pipeline — user gets immediate ACK; payment, stock, email, analytics run asynchronously.
- Traffic spikes — queue absorbs bursts; workers drain at sustainable rate.
- Microservices — publish OrderCreated; downstream services subscribe independently.
- Centralized logs/metrics — Kafka-style log aggregation.
- Heavy jobs — image resize, encoding, fan-out to worker pools.
RabbitMQ vs Kafka (rule of thumb)
| RabbitMQ | Kafka | |
|---|---|---|
| Model | Classic broker, routing | Durable log / streaming |
| Retention | Often delete after ack | Time/size retention |
| Throughput | Very high | Extremely high for logs |
| Ordering | Per queue | Per partition |
| Fits | RPC-ish, work queues | Event 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.
Related posts
- 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.