Amazon Simple Queue Service (Amazon SQS) is a distributed queue messaging service. The idea of SQS is to remove the direct associations between producer and consumer and act as mediator between them.
Consider that you have a large application like websites monitoring which involve many stages like
- Downloading a website
- Processing the downloaded website
- Generating report of the above processing
So instead of clubbing the above three one can just split them on their specific need(based on the work they do) with each doing their respective task and on completion notify the other about it completion.
The traditional approach (called as Client-Server architecture) is to send the notification using a simple HTTP , REST , SOAP etc request. But it too has some disadvantage as it require extra care and precision to handle all possible response e.g Time-out,Server down etc.
One can save this time and effort my using SQS.
e.g Instead of sending the direct request to Server one can use to SQS to interact with Server/Client an can eventually cut down some of drawback of traditional architecture.
Setting up Amazon SQS in Ruby: There are various library available for using Amazon SQS in Ruby (e.g right_aws, sqs)
- ruby-library(right_aws or sqs gem)
both of which is provide by the Amazon (or you can ask for incase) during the process of account-registration
In your irb console
require "rubygems" require "right_aws" aws_access_key_id = "Your access key id" aws_secret_access_key = "Your secret access Key" sqs = RightAws::SqsGen2.new(aws_access_key_id,aws_secret_access_key)
This basically initialize the sqs object for you to work on.
One can create a new queue just by a single method.
queue(queue_name, create=true, visibility=nil)
carefully look at parameter supplied.
queue_name => name of queue.
create => true (create a queue if it does not exist (optional)).
visibility => set visibility (which make the message reappear after a specified time if not delete from queue).
One key point about visibility is that one can’t set visibility more than 7200 sec for a queue or a message .But you can set so from RightScale (Cloud Computing management Platform).
my_queue = queue('queue_1')
just look at few other methods.
1. send_message or push :- Send message to the specified queue.
my_queue.send_message "This is my first message"
2. receive or pop :- Receive message from the specified queue.
message_received = my_queue.receive puts message_received.body => "This is my first message"
2.1 receive_messages : – There is also a method called receive_messages to receive an array of messages but it never returned me more than one message so if it work for you it fine then syntax is like this
receive_messages(number_of_message=1,visibility=nil) my_queue.receive_messages(2,60) => return and array of messages
3. delete : – Delete a message or a queue
my_queue.delete => "delete the my_queue for SQS"
message_received.delete => "delete the message from the intended queue"
4. size :- Specify the no of message in the queue
my_queue.size => 4 ("4" message in my_queue)
5. name :- Specify name of the queue
my_queue.name => "queue_1" (Name of the queue)
Note : – There is delay lag in all request and response sent/received to/from SQS server don’t assume to work it instantaneous approximately of around 45 sec -1 minute(can be more)
If you send a message to the queue don’t assume it to appear instantaneously in the queue
Pricing Model => $0.10 per 100,000 requests.
Message Size => 64 KB
For more information on Pricing and message size click here.
1 comments On Amazon Simple Queing Service (SQS) + Ruby