Decoupling applications: SQS, SNS, Kinesis, Active MQ Flashcards
Two patterns of application communication
Synchronous (app to app)
Asynchronous / Event based (app to queue to app)
Synchronous problems
Problematic if sudden spikes of traffic appear
How can you decouple your apps? (to avoid sudden spike of traffic issues)
SQS - Queuing model
SNS - pub/sub model
Kinesis - real time streaming model
What is the benefit of decoupling?
Services (sqs, sns, kinesis) decouple independently from the app
SQS architecture
Producer sends messages to SQS queue, then the Consumer polls the messages to process them. Once processes they get deleted
What is the purpose of an SQS queue?
Acts as a buffer to decouple between producers (sender) and consumers (processor)
Standard Queue
Fully managed
Unlimited throughput and messages in queue
Retention = 4-14 days
Low latency
256KB per message
Can have duplicates
Can have out of order mesasges
What do producers do
Send to SQS using SDK (SendMessage API)
message persists 4-14 days until consumer deletes it
How do consumers work
Consumers run on EC2 instances, servers or AWS Lambda
Polls / Receive SQS messages (up to 10 at a time)
Process message (e.g. insert into RDS DB)
Delete message using DeleteMessage API
Multiple EC2 instances consumers
Can have many consumers in SQS Queue
receive and process messages in parallel
At least once delivery (because they can work on one message at the same time)
Best-effort message ordering
Message deleted after processed
Can scale consumers horizontally to improve throughput of processing
SQS with ASG
SQS polls messages to EC2 instances (consumers) which are in an ASG.
CloudWatch Metric - Queue Length (ApproximateNumberOfMessages) will set an alarm and send it to CloudWatch.
If alarm is triggered, CloudWatch notifies ASG
How to decouple using SQS?
Instead of doing requests & processing on front end web app you can:
Receive the requests in the front-end web app (in an ASG) and send them to SQS.
SQS will send them to the back-end processing application (video processing) which is in an ASG. The processing is done here.
Then get sent to the S3 bucket once done.
SQS Security
Encryption in flight using HTTPS API, at rest encryption using KMS keys.
Client-side encryption if the client wants to handle encryption/decryption
Access Controls - IAM policies to regulate access to SQS API
SQS Access Policies (similar to S3 bucket policies)
-useful for cross-account access to SQS queues
- allowing other services (SNS, S3) to write to an SQS queue
Message visibility timeout
When message gets polled by consumer it becomes invisible to other consumers (30 seconds)
After that it will be visible in the queue and another consumer can work on it
ChangeMessageVisibility API
When a consumer knows that they need more than 30 seconds (default) to process a message, they can request this API and extend the invisibility window.
This will help with not processing the same message twice
Long Polling
When a consumer requests messages from queue but they want to “wait” for messages to arrive if there are non in the queue.
Can reduce latency
Can reduce API calls
Long Polling how it works
Consumer polls for up to 20 sec, waiting and when a message is received in the SQS queue, then it polls it and processes it.
Downsides of FIFO
Limited throughput 300msg/s without batching and 3000 with batching.
Without FIFO there is unlimited throughput
Benefits of FIFO
exactly once send capability (removing duplicates)
Messages processed in order by consumer
How to use SQS as a Buffer to database writes
Requests go in your EC2 within an ASG. They get enqueued, meaning they get sent to SQS Queue (infinitely scalable) and then they get sent from the SQS Queue to another ASG which holds Dequeueing EC2 instances. These requests get sent as messages.
From that Dequeueing SQS the messages get insert into the Databases.
This makes sure nothing is lost. Once they are in the DB, then they can be deleted from SQS.
Amazon SNS
Send one message to multiple receivers
How to use SNS with Pub / Sub?
Buying service -> SNS topic which has multiple subscribers (services) such as email notifications, fraud service, shipping service, SQS queue and they all receive the messages
SNS how it works
even producer only sends to ONE SNS topic
can have as many event receivers as we want
Up to 12,500,000 subs per topic, 100,000 topics limit