16. Amazon Simple Queue Service
- Highly reliable, distributed, scalable message queue
- Asynchronous communication between components
- Not all components have to be available all the time
- Buffer messages when producing / consuming at
different speeds
- Many senders, many recipients sharing the same queue
- 1:1 communication: each message has one recipient
17. Amazon SQS
- Use different queues for different purposes, applications etc.
- Message retention
- Delays
- Visibility timeout
- Send SQS messages from one AWS account to another
- Send messages to SQS from your mobile app or data center
18. Amazon SQS – How does it work?
- Someone (usually the sender) creates the queue
- Senders put messages into the queue
- User-defined format (up to 64 KB)
- Receivers
- take messages off the queue…
- … process the data, …
- … and delete the message from the queue
19. Put a message in the queue
$q = new SQSClient(AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY, SQS_ENDPOINT);
try{
$result = $q->CreateQueue(‘process-req-q’);
//echo 'Queue Created: ', $result,’n<br/>’;
} catch(Exception $e){
throw($e);
}
$messageId = $q->SendMessage(“s3://$bucket/$key”);
20. Take messages off the queue
$q = new SQSClient(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY,
SQS_ENDPOINT,‘process-req-q’);
// poll until there’s a message available
do { $nextMessage = $q->ReceiveMessage(1); }
while ( count($nextMessage) == 0);
foreach ($nextMessage as $message) {
$S3URL = $message->Body;
$handle = $message->ReceiptHandle; // keep this!
// TODO: Get file from $S3URL and process it
$q->DeleteMessage($handle);
}
21. Amazon Simple Queue Service
- Simpler than most message queues
- No admin required
- No setup required
- Amazon SQS is affordable:
$0.01 per 10,000 requests
23. Failures
Receiver can fail after taking message off the queue
- Network partition
- Instance failure
- OS failure
- Code bug
What happens to the message when the receiver
dies while processing it?
35. “At-least-once delivery”
Takeaways:
- Set VisibilityTimeout long enough to allow for processing
- Delete messages when you’re done with them
- Processing must be idempotent (in case the same message
arrives more than once)
39. Autoscaling workers, step #1
Create a launch configuration:
What kind of instances should we launch?
PROMPT> as-create-launch-config MediaProcLC
--image-id ami-xxxxxxxx --instance-type
m1.small
40. Autoscaling workers, step #2
Create an AutoScaling group:
Give the group of instances a name, define the size…
PROMPT> as-create-auto-scaling-group
MediaProcAS --launch-configuration MediaProcLC
--availability-zones us-east-1a us-east-1b --
min-size 1 --max-size 10
41. Autoscaling workers, step #3
Define the scaling policy:
How do we scale this group?
PROMPT>as-put-scaling-policy MediaProcScaleUp -
-auto-scaling-group MediaProcAS --adjustment=1
--type ChangeInCapacity --cooldown 300
Returns a policy ARN:
POLICY-ARN arn:aws:autoscaling:us-east-
1:0123456789:scalingPolicy/abc-1234- def-567
43. SQS Data in CloudWatch
Several metrics per queue:
- ApproximateNumberOfMessagesVisible
- NumberOfEmptyReceives
- …
Use these to determine the number of processing nodes!
44. Autoscaling workers, step #4
Define a Cloudwatch Alarm:
When should the scaling happen?
PROMPT>mon-put-metric-alarm QSizeAlarm
--comparison-operator GreaterThanThreshold
--statistic Average –threshold 100
--evaluation-periods 1 --period 600
--namespace "AWS/SQS”
--metric-name
ApproximateNumberOfMessagesVisible
--dimensions "QueueName=process-req-q”
--alarm-actions POLICY-ARN_from_previous_step
58. Introducing Amazon SWF
• Enables scalable, resilient, distributed applications
• Applications are built as a coordination of tasks
• Run application workflows and business processes
in the cloud
• Accessible anywhere from any programming
language
• Programming model and framework
62
59. Introducing Amazon SWF
Enables scalable, resilient, distributed applications
• All application components are stateless
• Components can be scaled independently
• SWF provides durable, distributed state
management
63
60. Introducing Amazon SWF
Enables scalable, resilient, distributed applications
• Components are stateless, failed components can
be replaced easily
• SWF keeps track of executed tasks and state,
timeouts, failures, …
• Workflow can respond to these failures
64
61. Introducing Amazon SWF
Enables scalable, resilient, distributed applications
• Application components anywhere (EC2, on-
premise, mobile)
• Components poll for work using HTTPS, easy to go
through firewalls
• No need to keep distributed state
• Component & network failures are easy to handle
65
62. How Amazon SWF works
Start > Encode video > Add DRM protection > Finish
Get task
Amazon Worker for
SWF Long Poll encoding
Web Server Front End
Accepts video URL, Start Workflow Execution
Starts execution with URL Encode Tasks Return results
DRM Tasks
Return decisions
Decision Tasks
Get task
Long Poll
Long poll Worker for adding
Execution History DRM
Decisions: Get task
- Input data (Video URL)
1. Schedule encoding of the video
- Encode task complete
2. Schedule DRM protection DECIDER Return results
- DRM task complete
3. Complete execution Makes decisions on what tasks to
schedule, when, in what order - Execution complete
63. Using Amazon SWF
Determine
Write workers Start workers
application flow
for activities and deciders
and activities
Write deciders Start
Setup domains to drive executions
application flow
Determine View
Write workflow executions in
workflow and
initiator code the console
activity types
67
64. Workflow components
Activity Workers
Perform activity tasks they receive from SWF
Different workers for different kinds of tasks
Deciders
Decide what to do next
Pick the next step in the workflow based on workflow
history
65. Writing Workflow components
Writing Activity Workers
• Poll for work on a specific task list
• Do the work, send heartbeats as required
• Use input data
• Report success or failure to Amazon SWF
• You can provide detailed data to the decider
• Repeat
66. Writing Workflow components
Writing Deciders
• Poll for work on a decision task list
• Evaluate workflow execution history
• SWF sends full history in JSON format
• Return decision to Amazon SWF
• Usually this means scheduling another task
• Repeat
All the coordination logic of your application is
encapsulated in this one component!
67. AWS Flow Framework
Entire workflow looks like simple, sequential code
Abstracts polling, scheduling tasks, remote execution
Easy to program complex dependencies
Simple method call to asynchronously execute an application step
Express dependencies by passing output of one task as input of another.
Handle remote failures as standard exceptions
Uses Amazon SWF as the backend
Open-sourced on Github.
Currently in Java. Soon in more languages.
71
69. Timeouts
• SWF keeps track of execution times on every step
• Time to start, time to finish, … for every activity task
• Time to finish for overall workflow
• Timeouts controlled for each of these (and more)
• Heartbeats for long-running activities (optional)
• Decider is informed of timeouts and controls the reaction
• Schedule retries or
• Schedule new “mitigation” or cleanup tasks
70. Failures
- Workers reply with failure messages when they failed to do
their work
- Replies can contain application-specific data
- Decider can react to these messages accordingly
- Decider can reschedule, skip incorrect results or even cancel
the whole workflow execution
76. Amazon SWF compared to SQS
• Centralized control logic • No centralized logic
• All activities are coordinated • No coordination between
• Task-oriented different queues
• Tasks delivered only once • Message-oriented
• Tasks run in order • At-least-once delivery
• Auditable execution history • “best-effort” order
• Visibility APIs • No history maintained
• Flow framework for Java
• Long-running processes
77. Amazon SWF Pricing
Several dimensions to pricing:
- Workflow executions
$1 for 10,000 workflows started
- Actions run
$0.25 for 10,000 actions
- History retention
$0.05 per day for every 10,000 workflows
82. Amazon SNS Features
- Publish – Subscribe messaging, i.e. “1 to many”
- Define topics
- Multiple subscribers per topic
- Different endpoints for subscribers:
• E-Mail
• SMS
• HTTP(S) POST (delivers JSON messages)
• Amazon SQS
83. Amazon SNS Pricing
2 dimensions:
- Pay per request to the API
first 100,000 requests / month are free
- Pay for each notification sent, e.g.
- 100,000 HTTP notifications are $0.06
- 100,000 email notifications are $2.00
84. Amazon SNS Use Case: Monitoring
- Use CLI tools to publish events from your instances via startup
scripts etc.
- Subscribe to events published by AWS infrastructure:
- Autoscaling added / removed an instance
- CloudWatch alarms
- CloudFormation stack setup / teardown
- Subscribe via eMail for development & testing
- Connect to your monitoring system via HTTP or SQS endpoint
85. Amazon CloudWatch
Monitoring for AWS resources
+
Amazon SNS
Simple Notification Service
87. Amazon SNS Use Case: Decoupling apps
- Use SDKs and API to publish relevant events for your app
- Use topics as part of your API to provide loose coupling
- Other applications can subscribe to topics they care about
- Subscriptions can be changed at any time
- Subscribe an SQS endpoint for message persistence
- Subscribe via eMail for development & testing
90. #1 Use SQS for loose coupling
#2 Set VisibilityTimeout correctly
#3 Autoscale your queue workers
#4 Use spot instances for (some of) your workers
#5 Use Amazon SWF to coordinate tasks
#6 Use SNS notifications to stay up to date