AWS SQS for better architecture

29
AWS SQS for better architecture 23 rd Dec'14 Saurabh S Bangad

Transcript of AWS SQS for better architecture

Page 1: AWS SQS for better architecture

AWS SQS for better architecture

23rd Dec'14 Saurabh S Bangad

Page 2: AWS SQS for better architecture

Contents

-How did SQS solve a problem?-Usage guidance-Architectural patterns-Example architectures-Mistakes-Example cases-References

Page 3: AWS SQS for better architecture

How did SQS solve a problem?

“My app has several jobs running based on a cron setting. That is, some run every 15 minutes, some once an hour, some once a day.These are jobs that affect our RDS data and our S3 resources, which are shared amongst all our ec2 instances such that each job only needs to be run by one machine. Running the same job multiple times is at best wasteful, and at worst could send out multiple emails to the same person.So I want to be able to auto-scale to multiple ec2 instances, yet still have these jobs only run by one machine. But because each auto-scaled instance must use the same ami and run the same application, it is not easy to configure which particular machine runs which jobs.”

Page 4: AWS SQS for better architecture

How did SQS solve a problem?

"My app has several jobs running based on a cron setting." => ????

"That is, some run every 15 minutes, some once an hour, some once a day." => Randomness

"These are jobs that affect our RDS data and our S3 resources, : => Transactional

Page 5: AWS SQS for better architecture

How did SQS solve a problem?

"These are jobs that affect our RDS data and our S3 resources, which are shared amongst all our ec2 instances such that each job only needs to be run by one machine." => ????

Page 6: AWS SQS for better architecture

How did SQS solve a problem?

"Running the same job multiple times is at best wasteful, and at worst could send out multiple emails to the same person." => Of course! Just make use of an EC2 instance and you are all set.

Page 7: AWS SQS for better architecture

How did SQS solve a problem?

"So I want to be able to auto-scale to multiple ec2 instances" => AutoScaling! I like that, but instances don't talk to each other.

"yet still have these jobs only run by one machine" => Sorry ?

Page 8: AWS SQS for better architecture

How did SQS solve a problem?

"But because each auto-scaled instance must use the same ami and run the same application, it is not easy to configure which particular machine runs which jobs." => I know..

Page 9: AWS SQS for better architecture

How did SQS solve a problem?

-This is an SQS session, so obviously the answer is SQS, but how would you solve this problem otherwise ?

-Problem : This is not a client-server model

-How would the instances synchronize ?

-How about a protocol for decision making in a distributed fashion ? Too much of research!

Page 10: AWS SQS for better architecture

Usage Guidance

Producer-consumer model

Page 11: AWS SQS for better architecture

Usage Guidance

"My app has several jobs running based on a cron setting." => The Producer"These are jobs that affect our RDS data and our S3 resources, which are shared amongst all our ec2 instances such that each job only needs to be run by one machine." => The Consumer*Do think about it after the session.*

Page 12: AWS SQS for better architecture

Usage GuidanceProcessing of a message in SQSP: Producer m:MessageC: Consumer Q:QueueStory/Flow :1)P1 sends m1 to Q1 & P2 sends m2,m3 to the Q12)Q1={m1,m2,m3} //unordered set3)C1 requests a message4)Q1 returns one of the messages5)m2 is received by the C16)C1 processes m27)C1 deletes m2 from the queue8)Q1={m1,m3}9)C2 requests a message10)...story continues...

Page 13: AWS SQS for better architecture

Usage Guidance

What will happen if the two consumers request at the same time ?=> The Visibility timeout

Page 14: AWS SQS for better architecture

Usage Guidance

What will happen when the consumer that polled the message dies/forgets ?=> Expired visibility timeout

Page 15: AWS SQS for better architecture

Usage Guidance

What if there are no messages in the queue for a longer duration? It would waste resources looking up continuously and getting empty responses.This does affect the billing as well.=>sleep on the consumer side =>long polling while receivingThe wait time

Page 16: AWS SQS for better architecture

Architectural patterns

Characteristics of SQS :

-Buffer reliably-Scalable-Managed/Queue-aaS-Secured-Highly available-SIMPLE-QS

HA in real sense : (Queue==Que==Q)LOL

Page 17: AWS SQS for better architecture

Architectural patterns

Where can it be used ?

-Asynchronous functioning-Loosely coupled components-Stateless system-Failure resilient system

Page 18: AWS SQS for better architecture

Example architecture

Chained processing

e.g. Intermediate code generation between multiple teams and/or components of the system

Page 19: AWS SQS for better architecture

Example architecture

Priority queuing with SQS

e.g. meeting SLAs based on the type of a customerAnother approach : With a single queue?

Page 20: AWS SQS for better architecture

Example architecture

File sharing

Possible approaches :

1)Producer AutoScaling group can upload the files to S3 and share the URLs over the queue.

2)S3 events can be pushed directly to SQS

3)If the file size is upto 256KB, you can just share it as a message.

Page 21: AWS SQS for better architecture

Architectural patterns/example

Publisher-Subscriber model

e.g. independent teams using part of the messageTIMESTAMP Order Shipping-Type03/03/14:10:10GMT HeadphonesX Two Days Paid03/03/14:10:10GMT ProductY Three Days FreeA team uses timestamp+shipping, the other ignores that

Page 22: AWS SQS for better architecture

Example architecture

Automation by signaling

e.g. Polling SQS for Job Status / Event drivenwhen Elastic Transcoder publishes status messages to SNS, they ultimately get buffered to the Queue. When a consumer poll the message, it can resume the pending operations

Page 23: AWS SQS for better architecture

Example architecture

Email confirmation

e.g. Sign up of new user where user would be sent an email to confirm which doesn't happen interactively

Page 24: AWS SQS for better architecture

Example architecture

SES bounces and complains

Page 25: AWS SQS for better architecture

Example architecture

Users talking directly to the SQS which is backed by DynamoDB

Mobile app

Page 26: AWS SQS for better architecture

Mistakes

-Can SQS send message to .. ? NO!-Is SQS FIFO? NO!-Not deleting the messages-visibility timeout=0-visibility timeout=too high-Using standard/short polling-Not polling enough-Retention period=too low-Synchronous/interactive jobs-Producers and consumers in the same machine

Page 27: AWS SQS for better architecture

Example Cases

-How do I deal with the at least once delivery?

-How do I deploy the new code to prod with the changes in the message format?

-Our website will start splittting the traffic between two ELBs, one for actual content and other for logging, we need to get the ELB prewarmed to support logging.

-Where did that message go?

Page 28: AWS SQS for better architecture

References

1)www.cs.rpi.edu/~goldsd/docs/fall2014-csci4210/producer-consumer-problem.jpg2)docs.aws.amazon.com/sns/latest/dg/SNS_Scenarios.html3)en.clouddesignpattern.org/index.php/CDP:Queuing_Chain_Pattern4)en.clouddesignpattern.org/index.php/CDP:Priority_Queue_Pattern5)blogs.aws.amazon.com/application-management/blog/tag/SQS6)Re:invent'14 ARC3137)blogs.aws.amazon.com/application-management/post/Tx2PM64E771CQGG/Using-DynamoDB-and-SNS-with-Elastic-Beanstalk-in-any-Supported-AWS-Region8)sesblog.amazon.com/post/TxJE1JNZ6T9JXK/Handling-Bounces-and-Complaints9)docs.aws.amazon.com/AWSSimpleQueueService/latest10)Font name : Courier New

Page 29: AWS SQS for better architecture

Thanks a lot!

Any Questions?