Quick Comparison: SQS vs Kafka

Quick Comparison: SQS vs Kafka
Photo by Cookie the Pom / Unsplash

Introduction

SQS and Kafka are both tools to implemented asynchronous programming. The difference between SQS and Kafka is similar to the difference of Imperative and Reactive programming.

SQS

SQS (AWS's Simple Queue Service) allows only 1 consumer per message.

Once a message is consumed from SQS queue it stays in queue until the consumer sends an acknowledgement to delete message, or queue receives no response for a time period called the Visibility Timeout.

Visibility timeout is the time after which the message reappears in the queue if initial consumer doesn't send a delete acknowledgement.

The benefit is the same as of a distributed lock (in database terms) where if you have multiple consumers on a queue, only one consumer will receive a message at one time.

Kafka

Kafka allows multiple consumers per message

Kafka approach is more "event driven". If something happens it is an "event in the system" that any subscriber (consumer) can receive.

This allows multiple services to react to an event.

However a single message is consumed by multiple consumers independently at a single time there is no "locking of message to one service"

Conclusion

Depending on your usec-ase you may use SQS or Kafka. If your messages need to read by only one service (like your message mean "Send Mr Parmu 5 rupees") its better to have only one service consume it.

If multiple consumer were to consume it the transaction will get executed multiple times. SQS ensures that doesn't happen.

Kafka on the other hand is better when you want multiple consumers to react to an event. Example: "user completed level 1", it'll impact multiple services: one may send congratulation email, one may unlock level 2.. etc".