๐จ Messaging Systems - Kafka, RabbitMQ, SQS & Pub/Sub
Donโt ask โwhich is better?โ Ask โWhat are the delivery guarantees I need?โ and โHow do I want to handle state?โ Choosing a broker is a trade-off between throughput, latency, and the complexity of the consumer logic.
๐ฆ Broker Architectures: Two Main Philosophies
Section titled โ๐ฆ Broker Architectures: Two Main Philosophiesโ1. Message Queues (Smart Broker, Dumb Consumer)
Section titled โ1. Message Queues (Smart Broker, Dumb Consumer)โThe broker tracks which messages are consumed and deletes them once acknowledged.
- RabbitMQ: High feature set (routing, priorities). Best for complex workflows.
- AWS SQS: Fully managed, infinitely scalable, but simpler routing logic.
- Behavior: Usually Point-to-Point. One message is processed by exactly one consumer.
2. Log-based Streaming (Dumb Broker, Smart Consumer)
Section titled โ2. Log-based Streaming (Dumb Broker, Smart Consumer)โThe broker is a distributed append-only log. It doesnโt track consumption; consumers track their own โoffsetโ (position in the log).
- Apache Kafka / Amazon MSK: Built for massive throughput and data retention.
- Google Cloud Pub/Sub: A managed hybrid that scales globally.
- Behavior: Fan-out. The same stream of events can be read by multiple different service groups simultaneously.
๐ The Big Three Comparison
Section titled โ๐ The Big Three Comparisonโ| Feature | RabbitMQ | Apache Kafka | AWS SQS |
|---|---|---|---|
| Model | Push (Broker pushes to consumer) | Pull (Consumer requests data) | Pull (Short/Long polling) |
| Persistence | Deleted after ACK | Persistent (Retention policy) | Deleted after ACK |
| Ordering | Guaranteed within a queue | Guaranteed within a Partition | Best-effort (or strict with FIFO) |
| Scaling | Vertical / Cluster-based | Horizontal (Adding partitions) | Native / Serverless |
| Best For | Task Queues, RPC, Complex Routing | Log Aggregation, Event Sourcing, Big Data | Decoupling Microservices (Cloud-native) |
๐ ๏ธ Delivery Guarantees
Section titled โ๐ ๏ธ Delivery GuaranteesโYou must choose which โlieโ you can live with:
- At-Most-Once: Messages may be lost, but never duplicated. (Fastest, lowest overhead).
- At-Least-Once: Messages are never lost, but may be delivered more than once. (The industry standard). Requires consumers to be idempotent.
- Exactly-Once: Theoretically impossible across a network, but โeffectivelyโ achieved by Kafka through transactional writes and idempotent producers. (Highest overhead).
โ๏ธ Strategic Decision Framework
Section titled โโ๏ธ Strategic Decision FrameworkโWhen to choose RabbitMQ?
Section titled โWhen to choose RabbitMQ?โ- You need complex routing (e.g., using Header or Topic exchanges).
- You need built-in Message Priority.
- You are working with legacy protocols like AMQP or MQTT.
When to choose Kafka?
Section titled โWhen to choose Kafka?โ- You need to replay data (e.g., rebuilding a database from an event log).
- You have massive throughput requirements (millions of events per second).
- You are implementing Event Sourcing or CQRS.
When to choose SQS?
Section titled โWhen to choose SQS?โ- You are in the AWS ecosystem and want Zero Maintenance.
- You need to handle huge spikes in volume without managing a cluster.
- Your architecture is mostly โFire and Forgetโ task processing.
๐ฉ Common Pitfalls
Section titled โ๐ฉ Common PitfallsโThe โPoison Pillโ Message
Section titled โThe โPoison Pillโ MessageโA message that causes a consumer to crash every time itโs read.
- Solution: Use Dead Letter Queues (DLQ). After X failed retries, the broker moves the message to a separate queue for manual debugging.
Backpressure & Slow Consumers
Section titled โBackpressure & Slow ConsumersโIf the producer is faster than the consumer, the queue grows.
- RabbitMQ: Can run out of memory and crash.
- Kafka: Disk space fills up, but doesnโt affect broker performance as much.
- Strategy: Monitor Consumer Lag religiously. If lag increases, scale your consumer instances.
๐ก Note: A message broker is stateful infrastructure. It is much harder to maintain than a stateless API. Before adding Kafka to your stack, ask if a simple Redis Pub/Sub or even a Database-backed queue (like Postgres
SKIP LOCKED) is enough for your current scale.