Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon sns & sqs messages with java

I have 2 different systems (A an B) that communicates using amazon sqs. System A sends messages to system B.

Currently system B gets the messages using a separated thread that starts when the server goes up. Here is the run method:

@Override
public void run() {
    while (true) {
        ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
        try {
            receiveMessageRequest.setWaitTimeSeconds(1);
            List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();

            for (Message message : messages) {
             // process messages
            }
        }
    }
}

Looking at this code I have a feeling it is not efficient since it uses a busy waiting loop. I would expect to get the messages using some sort of push mechanism.

Reading a bit about amazon sqs and sns this seems possible using http (server B can expose servlets for that) but still I'm a bit confused.

  • Which one (sns or sqs) should provide me this ability (pushing a message to server B)?
  • What is the easiest way of doing it (any reference to code)?
like image 878
forhas Avatar asked May 29 '14 06:05

forhas


People also ask

What is an Amazon SNS topic?

An Amazon SNS topic is a logical access point that acts as a communication channel. A topic lets you group multiple endpoints (such as AWS Lambda, Amazon SQS, HTTP/S, or an email address).

What is the difference between Amazon SQS and Amazon SNS?

In simple terms, SNS - sends messages to the subscriber using push mechanism and no need of pull. SQS - it is a message queue service used by distributed applications to exchange messages through a polling model, and can be used to decouple sending and receiving components.

What is SNS subscription in AWS?

To receive messages published to a topic, you must subscribe an endpoint to the topic. When you subscribe an endpoint to a topic, the endpoint begins to receive messages published to the associated topic.


1 Answers

Of the two, only SNS can be used to push a message to system B. SQS can be used to have system B poll for messages.

It depends on your workload/application requirements as to which is the better solution. If you use SNS, then a big spike in notifications generated by system A will result in a spike in workload of system B, which may not be able to handle the load.

If you were using a queue, the the load spike would be buffered by Amazon SQS, and would not directly impact your system B. This helps decouple system B from system A, and provides a buffer between them. This means you can shutdown system B, do maintenance, then bring the system back up and carry on processing messages like nothing happened (assuming your application can handle delayed processing of messages).

The other main advantage I see in a queue is that it makes scaling the application simpler, since you can start a new instance running a consumer and now you can process messages at a higher rate (assuming no other bottlenecks in your system).

Another consideration is message delivery semantics. Does it matter if messages are delivered out of order or delivered multiple times? I haven't used SNS much, so am unsure of its semantics around this. SQS is a distributed queue, so you may receive messages out of order, or even multiple times in some cases. Can your application deal with this?

SQS supports long polling, allowing you to specify a period to wait (up to 20s) for messages before the receiveMessage call returns. This can limit the number of requests you are making to SQS, thereby costing you less to run. This may be slightly less efficient than SNS, but in applications I have build using SQS, it is not worth worrying about. See http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html.

As for examples, the AWS Java SDK available on GitHub includes usage examples of various AWS services. See https://github.com/aws/aws-sdk-java/blob/master/src/samples/AmazonSimpleQueueService/SimpleQueueServiceSample.java and http://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.example.java.html for SQS and SNS examples.

You could also combine both SNS and SQS. ie you push a message to a single SNS topic, and have SNS push copies of the message into multiple queues. See http://docs.aws.amazon.com/sns/latest/dg/SendMessageToSQS.html

like image 109
aj.esler Avatar answered Oct 09 '22 10:10

aj.esler