Message Queues & Async Messaging
Overview
Asynchronous messaging decouples producers from consumers in time and identity, smoothing load spikes and improving resilience. Apache Kafka is a partitioned, replayable log; RabbitMQ is a broker built around queues and exchanges. Both force you to reason about delivery guarantees, ordering, and idempotency. Read this before introducing a queue or event bus between services.
Official sources
Core concepts
- Kafka topics and partitions: messages are published to topics split into partitions; ordering is guaranteed only within a single partition, and consumers track position via offsets.
- Kafka consumer groups: a consumer group acts as one logical subscriber across multiple processes; any number of groups can consume the same topic without duplicating data.
- Delivery semantics: systems offer at-most-once (may lose, never redelivers), at-least-once (never loses, may redeliver), or exactly-once. Kafka guarantees at-least-once by default.
- Kafka exactly-once: from Kafka 0.11 the idempotent producer prevents retry-induced duplicates, and transactions with read-committed consumers enable exactly-once across read-process-write.
- RabbitMQ exchanges and routing: producers publish to exchanges that route to queues by binding/routing keys, enabling work queues, publish/subscribe, routing, and topic patterns.
- Acknowledgements and confirms: consumer acks and publisher confirms underpin reliable delivery — a message is only considered handled once acknowledged.
Best practices
- Make consumers idempotent: because at-least-once delivery can redeliver messages, design handlers so processing the same message twice has no extra effect.
- Use Kafka partition keys deliberately when you need per-key ordering, since ordering holds only within a partition.
- Acknowledge after successful processing (manual acks / publisher confirms), not on receipt, so failures lead to redelivery rather than loss.
- Reserve exactly-once for when you truly need it (Kafka transactions/idempotent producer); it adds overhead and is not always necessary if consumers are idempotent.
Common pitfalls
- Assuming global ordering across a Kafka topic → ordering is per-partition only; key related messages to the same partition if order matters.
- Acking a message before the work succeeds → message is lost on crash; ack only after the side effects are durable.
- Ignoring duplicate delivery under at-least-once → double charges/sends; dedupe with idempotency keys or upserts.
Examples
Idempotent consumer under at-least-once delivery:
on message m with key m.id:
if processed_ids.contains(m.id): ack(m); return # dedupe
do_work(m)
processed_ids.add(m.id)
ack(m) # ack after success
Further reading
Related skills
- ../software-architecture-patterns — event-driven architecture and queue patterns
- ../scalability-reliability — queues for load leveling and resilience
- ../system-design-fundamentals — throughput and partitioning trade-offs
1---2name: message-queues-async3description: Async messaging with queues and logs — delivery guarantees, ordering, and idempotency (Kafka, RabbitMQ); read before decoupling services.4domain: engineering5category: architecture6tags: [messaging, queues, kafka, rabbitmq, idempotency]7official_sources:8 - https://kafka.apache.org/documentation/9 - https://www.rabbitmq.com/tutorials10verified: 2026-06-1611metadata:12 source: https://skillmd.com/skills/gabriel7silva/message-queues-async13---1415# Message Queues & Async Messaging1617## Overview18Asynchronous messaging decouples producers from consumers in time and identity, smoothing load spikes and improving resilience. Apache Kafka is a partitioned, replayable log; RabbitMQ is a broker built around queues and exchanges. Both force you to reason about delivery guarantees, ordering, and idempotency. Read this before introducing a queue or event bus between services.1920## Official sources21- Apache Kafka — Documentation (concepts, design, delivery semantics): https://kafka.apache.org/documentation/22- RabbitMQ — Tutorials (queues, exchanges, work queues, pub/sub): https://www.rabbitmq.com/tutorials2324## Core concepts25- **Kafka topics and partitions**: messages are published to topics split into partitions; ordering is guaranteed only within a single partition, and consumers track position via offsets.26- **Kafka consumer groups**: a consumer group acts as one logical subscriber across multiple processes; any number of groups can consume the same topic without duplicating data.27- **Delivery semantics**: systems offer at-most-once (may lose, never redelivers), at-least-once (never loses, may redeliver), or exactly-once. Kafka guarantees at-least-once by default.28- **Kafka exactly-once**: from Kafka 0.11 the idempotent producer prevents retry-induced duplicates, and transactions with read-committed consumers enable exactly-once across read-process-write.29- **RabbitMQ exchanges and routing**: producers publish to exchanges that route to queues by binding/routing keys, enabling work queues, publish/subscribe, routing, and topic patterns.30- **Acknowledgements and confirms**: consumer acks and publisher confirms underpin reliable delivery — a message is only considered handled once acknowledged.3132## Best practices33- Make consumers idempotent: because at-least-once delivery can redeliver messages, design handlers so processing the same message twice has no extra effect.34- Use Kafka partition keys deliberately when you need per-key ordering, since ordering holds only within a partition.35- Acknowledge after successful processing (manual acks / publisher confirms), not on receipt, so failures lead to redelivery rather than loss.36- Reserve exactly-once for when you truly need it (Kafka transactions/idempotent producer); it adds overhead and is not always necessary if consumers are idempotent.3738## Common pitfalls39- Assuming global ordering across a Kafka topic → ordering is per-partition only; key related messages to the same partition if order matters.40- Acking a message before the work succeeds → message is lost on crash; ack only after the side effects are durable.41- Ignoring duplicate delivery under at-least-once → double charges/sends; dedupe with idempotency keys or upserts.4243## Examples44```text45Idempotent consumer under at-least-once delivery:46 on message m with key m.id:47 if processed_ids.contains(m.id): ack(m); return # dedupe48 do_work(m)49 processed_ids.add(m.id)50 ack(m) # ack after success51```5253## Further reading54- Kafka design — message delivery semantics: https://kafka.apache.org/documentation/#semantics55- RabbitMQ publisher confirms tutorial: https://www.rabbitmq.com/tutorials/tutorial-seven-java5657## Related skills58- ../software-architecture-patterns — event-driven architecture and queue patterns59- ../scalability-reliability — queues for load leveling and resilience60- ../system-design-fundamentals — throughput and partitioning trade-offs