Kafka vs SQS: A Comprehensive Comparison
Published: 2024-06-15 06:36:18

## Introduction

Comparing Apache Kafka and Amazon SQS (Simple Queue Service) involves understanding their architectures, use cases, and performance characteristics. Both are popular messaging systems but are designed for different purposes and scenarios.

## High-Level Overview
Apache Kafka is a distributed streaming platform that is used for building real-time streaming data pipelines and applications. It is known for its high throughput, fault tolerance, and scalability. Kafka is often used for building real-time analytics, log aggregation, and event-driven architectures.

Amazon SQS, on the other hand, is a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. SQS offers reliable message delivery and can handle high message throughput.

## How Kafka Works?

![How Kafka works?](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8g2g45avp152ohgzcjr7.png)


- **Kafka Broker:** A Kafka broker is a server that stores and manages Kafka topics. It is responsible for receiving messages from producers, storing them on disk, and serving them to consumers.
- **Topic:** A topic is a category or feed name to which records are published. Topics in Kafka are similar to tables in a database. They help in organizing and segregating messages.
- Partition: Topics in Kafka are divided into partitions to parallelize data across multiple brokers. Each partition is an ordered, immutable sequence of records that is continually appended to.
- **Producer:** A producer is a client application that publishes records to Kafka topics. Producers are responsible for choosing which record to assign to which partition within the topic.
- **Consumer:** A consumer is a client application that reads records from Kafka topics. Consumers subscribe to one or more topics and process records in the order they are stored in the partition.
- **Consumer Group:** A Consumer Group is a collection of consumers that work together to consume and process records from Kafka topics. Each consumer in the group reads data from a subset of the partitions in the topic(s) assigned to that group.
- **Kafka Record:** A Kafka record is a key-value pair consisting of a key, a value, and metadata. The key and value are byte arrays, and the metadata includes information such as the topic, partition, and offset of the record.

### Basic Functioning of Kafka:

1. **Producers publish records:** Producers send records to Kafka brokers. The producer specifies a topic and, optionally, a key, value, and partition.
2. **Kafka stores records in partitions:** Each partition is an ordered sequence of records. Kafka appends incoming records to the end of the partition.
3. **Consumers subscribe to topics:** Consumers subscribe to one or more topics and read records from partitions. Each consumer is assigned to one partition and reads records in the order they are stored.
4. **Records are processed by consumers:** Consumers process records based on their application logic. Once a record is processed, the consumer commits its offset to Kafka to indicate that it has been processed.
5. **Fault tolerance and scalability:** Kafka provides fault tolerance by replicating partitions across multiple brokers. This ensures that data is not lost in case of a broker failure. Kafka is scalable, allowing you to add more brokers and partitions to handle increased load.
6. **Durability:** Kafka ensures that once a record is written to a partition, it is immutable and will not be lost unless the retention policy expires. This durability guarantee is crucial for applications that require data persistence.
7. **High throughput and low latency:** Kafka is designed to handle high message throughput with low latency, making it suitable for real-time streaming applications.

## How SQS Works?

![How SQS Works?](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxast49cdd3ds1eq8rxa.png)



- **Queue:** An SQS queue is a buffer that stores messages. It acts as a temporary repository for messages that are waiting to be processed.
- **Message:** A message in SQS is a unit of data that contains the payload (the actual data) and metadata (attributes such as message ID, timestamp, etc.). Messages are stored in SQS queues.
- **Producers:** Producers are entities that send messages to SQS queues. They can be applications, services, or systems that generate messages to be processed.
- **Consumers:** Consumers are entities that receive and process messages from SQS queues. They can be applications, services, or systems that retrieve messages from queues for processing.

### Basic Functioning of SQS:

1. **Sending messages to queues:** Producers send messages to SQS queues using the SQS API or SDK. Messages are stored in the queue until they are processed by consumers.
2. **Receiving messages from queues:** Consumers poll SQS queues to receive messages. SQS guarantees that messages are delivered at least once and in the same order they are sent.
3. **Processing messages:** Consumers process messages based on their application logic. Once a message is processed, it is deleted from the queue. If a message cannot be processed successfully, SQS can be configured to retry delivering the message.
4. **Visibility timeout:** SQS provides a visibility timeout for messages. When a consumer receives a message from a queue, the message becomes invisible to other consumers for a specified period. This ensures that only one consumer processes the message at a time.
5. **Dead-letter queues:** SQS allows you to configure a dead-letter queue (DLQ) for messages that cannot be processed successfully after a certain number of retries. Messages sent to the DLQ can be analyzed to identify and fix processing issues.
6. **Scaling:** SQS is designed to scale horizontally to handle large numbers of messages and consumers. You can increase the number of queues, message producers, and consumers to accommodate increased load.
7. **Reliability:** SQS is a fully managed service provided by AWS, ensuring high availability and durability of messages. AWS manages the infrastructure and handles tasks such as message replication and storage.

## Detailed Comparison

### Kafka vs SQS
| Features                | Apache Kafka                                                                                                                                     | Amazon SQS                                                                                          |
|-------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------|
| Deployment              | - Fully Managed by Confluent, AWS MSK Managed Service, Manual Deployment                                                                 | AWS SQS Managed Service                                                                            |
| Scalability             | Horizontally scalable with partitioning and broker replication.                                                                                  | Automatically scales with demand, but individual queues have throughput limits.                      |
| Message Retention       | Configurable retention period for messages, with Confluent also supporting tiered storage.                                                      | Max 14 days.                                                                                         |
| Message Ordering        | Preserves order within a partition based on partition key.                                                                                      | FIFO queue supports ordering but with limited throughput, while the Standard queue does not support ordering but offers high throughput. |
| Message Delivery        | At-least-once, exactly-once, and at-most-once semantics.                                                                                        | Standard Queue - At-least Once<br>FIFO Queue - Exactly Once                                          |
| Message Size Limit      | Limited by broker configuration                                                                                                                 | 256 KiB per message (There are other ways to support larger messages but supported at its core)      |
| Message Visibility      | Messages remain in the queue until consumed or retention period expires                                                                         | Messages become invisible for a specified time when polled by a consumer                             |
| Vendor Lock-in          | Open-source with no vendor lock-in, can be deployed on any infrastructure                                                                       | Tied to AWS, which may limit flexibility in switching to other cloud providers                       |
| Durability              | Data replication across brokers ensures high durability.                                                                                        | Messages are stored redundantly across multiple servers.                                             |
| Communication Pattern   | Pub/Sub Architecture                                                                                                                            | SQS offers producer/consumer queuing pattern and no pub/sub by design, but can be implemented in conjunction with SNS.                     |
| Message ACK             | Auto and Manual Commits                                                                                                                         | Based on Visibility timeout                                                                          |
| Parallelism             | Based on no of partitions in a topic.                                                                                                           | Based on the number of consumers                                                                     |
| Performance             | High throughput and low latency due to efficient batching and partitioning.                                                                     | Good performance but can vary based on message size and queue configuration.                         |
| Integration             | Rich ecosystem with Kafka Streams, Kafka Connect, and integrations with big data tools.                                                         | Strong integration with AWS services like Lambda, SNS, and more.                                     |


## Conclusion
**Use Kafka:** For real-time data pipelines, high-throughput requirements, complex streaming needs, message replay, pub/sub, and when you need fine-grained control over message processing.
**Use SQS:** For simple queueing requirements, easy integration with AWS services, managed service with minimal operational overhead, and when message ordering and deduplication are required.