Kafka vs SQS¶
1. Decision¶
Choose AWS SQS for zero-operational-overhead, serverless, pay-per-message background task queuing and microservice decoupling on AWS; choose Apache Kafka for high-throughput event streaming, log-based event sourcing, message replayability, strict partition ordering, and real-time stream processing analytics.
2. 30-Second Answer¶
AWS Simple Queue Service (SQS) is a fully managed, serverless, point-to-point message queuing service provided by AWS. SQS requires zero cluster management, scales automatically from zero to millions of messages, and operates on a pay-per-request pricing model ($0.40 per million requests). Messages are transient: competing worker nodes pull tasks, process them, and delete them upon completion. Apache Kafka is a distributed event streaming platform built on an immutable, partitioned append-only log. Kafka requires running server broker clusters (self-hosted or MSK), but provides multi-consumer log retention, event replayability, microsecond stream processing, and sub-millisecond local network throughput. Choose SQS for serverless cloud task queues; choose Kafka for event-driven streaming architectures.
3. Decision Matrix¶
| Option | Best For | Avoid When | Latency | Scalability | Consistency | Operational Complexity |
|---|---|---|---|---|---|---|
| AWS SQS (Standard / FIFO) | Serverless AWS microservices, background job worker queues, unpredictable bursty task spikes, zero ops management | High-volume log retention, message replayability, real-time stream analytics, multi-gigabyte ingestion | Low (20-50ms API network RTT) | Fully Managed Serverless (Virtually unlimited throughput auto-scaling) | Transient; Deleted on worker deletion ACK | Zero (Fully Managed Serverless Cloud) |
| Apache Kafka (Self-Hosted / MSK) | High-velocity telemetry, CDC streaming (Debezium), event sourcing, stream processing (Flink), multi-consumer log analytics | Simple serverless task queues with low/intermittent traffic volume | Ultra-Low (<10ms batch throughput optimized) | High (Linear scale-out via partition topic sharding) | Durable immutable log; Strict partition ordering | Medium to High (Broker cluster / MSK provisioning) |
4. Selection Rules¶
| Requirement | Best Choice | Rationale / Example |
|---|---|---|
| Zero Operational Overhead (Serverless) | AWS SQS | No brokers to manage, no partition rebalancing, automatic capacity auto-scaling |
| Event Replayability & Log Retention | Kafka | Retain events for 30+ days; rewind consumer group offsets to re-process historical events |
| Ultra-High Throughput Ingestion (>100k msg/sec) | Kafka | Sequential disk log appends and Linux Zero-Copy delivery outperform HTTP API queuing |
| AWS Lambda Serverless Event Triggers | AWS SQS | SQS triggers AWS Lambda functions natively with automated concurrency scaling |
| Pay-Per-Use Pricing for Bursty Traffic | AWS SQS | Scale to zero cost when idle; pay strictly per million SQS API requests |
| Strict Ordering Across Partition Keys | Kafka | Partition keys (user_id) guarantee sequence ordering across high-volume event streams |
5. Tradeoffs¶
- Fully Managed Serverless vs Cluster Operations Management
- Gain (SQS): Zero infrastructure maintenance; AWS manages availability, durability, and scaling.
- Lose (SQS): Cloud vendor lock-in to AWS; API network request costs scale with volume.
- Pay-Per-Request Pricing vs Fixed Broker Instance Provisioning
- Gain (SQS): Cheaper for low-to-medium or bursty intermittent workloads (scales to zero).
- Lose (SQS): Continuous high-throughput workloads (>100,000 QPS) become drastically more expensive on SQS than Kafka instances.
- Transient Task Deletion vs Immutable Log Replayability
- Gain (SQS): Automatic queue cleanup; messages deleted immediately upon worker ACK.
- Lose (SQS): Cannot replay historical messages to new consumer applications.
- Competing Consumer Load Balancing vs Partition-Bounded Concurrency
- Gain (SQS): Dynamically add 500 Lambda workers to process a backed-up queue without partition limits.
- Lose (SQS): Standard SQS does not guarantee message delivery ordering.
- Visibility Timeout vs Consumer Group Offset Commits
- Gain (SQS): If a worker crashes during processing, the message automatically reappears in the queue after Visibility Timeout.
- Lose (SQS): Worker must process and delete within visibility window or duplicate executions occur.
6. Interview Counter Questions¶
- What is the difference between AWS SQS Standard Queues and SQS FIFO Queues?
- How does SQS Visibility Timeout work and what happens if a worker exceeds it?
- How do you implement Fanout messaging using AWS SNS + SQS?
- What is SQS Long Polling (
WaitTimeSeconds=20) and why should you always enable it? - Why is sustained 200,000 msg/sec streaming significantly cheaper on Kafka (AWS MSK) than SQS?
- How does message deduplication work in SQS FIFO Queues (
MessageDeduplicationId)? - What happens when SQS message consumption fails repeatedly (SQS Dead-Letter Queues)?
- How does AWS Lambda integrate natively with SQS vs Kafka as an event source?
- When would you choose AWS Kinesis Data Streams over Apache Kafka and AWS SQS?
- How do you handle poison-pill messages in AWS SQS?
7. Gold Standard Answers¶
- Standard: At-least-once delivery, best-effort ordering, nearly unlimited throughput. FIFO: Exactly-once processing, strict FIFO message ordering per Message Group ID, throughput capped at 300 msg/sec (or 3,000 with batching).
- Visibility Timeout hides a pulled message from other workers. If the worker fails to delete the message before the timeout expires (e.g., 30s), SQS makes the message visible again for another worker to process.
- SNS (Pub/Sub) fans out a single published message to multiple SQS queues (competing worker task pools), allowing independent microservices to consume a dedicated copy of the event asynchronously.
- Long Polling (
WaitTimeSeconds=20) holds theReceiveMessageAPI call open for up to 20s until a message arrives, eliminating empty API calls, reducing cost, and lowering latency. - SQS charges \(0.40 per million API requests. At 200,000 QPS continuous write + read, SQS generates millions of API calls daily (~\)15,000+/mo). Kafka running on provisioned EC2/MSK instances amortizes infrastructure cost independently of request volume.
- SQS FIFO uses
MessageDeduplicationIdor content-based SHA-256 hashing to reject duplicate messages published within a 5-minute deduplication interval window. - SQS tracks
ApproximateReceiveCount. When a message is received but not deleted \(N\) times (maxReceiveCount), SQS automatically moves it to a configured Dead-Letter Queue (DLQ). - SQS \(\rightarrow\) Lambda: AWS manages event polling and scales Lambda instances up to
ReservedConcurrentExecutions. Kafka \(\rightarrow\) Lambda: AWS Lambda Event Source Mapping polls Kafka partitions, scaling 1 Lambda container per partition. - AWS Kinesis is a fully managed cloud alternative to Kafka (partitioned streams, log retention). Choose Kinesis for native AWS streaming without managing Kafka brokers; choose SQS for simple task queues; choose Kafka for open-source ecosystem & higher throughput.
- Set
maxReceiveCount(e.g., 3 retries). When a poison pill repeatedly crashes the Lambda/worker, SQS moves it to the SQS DLQ, unblocking the main task queue automatically.
8. Common Mistakes¶
- Using SQS Standard Queues when strict FIFO message processing order is required.
- Short polling SQS (
WaitTimeSeconds=0), burning thousands of dollars on empty API requests. - Setting SQS Visibility Timeout shorter than worker execution time, causing duplicate parallel processing.
- Choosing Kafka over SQS for simple, low-volume background tasks, adding immense operational broker management overhead.
- Attempting to implement multi-consumer event replayability using AWS SQS.
9. Keywords¶
AWS SQS, SQS Standard, SQS FIFO, Kafka, MSK, Visibility Timeout, SNS + SQS Fanout, Long Polling, WaitTimeSeconds, MessageDeduplicationId, MessageGroupId, Dead-Letter Queue (DLQ), AWS Kinesis, Serverless.
10. Cheat Sheet¶
- Core Difference: SQS = Fully managed serverless task queue (AWS native, transient task deletion); Kafka = Distributed event streaming log (Multi-consumer replay, high throughput).
- SQS Queues: Standard = Unlimited scale, at-least-once, best-effort order; FIFO = 300 msg/sec limit, strict order per Message Group ID.
- Cost Rule: SQS is cheaper for low/bursty traffic (scales to zero); Kafka is far cheaper for continuous high-throughput streams (>100k QPS).
- Long Polling: Always set SQS
WaitTimeSeconds=20to eliminate empty API request billings. - Visibility Timeout: Ensure SQS Visibility Timeout is set longer than maximum worker processing time.
- Fanout Pattern: Combine AWS SNS (Pub/Sub) + SQS (Queues) to broadcast single events to multiple queue consumers.