Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
AWS is a cloud computing service provider that allows organizations to store, access, and manage their data in the cloud. Many companies are shifting their business to the cloud to make their data safe and easily accessible. AWS provides various services, amazon simple queue service (Amazon SQS) is one of the services provided by AWS. Let us look into it.
Amazon Simple Queue Service
Amazon Simple Queue Service (SQS) is a fully managed cloud-based queue service in the Amazon Web Services (AWS) cloud. A queue is a location where messages can be stored until they are pulled from the queue or expire. Queues are used to save textual information so that it may be received and used later by a consumer. Amazon Simple Queue Service (Amazon SQS) provides secure, persistent, and available hosted queues that enable you to integrate and decouple distributed software systems and components. Amazon SQS provides common constructs such asdead-letter queues andcosts allocation tags. It provides a generic web service API that can be accessed in any programming language that supports the AWS SDKs.
Amazon SQS queues are classified into two types: first-in, first-out (FIFO), and standard queues. Message strings in FIFO queues are kept in the order in which they were transmitted and received. FIFO queues can send, receive, or discard up to 300 messages per second. FIFO queues are intended for application communications when the sequence of activities and events is crucial.
Standard queues try to store message strings in the order they were sent, however processing needs may affect the original order or sequence of messages. Standard queues, for example, can be used to batch messages for future processing or to assign jobs to numerous worker nodes.
There are three main parts of a distributed message system.
The components of our distributed system.
The queue (allocated on Amazon SQS servers).
The messages in the queue.
In the following example, our system has many producers and consumers. The producer is the component of the distributed system that sends messages to the queue, and consumers receive the messages from the queue. The queue holds the messages from A to E and redundantly stores the messages in different Amazon SQS servers. An Amazon SQS message undergoes three primary states. It is first sent to a queue by the producer. The message is then received from the queue by the consumer and then it is deleted from the queue.
We can see below the lifecycle of an Amazon SQS message in a queue.
A producer (component 1) sends Message A to a queue, and the message is distributed redundantly across Amazon SQS servers.
When a consumer (component 2) is ready, it consumes messages from the queue and returns message A. Message A remains in the queue and is not returned to future receive requests for the duration of the visibility timeout.
When the visibility timeout is reached, the consumer (component 2) removes message A from the queue to prevent it from being received and processed again.
List queue pagination
The list queues and listDeadLetterQueues API methods support optional pagination controls. By default, these API methods return up to 1000 queues in response messages. You can set the MaxResults parameter to reduce the results returned in each response.
Set the MaxResults parameter in a listQueues or listDeadLetterQueues request to specify the maximum number of results to return in the response. If you don't set MaxResults, the response will contain a maximum of 1,000 results, and the NextToken value in the response will be null.
Setting MaxResults will include the value of NextToken in the response if there are more results to display. To get the next page of results, use NextToken as a parameter in the next request to listQueues. If there are no more results to display, the NextToken value in the response will be null.
When a consumer receives and processes a message from a queue, the message remains in the queue. The consumer deletes the message from the queue after receiving and processing it. Thus, immediately after receiving the message, it remains in the queue. To prevent other users from processing the message again, Amazon SQS sets a visibility timeout, during which other consumers are prevented from receiving the message.
Amazon SQS delay queues
Delay queues allow you to delay the delivery of new messages to consumers by a few seconds. For Example, When the consumer application needs extra time to process the message. When you create a delay queue, messages you send to the queue are invisible to consumers for the duration of the delay. The default (minimum) delay for queues is 0 seconds. The maximum is 15 minutes. For information on how to configure delay queues using the console, see Configuring Queue Parameters (Console).
Delay queues are similar to visibility timeouts in that both features make messages unavailable to consumers for a period of time. The difference between the two is that for deferred queues messages are hidden when they are first added to the queue, whereas for visibility timeout messages are only hidden after they have been consumed from the queue. to be. The following diagram shows the relationship between delay queues and visibility timeouts.
To set delay seconds for individual messages instead of the entire queue, use message timers to allow Amazon SQS to use the message timer's DelaySeconds value instead of the delay queue's DelaySeconds value.
Amazon SQS temporary queues
Temporary queues help save development time and deployment costs when using common message patterns such as request/response. The Temporary Queue Client allows you to create low-cost, high-throughput, application-managed temporary queues.
The client automatically maps multiple temporary queues (application-managed queues created on demand for a specific process) to a single Amazon SQS queue. This allows your application to make fewer API calls and increase throughput when there is less traffic to each temporary queue. The client automatically cleans up the temporary queue when it is no longer in use, even if some processes using the client have not been gracefully shut down.
The advantages of temporary queues are:
Acts as a lightweight communication channel for a specific thread or process.
Can be created and deleted at no additional charge.
API is compatible with static (regular) Amazon SQS queues. This means that existing code that sends and receives messages can send messages to and receive messages from virtual queues.
Amazon SQS message timers
A message timer allows you to set an initial period of time after which messages added to the queue are hidden. For example, if I send a message with a timer of 45 seconds, the consumer will not see the message for the first 45 seconds in the queue. The default (minimum) delay for messages is 0 seconds. The maximum is 15 minutes. For information on how to send messages using timers from the console, see Sending Messages to Queues (Console).
Use delay queues to set delay times for entire queues instead of individual messages. The message timer setting for single messages overrides the DelaySeconds value for Amazon SQS delay queues.
The need for Amazon SQS
Amazon SQS removes server dependency. The message in Amazon SQS is stored in queues and even if the application server crashes, the message in the queue is marked invisible in the timeout interval window. When the timeout runs out, the message is reappeared in the queue. Then, a new EC2 instance can use the message to perform its job.
5. The console displays the Send and receive messages page.
6. Enter the message text in the Message body.
7. Enter a Message group ID for a First-In-First-Out (FIFO) queue. For detailed information, please visit FIFO delivery logic.
8. Choose to Send a message.
The console displays a success message when your message is sent. You can also choose View details to display information about the sent message.
Receiving and deleting messages (console)
After sending a message to a queue, you can receive it and delete it. When requesting a message from a queue, you cannot specify which message to retrieve. Instead, specify the maximum number of messages to retrieve (up to 10).
Amazon SQS does not automatically delete messages after retrieving them if they could not be successfully received (for example, the consumer is down or the connection is lost). To delete a message, you must send another request to confirm that you have successfully received and processed the message. Please note that the message must be received before it can be deleted. How to receive and delete
5. The console displays the Send and receive messages page.
6. Choose Poll for messages.
7. Amazon SQS begins to poll for messages in the queue. The progress bar on the right side of the Receive messages section displays the duration of polling.
8. The Messages section displays a list of the received messages. For each message, the list displays the message ID, Sent date, Size, and Receive count.
9. To delete messages, choose the messages that you want to delete and choose Delete.
10. In the Delete Messages dialog box, choose Delete.
Purging messages from an Amazon SQS queue (console)
If you don't want to delete the Amazon SQS queue, but you want all messages from the queue to be deleted, delete the queue. The message deletion process can take up to 60 seconds. We recommend waiting 60 seconds, regardless of queue size.
Most of the time you can use a long query to determine if the queue is empty. In rare cases, you may receive an empty response even though the queue still contains messages. Especially if you specify a low value to wait for messages to arrive when you create the queue. This section describes how to check that the queue is empty.
At the top right of the monitoring dashboard, select the down arrow next to the refresh icon. Select Auto Update from the dropdown menu. Leave the refresh interval at 1 minute.
Observe the following dashboards:
Approximate No. Of Messages Delayed
Approximate No. Of Messages Not Visible
Approximate No. Of Messages Visible
When all of them show 0 values for several minutes, the queue is empty.
To confirm that a queue is empty (AWS CLI, AWS API)
3. Observe the metrics for the following attributes:
ApproximateNumberOfMessagesDelayed
ApproximateNumberOfMessagesNotVisible
ApproximateNumberOfMessages
The queue is empty when all of them are 0 for several minutes
If you rely on Amazon CloudWatch metrics, make sure you see multiple consecutive zero data points before considering this queue empty. For more information about CloudWatch metrics, see CloudWatch Metrics Available for Amazon SQS.
Benefits of using Amazon SQS
Security - We control who can send and receive messages from an Amazon SQS queue. Server-side encryption (SSE) let us send sensitive data by protecting the contents of messages in queues with keys managed by AWS Key Management Service (AWS KMS).
Availability - Amazon SQS uses redundant infrastructure. This allows us concurrent access to the messages and high availability for producing and consuming messages.
Scalability - Amazon SQS processes each delayed request separately and can easily manage any surge in the messages.
Reliability - Numerous producers and consumers can send and receive messages simultaneously.
Server-side encryption(SSE) - Amazon SQS use keys controlled by AWS Key Management Service (AWS KMS) to protect messages in queues.
Customization - The queues don't have to be exactly the same. For example, we can set the default delay for queues. Use Amazon Simple Storage Service (Amazon S3) or Amazon DynamoDB (Amazon SQS contains pointers to Amazon S3 objects) to store message content larger than 256 KB or split large messages into smaller messages. We can save it.
Differences between Amazon SQS, Amazon MQ, and Amazon SNS
Amazon SQS and Amazon SNS are highly scalable, easy-to-use queue and topic services that don't require message broker setup. These services are recommended for new applications that can benefit from near-infinite scalability and simple APIs.
Amazon MQ is a managed message broker service that offers compatibility with many popular message brokers. Amazon MQ is recommended for migrating applications from existing message brokers that require compatibility with APIs such as JMS or protocols such as AMQP, MQTT, OpenWire, and STOMP.
Frequently Asked Questions
Which companies use Amazon SQS?
Many companies use Amazon SQS, including Amazon itself, Pinterest, Accenture, Coursera, quora, cred, OYO, etc.
What are some of the popular tools that can integrate with Amazon SQS?
MongoDB Stitch, SignalFx, LocalStack, and Cloudcraft are some of the popular tools that integrate with Amazon SQS.
Is there any limit to the number of messages that we can send in a queue?
No, there is no limit to the number of messages that can be sent in a queue.
Conclusion
In this article, we talked about Amazon SQS. We saw its working and learned about the need and benefits of using Amazon SQS.