After we published 3 posts about microservices and AWS Beanstalk, we are going to switch a little the topic – without permanently abandoning this theme (sooner we’ll debate more interesting cases) – and to discuss about how to reduce your costs to SQS.
AWS SQS is one of the first service launched and an essential piece in the distributed systems puzzle. It has a very simple API, being based on 2 operations: put and get messages. In this post, we are going to discuss 2 aspects that could decrease your monthly AWS bill.
SQS has a very simple pricing plan, billing customers according to the number of requests they make. Thus, we have to focus how to reduce number of calls without affecting the performance of our service. One simple idea is to fetch multiple messages during a receieveMessage call, maximum being 10, without exceeding 256Kb per request. See below how to do this.
ReceiveMessageRequest request = new ReceiveMessageRequest() .withQueueUrl("your_queue_url") .withMaxNumberOfMessages(10);
Another thing to consider is to make receiveMessages calls waiting for a while or until it accumulates the desired number of messages in the queue. This works perfect if you have a worker that reads and processes the messages from the queue, you can configure it to wait for a while (maximum is 20 seconds) in order to avoid empty results. You have to balance the wait time, even more you set to return multiple messages, because it could increase the processing time for all your messages. A concrete example: you requested to return 8 messages, but in the queue are only 7. Also, you set the wait time to 15 seconds. This means that all the 7 messages will be delayed until the 8th message appears in the queue or the 15 seconds passes.
To set this wait time interval, you have 2 options:
1. As a global queue configuration
2. As a value set per request
ReceiveMessageRequest request = new ReceiveMessageRequest() .withQueueUrl("your_queue_url") .withMaxNumberOfMessages(10) .withWaitTimeSeconds(15);
Please don’t hesitate to send us your feedback about this post and to share it with anyone could benefit from it!