What is Amazon SNS and SQS?

Christian Talavera
5 min readAug 10, 2021

Overview of SQS

SQS (Simple Querying Service) is a queuing system that provides a fully managed, highly available message queue; this can be used to decouple systems to increase system resiliency. SNS messages can be up to ***256 KB***.

What is a queue?

Queues are used when two or more computer systems need to communicate, but don’t need to do so in real time speeds; asynchronously.

An example would be, if you forgot your password, and ask the website to send out an email linking to a password reset form.

The actual request needs to eventually cause the reset email to be sent, but the server being requested might be busy or unavailable. In addition, some servers are not optimized to send emails, and these particular requests might not be the preferred work being run on the server. Queuing allows for a couple of things; the request is to be kept in memory, and a different, more appropriate server can instead handle the requests. If no machine can handle the request, it is just left in the queue so it can be processed later.

Polling

SNS ‘polls’, or check requests in the queue using two different methods:

Short Polling

  • Uses a single API call to check the queue for messages
  • Can return a max of 10 messages
  • Results are delivered with little wait time
  • Not cost effective, as many API calls have to be made

Long Polling

  • Uses a single API call to check queue similar to ‘short polling’ but with added processing time
  • The additional time to process is called ‘WaitTimeSeconds’ this be specified to allow for queue to build up
  • More efficient and cost-effective; less API calls have to be made because more messages are returned, and more API calls are not empty

How are messages pulled

When a message is pulled, the message is not actually deleted from the queue. It is instead hidden for a particular period of time, which is ‘VisibilityTimeOut’. Once this time expires, the message actually returns back to the queue. Due to this, it is best practice to delete messages from the queue after it has been already processed.

Queue types

SQS queues comes in two different varieties:

Standard

  • the original offering queue type that was first available for SQS
  • Best choice for performance
  • Distributed and scalable
  • ‘Best effort’ is used to deliver messages, so at least one message is guaranteed to be delivered
  • The sequential order of the message is not guaranteed
  • Messages can sometimes be delivered more than once

FIFO

  • ‘First In, First Out’; messages are delivered in the order they are received
  • The sequential order of the message is guaranteed
  • Messages are only delivered once
  • Throughput performance is limited
  • with batching, throughput limit of 3000 messages per second
  • without batching, throughput limit of 300 messages per second

SQS Effects on Architecture

Decouple

  • SQS is great for ‘decoupling’ different parts of a system, so if it fails then the failure does not cascade to any components connected to it

Asynchronous Messaging

  • Applications are able to be independently scaled due to the implementation of asynchronous messaging.

Creating a Queue in SQS

Navigate to AWS Console>Services>Application Integration>Amazon SQS:

Fill in the basic information of the queue

  • Type — Standard or FIFO
  • Name — the name of the queue to be created

Next, the queue needs configuration options to be set:

  • Visibility timeout — The amount of time that a message received from a queue won’t be visible to the other message consumers
  • Message retention period — The amount of time that Amazon SQS retains messages that are placed in the queue. Default value is 4 days; max value is 14 days
  • Delivery delay — The time delay before delivering a message that has been added to the queue.
  • Maximum message size — The maximum size for each message in the queue

Afterwards, an ‘access policy’ must be created:

  • Type — Basic or Advanced
  • Who can send messages to the queue — can be set to ‘owner only’ or ‘approved IAM roles/users and AWS accounts’
  • Who can receive massages to the queue — can be set to ‘owner only’ or ‘approved IAM roles/users and AWS accounts’

Encryption can be optionally set at this stage:

  • Server-side encryption — enabled or disabled

Messages that cannot be consumed can be redirected to this created queue by optionally setting it as ‘dead-letter queue’:

  • Set queue to receive deliverable messages — enabled or disabled

Lastly, associative tags can be added if desired.

Overview of SNS

What is a Notification System?

A notification system is the hardware and software that makes up a system that delivers a message to a set group of recipients based on pre-configured conditions.

What is SNS?

Amazon offers a public notification system service ‘SNS’, which stands for Simple Notification Service. SNS is a highly durable, secure and available notification system, which can integrate with other AWS products and services.

This allows for ‘event-driven’ notifications to be sent, for systems to perform actions afterward. This is able to decouple applications, which increases scalability, performance, and reliability.

SNS is able to coordinate how notification messages are sent and delivered to different ‘topics’; each notification message can be up to 256 KB in size.

SNS uses the ‘pub/sub’ notification model; In the pub/sub notification model, all messages are published to a ‘topic’. Recipients can ‘subscribe’ to the topic; all messages published to a topic are immediately sent to all users who are ‘subscribed’.

To use SNS, first an SNS ‘topic’ must be created, for notifications to be sent to; ‘subscribers’ are then added so notifications will be delivered, which can be a variety of destinations (HTTP, HTTPS, SMS, AWS Lambda, email, etc.)

Some features of SNS include:

  • Delivery status — helps keep track of specific notifications in transit
  • Delivery retries — if a notification fails to deliver, the notification can automatically try to resend
  • Highly-available — SNS works even in case of AZ failure
  • Encryption — server-side encryption of data is available
  • Cross account usage — using ‘resource policies’ allows for notification usage across different AWS accounts
  • SNS FIFO — messages are delivered in the order they are received to guarantee delivery

Uses of SNS

Some examples of practical usage of notifications in AWS:

  • CloudWatch when an alarm changes state
  • CloudFormation when an infrastructure stack changes state
  • Auto Scaling Groups when a scaling event occurs

Creating an SNS Topic

Navigate to AWS Console>Services>Application Integration>Simple Notification Service>Create Topic.

Basic details must be added:

  • Name — name of the topic to be created
  • Display name — if sending to SMS, this will be the name that displays

Encryption can be optionally configured:

  • Encryption — enabled or disabled

Access policies can also be optionally set:

  • Type — Basic or Advanced
  • Who can publish messages to the topic — can be set to ‘owner only’, ‘approved IAM roles/users and AWS accounts’, or ‘everyone’
  • Who can subscribe to the topic — can be set to ‘owner only’, ‘approved IAM roles/users and AWS accounts’, ‘requesters with certain endpoints’ or ‘everyone’

Delivery retry policies and delivery status logging options for the topic can optionally be set, and associative tags can be added if desired.

--

--

Christian Talavera

DevOps Engineer writing about breaking into the industry