Why should I use Amazon Kinesis and not SNS-SQS?

Amazon Web-ServicesAmazon SqsAmazon Kinesis

Amazon Web-Services Problem Overview


I have a use case where there will be stream of data coming and I cannot consume it at the same pace and need a buffer. This can be solved using an SNS-SQS queue. I came to know the Kinesis solves the same purpose, so what is the difference? Why should I prefer (or should not prefer) Kinesis?

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

Keep in mind this answer was correct for Jun 2015

After studying the issue for a while, having the same question in mind, I found that SQS (with SNS) is preferred for most use cases unless the order of the messages is important to you (SQS doesn't guarantee FIFO on messages).

There are 2 main advantages for Kinesis:

  1. you can read the same message from several applications
  2. you can re-read messages in case you need to.

Both advantages can be achieved by using SNS as a fan out to SQS. That means that the producer of the message sends only one message to SNS, Then the SNS fans-out the message to multiple SQSs, one for each consumer application. In this way you can have as many consumers as you want without thinking about sharding capacity.

Moreover, we added one more SQS that is subscribed to the SNS that will hold messages for 14 days. In normal case no one reads from this SQS but in case of a bug that makes us want to rewind the data we can easily read all the messages from this SQS and re-send them to the SNS. While Kinesis only provides a 7 days retention.

In conclusion, SNS+SQSs is much easier and provides most capabilities. IMO you need a really strong case to choose Kinesis over it.

Solution 2 - Amazon Web-Services

On the surface they are vaguely similar, but your use case will determine which tool is appropriate. IMO, if you can get by with SQS then you should - if it will do what you want, it will be simpler and cheaper, but here is a better explanation from the AWS FAQ which gives examples of appropriate use-cases for both tools to help you decide:

FAQ's

Solution 3 - Amazon Web-Services

Kinesis support multiple consumers capabilities that means same data records can be processed at a same time or different time within 24 hrs at different consumers, similar behavior in SQS can be achieved by writing into multiple queues and consumers can read from multiple queues. However writing again into multiple queue will add sub seconds {few milliseconds} latency in system.

Second, Kinesis provides routing capability to selective route data records to different shards using partition key which can be processed by particular EC2 instances and can enable micro batch calculation {Counting & aggregation}.

Working on any AWS software is easy but with SQS is easiest one. With Kinesis, there is a need to provision enough shards ahead of time, dynamically increasing number of shards to manage spike load and decrease to save cost also required to manage. it's pain in Kinesis, No such things are required with SQS. SQS is infinitely scalable.

Solution 4 - Amazon Web-Services

Semantics of these technologies are different because they were designed to support different scenarios:

  • SNS/SQS: the items in the stream are not related to each other
  • Kinesis: the items in the stream are related to each other

Let's understand the difference by example.

  1. Suppose we have a stream of orders, for each order we need to reserve some stock and schedule a delivery. Once this is complete, we can safely remove the item from the stream and start processing the next order. We are fully done with the previous order before we start the next one.
  2. Again, we have the same stream of orders, but now our goal is to group orders by destinations. Once we have, say, 10 orders to the same place, we want to deliver them together (delivery optimization). Now the story is different: when we get a new item from the stream, we cannot finish processing it; rather we "wait" for more items to come in order to meet our goal. Moreover, if the processor process crashes, we must "restore" the state (so no order will be lost).

Once processing of one item cannot be separated from processing another one, we must have Kinesis semantics in order to handle all the cases safely.

Solution 5 - Amazon Web-Services

The biggest advantage for me is the fact that Kinesis is a replayable queue, and SQS is not. So you can have multiple consumers of the same messages of Kinesis (or the same consumer at different times) where with SQS, once a message has been ack'd, it's gone from that queue. SQS is better for worker queues because of that.

Solution 6 - Amazon Web-Services

Excerpt from AWS Documentation:

> We recommend Amazon Kinesis Streams for use cases with requirements that are similar to the following: > > * Routing related records to the same record processor (as in streaming MapReduce). For example, counting and aggregation are simpler when all records for a given key are routed to the same record processor. > > * Ordering of records. For example, you want to transfer log data from the application host to the processing/archival host while maintaining the order of log statements. > > * Ability for multiple applications to consume the same stream concurrently. For example, you have one application that updates a real-time dashboard and another that archives data to Amazon Redshift. You want both applications to consume data from the same stream concurrently and independently. > > * Ability to consume records in the same order a few hours later. For example, you have a billing application and an audit application that runs a few hours behind the billing application. Because Amazon Kinesis Streams stores data for up to 7 days, you can run the audit application up to 7 days behind the billing application. > > We recommend Amazon SQS for use cases with requirements that are similar to the following: > > * Messaging semantics (such as message-level ack/fail) and visibility timeout. For example, you have a queue of work items and want to track the successful completion of each item independently. Amazon SQS tracks the ack/fail, so the application does not have to maintain a persistent checkpoint/cursor. Amazon SQS will delete acked messages and redeliver failed messages after a configured visibility timeout. > > * Individual message delay. For example, you have a job queue and need to schedule individual jobs with a delay. With Amazon SQS, you can configure individual messages to have a delay of up to 15 minutes. > > * Dynamically increasing concurrency/throughput at read time. For example, you have a work queue and want to add more readers until the backlog is cleared. With Amazon Kinesis Streams, you can scale up to a sufficient number of shards (note, however, that you'll need to provision enough shards ahead of time). > > * Leveraging Amazon SQS’s ability to scale transparently. For example, you buffer requests and the load changes as a result of occasional load spikes or the natural growth of your business. Because each buffered request can be processed independently, Amazon SQS can scale transparently to handle the load without any provisioning instructions from you.

Solution 7 - Amazon Web-Services

Another thing: Kinesis can trigger a Lambda, while SQS cannot. So with SQS you either have to provide an EC2 instance to process SQS messages (and deal with it if it fails), or you have to have a scheduled Lambda (which doesn't scale up or down - you get just one per minute).

Edit: This answer is no longer correct. SQS can directly trigger Lambda as of June 2018

https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html

Solution 8 - Amazon Web-Services

The pricing models are different, so depending on your use case one or the other may be cheaper. Using the simplest case (not including SNS):

  • SQS charges per message (each 64 KB counts as one request).
  • Kinesis charges per shard per hour (1 shard can handle up to 1000 messages or 1 MB/second) and also for the amount of data you put in (every 25 KB).

Plugging in the current prices and not taking into account the free tier, if you send 1 GB of messages per day at the maximum message size, Kinesis will cost much more than SQS ($10.82/month for Kinesis vs. $0.20/month for SQS). But if you send 1 TB per day, Kinesis is somewhat cheaper ($158/month vs. $201/month for SQS).

Details: SQS charges $0.40 per million requests (64 KB each), so $0.00655 per GB. At 1 GB per day, this is just under $0.20 per month; at 1 TB per day, it comes to a little over $201 per month.

Kinesis charges $0.014 per million requests (25 KB each), so $0.00059 per GB. At 1 GB per day, this is less than $0.02 per month; at 1 TB per day, it is about $18 per month. However, Kinesis also charges $0.015 per shard-hour. You need at least 1 shard per 1 MB per second. At 1 GB per day, 1 shard will be plenty, so that will add another $0.36 per day, for a total cost of $10.82 per month. At 1 TB per day, you will need at least 13 shards, which adds another $4.68 per day, for a total cost of $158 per month.

Solution 9 - Amazon Web-Services

Kinesis solves the problem of map part in a typical map-reduce scenario for streaming data. While SQS doesnt make sure of that. If you have streaming data that needs to be aggregated on a key, kinesis makes sure that all the data for that key goes to a specific shard and the shard can be consumed on a single host making the aggregation on key easier compared to SQS

Solution 10 - Amazon Web-Services

I'll add one more thing nobody else has mentioned -- SQS is several orders of magnitude more expensive.

Solution 11 - Amazon Web-Services

Kinesis Use Cases

  • Log and Event Data Collection
  • Real-time Analytics
  • Mobile Data Capture
  • “Internet of Things” Data Feed

SQS Use Cases

  • Application integration
  • Decoupling microservices
  • Allocate tasks to multiple worker nodes
  • Decouple live user requests from intensive background work
  • Batch messages for future processing

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionApoorvView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesRoee GavirelView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesE.J. BrennanView Answer on Stackoverflow
Solution 3 - Amazon Web-ServiceskartikView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesKonstantin TrigerView Answer on Stackoverflow
Solution 5 - Amazon Web-ServicesMatthew CurryView Answer on Stackoverflow
Solution 6 - Amazon Web-ServicescloudtechnicianView Answer on Stackoverflow
Solution 7 - Amazon Web-ServicesDenNukemView Answer on Stackoverflow
Solution 8 - Amazon Web-ServicesJohn VelonisView Answer on Stackoverflow
Solution 9 - Amazon Web-Servicesbhanu tadepalliView Answer on Stackoverflow
Solution 10 - Amazon Web-ServicesEugene FeingoldView Answer on Stackoverflow
Solution 11 - Amazon Web-ServicesSharhabeel HamdanView Answer on Stackoverflow