Amazon Simple Queue Service or Amazon SQS is a distributed message queuing service that enables developers to build loosely coupled solutions. Often valued for its ease of use, Amazon SQS queues can be spun up in a matter of seconds from the AWS console, SDK or the CLI. Still, there are subtleties that developers should be aware of when developing solutions with Amazon SQS.
Photo by Shumilov Ludmila on Unsplash
Encryption
Messages stored in an Amazon SQS queue is data at rest. And, we can protect that data by encrypting it with KMS keys, just like we would if we were going to protect data in an Amazon S3 bucket or an Amazon SNS topic. With Amazon SQS Server-side encryption with KMS, messages are encrypted when they are received by SQS and are decrypted when delivered to a message consumer that is authorized for the SQS message queue and the KMS key.
Alternatively, Amazon Simple Queue Service provides it's own server-side encryption using SQS-managed encryption keys. Encrypting an SQS queue using SQS-managed encryption keys is as easy as using the following command:
$ aws sqs set-queue-attributes \
––queue-url https://(MyQueueURL) \
––attributes '{"SqsManagedSseEnabled": "true"}'
Temporal Messaging
By default, messages that are stored in Amazon SQS are designed for "At-Least-Once Delivery". That is, an Amazon SQS message will be delivered at least once, but can be delivered more than once and there is no guarantee that the messages will stay ordered. However, if you need messages delivered in order and only once, you may opt for Amazon SQS FIFO queues. Amazon SQS FIFO queues are designed for, Exactly-Once Processing, where messages are only delivered once and are delivered in the order of, First-In-First-Out.
Below is an example of sending a message to an Amazon SQS FIFO queue using the AWS .NET SDK. Note the use of the "MessageGroupId" property. This is a requirement for FIFO messaging.
SendMessageRequest sendMessageRequest = new SendMessageRequest(queueUrl, message);
sendMessageRequest.MessageGroupId = "message-group-1";
var sqsClient = new AmazonSQSClient();
await sqsClient.SendMessageAsync(sendMessageRequest);
Message Retention
Messages in Amazon SQS can not be stored forever. In fact, messages in Amazon SQS can be stored for a maximum of 14 days and for as little as 1 minute. By default, the Amazon SQS message retention period is a generous 4 days.
The following is an example of a command for setting an Amazon SQS queue to have a retention period of 1 minute using the AWS CLI.
$ aws sqs set-queue-attributes \
––queue-url https://(MyQueueURL) \
––attributes '{"MessageRetentionPeriod": "60'
Want to know more about the tech in this article? Checkout these resources:
Top comments (0)