← Cloud Practitioner study path

Technology and services · 12 min

Amazon SQS, Amazon SNS and Amazon EventBridge

In this lesson you will learn what it means to decouple an application, how an Amazon SQS queue, an Amazon SNS topic and an Amazon EventBridge bus work, and how to choose among the three.

A large application is usually made of several parts, which are separate programs that work together. Amazon SQS, Amazon SNS and Amazon EventBridge are the AWS services that connect those parts without one having to wait for the other. With them, one part leaves the work written down in a message and the other picks it up when it can. For example, an online store's website writes down each new order, and the program that charges for and prepares the orders picks them up one by one.

What does it mean to decouple an application?

Decoupling an application means that one of its parts can keep working even if another one is not available at that moment. For example, in a coupled store, the website sends each order directly to the processor, which is the part that charges for and prepares the orders, so if the processor goes down or gets overloaded, that is, receives more work than it can handle, the website fails with it. In a decoupled store there is an Amazon SQS queue in between: the website leaves the order in the queue and keeps working, and if the processor goes down, the orders wait in the queue until it comes back.

Coupled application and decoupled application with an Amazon SQS queue

Amazon SQS

Amazon SQS (Simple Queue Service) is a message queue, that is, a line where messages wait their turn. A message is a package of information that one part of the application leaves for another, such as the details of an order. The parts that leave messages in the queue are called producers, and the part that picks them up is called the consumer. The consumer requests the messages when it is ready, processes them and deletes them, so each message is processed by a single consumer. For example, in the store, the website is the producer that leaves each order in the queue, and the processor is the consumer that picks it up, charges for it and deletes it from the queue.

Messages don't stay in the queue forever. If you don't change anything, the queue keeps each message for 4 days, and that time can be configured from 60 seconds up to 14 days. For example, if the store's processor stays off for longer than the queue keeps the messages, the oldest orders are deleted without anyone processing them.

Queue types

The two Amazon SQS queue types: the standard queue, with nearly unlimited throughput and at-least-once delivery, and the FIFO queue, with ordering and exactly-once processing

SQS has two queue types:

  • The standard queue, which has nearly unlimited throughput, that is, it can move as many messages per second as needed, delivers each message at least once and tries to preserve the order in which they arrived, so now and then a message shows up repeated or out of order and your application has to tolerate it. For example, if the store uses it to send confirmation emails, its program checks before sending each email that the order doesn't already have one.
  • The FIFO (first in, first out) queue, which processes each message exactly once and preserves the order, but has a limit of 300 per second, or 3,000 messages per second if you send them in batches, that is, in groups, of 10, and goes higher with high throughput mode. For example, a bank uses it for the transactions on an account, where a deposit and a withdrawal have to be applied only once and in the order they happened.

True or false?

0 of 4
  1. With a standard queue, a repeated message arrives now and then.

  2. A message stays in the SQS queue until someone reads it, with no time limit.

  3. In SQS, each message is processed by a single consumer.

  4. The FIFO queue has nearly unlimited throughput.

Amazon SNS

An SQS queue delivers each message to a single consumer, so it doesn't work when you need to notify many at the same time. For that there is Amazon SNS (Simple Notification Service), where a publisher sends a message to a topic and SNS pushes a copy to each subscriber of that topic. A topic is a named channel that those who want to receive notifications sign up to, and pushing means that SNS sends them the copy without them asking for it. Subscribers can be SQS queues, , , email addresses, mobile notifications and SMS messages. For example, the store creates a deals topic, and when it publishes a deal to it, SNS sends an email to each customer subscribed by email and an SMS to each customer subscribed by SMS.

Because an SQS queue can be a subscriber, SQS and SNS are also combined. If you subscribe several queues to an SNS topic, each queue receives its own copy of the message and each system processes it at its own pace from its queue. AWS calls that pattern fanout, because a single message fans out into several copies. For example, each new order in the store goes to a topic, and from the topic one copy goes to the billing queue, another to the inventory queue and another to the shipping queue.

How do you set it up?

Each new order has to reach billing, inventory and shipping, and each system processes it at its own pace. How do you set it up?

Amazon EventBridge

Amazon EventBridge is a service that connects the parts of an application by means of events. An event is the record of something that happened, for example that a file was uploaded, that a user was created or that an was stopped.

Events arrive at an event bus, which is the point where they come together and which receives them from AWS services, from your applications and from third-party software, that is, from programs made by other companies that you use. On the bus you define rules, and each rule decides which target each event goes to. For example, the store creates a rule so that, when the payment system reports that a payment was declined, that event goes to the function that writes to the customer. When you only want to connect one source to one target, EventBridge has another feature, called pipes, which connects the two point to point, that is, directly from one to the other.

How do you tell SQS, SNS and EventBridge apart?

Amazon SQS queue, Amazon SNS topic and Amazon EventBridge bus

All three services move messages, but each one moves them in a different way. In SQS the consumer requests the messages and each one is processed by a single consumer, in SNS the topic pushes a copy to each subscriber, and in EventBridge events arrive from several sources and each rule sends them to its targets.

That is why, to choose among the three, think about what has to happen with the message. SQS is for someone to process the work later, SNS is for notifying many now, and EventBridge is for reacting, based on rules, to what happens in your services and applications. For example, the store uses SQS so that orders wait for the processor, SNS to tell all its customers about a deal, and EventBridge so that, when an instance stops, a rule notifies the technical team.

SQS, SNS or EventBridge?

0 of 6

Tap the correct column for each case.

  1. Have the orders wait if the processor goes down

  2. Send an SMS to all the subscribers of an alert

  3. When an instance is stopped, a rule sends the event to a function

  4. Process the orders in the order they arrive and without duplicates

  5. React to events sent by a third-party application

  6. Notify several systems at once right now

On the exam

In questions on this topic, look in the question for what has to happen with the message. For example, if the question says that a school wants to send an SMS to all subscribed parents when classes are canceled, the answer is SNS, because many people have to be notified at the same time. These phrases point to each service:

  • "Decouple", "so that one part doesn't depend on the other" or "so that the orders wait", which point to SQS, and to a FIFO queue if the question also says "order matters" or "without duplicates".
  • "Notify several systems at once", "publish and subscribe" or "send SMS or email to subscribers", which point to SNS.
  • "React to events from AWS services or " or "route events with rules", which point to EventBridge.

cloud path tip: if you use an account with the new AWS experience, SQS, SNS and EventBridge are included in the free plan, so you can try all three.

Review question

When a customer pays, the payment system has to notify inventory, billing and shipping. Each one must receive its own copy of the notification and process it at its own pace, even if one of them is down for a while. Which combination do you use?

Next