(WRK302) Event-Driven Programming
-
Upload
amazon-web-services -
Category
Technology
-
view
3.729 -
download
0
Transcript of (WRK302) Event-Driven Programming
![Page 1: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/1.jpg)
© 2015, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Daniela Miao, Software Engineer, AWS, DynamoDB
October 2015
WRK302
Event-Driven ProgrammingWith Amazon DynamoDB Streams and AWS Lambda
![Page 2: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/2.jpg)
What to Expect from the Session
Why Amazon DynamoDB?
Amazon DynamoDB Basics
Amazon DynamoDB Streams
AWS Lambda
Building an event-driven application
Summary
![Page 3: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/3.jpg)
Why Amazon DynamoDB?
![Page 4: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/4.jpg)
Customers wanted a database with…
Reliably low latency at any scale
Elastic throughput and storage
Flexible queries and data models
Strong consistency
Availability and fault-tolerance
![Page 5: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/5.jpg)
What Amazon DynamoDB Provides…
Fully-managed NoSQL database service
Consistent, single-digit millisecond latency
Independent throughput and storage scaling
Supports both document and key-value data models
Automatic data replication across three facilities
![Page 6: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/6.jpg)
Amazon DynamoDB Basics
![Page 7: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/7.jpg)
Data model
Tables: collection of items
Items: collection of attributes
Attributes: name-value pairs
• Scalar: number, string, binary, boolean, null
• Multi-valued: string set, number set, binary set
• Document: list, map
![Page 8: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/8.jpg)
Data model
RecordID PlayerName Score NickName
1 Alice 999 AliceTheMalice
2 Bob 888
Sample table: GameRecords
• Note attributes can be optional
![Page 9: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/9.jpg)
Primary key – hash
RecordID PlayerName Score NickName
1 Alice 999 AliceTheMalice
2 Bob 888
Hash type primary key: primary key is made of one attribute,
required for all items
![Page 10: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/10.jpg)
55 A9 54
Distributing hash keys
Hash key uniquely identifies an item
Hash key is used for building an unordered hash index
00 FF
RecordId = 1
Name = Alice
Hash (1) = 7B
RecordId = 2
Name = Bob
Hash (2) = 48
RecordId = 3
Name = John
Hash (3) = CD
Key Space
![Page 11: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/11.jpg)
55 A954 AA FF
Distributing hash keys
Table can be partitioned for scale
00 FF
RecordId = 1
Name = Alice
Hash (1) = 7B
RecordId = 2
Name = Bob
Hash (2) = 48
RecordId = 3
Name = John
Hash (3) = CD
Key Space
![Page 12: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/12.jpg)
Primary key – hash and range
Customer# Order# Item
1 10 Shoes
1 11 Toys
2 11 Pen
Hash and range type primary key: the primary key is made of
two attributes, required for all items
![Page 13: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/13.jpg)
Hash range table
Within an unordered hash index, data is sorted by the range key
00 FF
Hash (2) = 48
Customer# = 2
Order# = 10
Item = Pen
Customer# = 2
Order# = 11
Item = Shoes
Customer# = 1
Order# = 10
Item = Toy
Customer# = 1
Order# = 11
Item = Boots
Hash (1) = 7B
Customer# = 3
Order# = 10
Item = Book
Customer# = 3
Order# = 11
Item = Paper
Hash (3) = CD
55 A954 AA
Partition 1 Partition 2 Partition 3
![Page 14: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/14.jpg)
Query and scan
A query operation finds items in a table using only primary key
attribute values.
• Hash key attribute-value pair is required
• Range key attribute-value pair is optional
• In the previous example, query for all orders of a specific customer by
setting “customer# = 1".
A scan operation examines every item in the table.
• Returns all of the data attributes for every item
![Page 15: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/15.jpg)
Amazon DynamoDB Streams
![Page 16: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/16.jpg)
Amazon DynamoDB StreamsAmazon DynamoDB Streams
Stream of updates to a table in a continous pipeline
Records all modifications made to items of the table
Scales with table size and throughput
StreamsTable
DynamoDB
Client ApplicationUpdates
![Page 17: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/17.jpg)
Customer application
Amazon DynamoDB replicator using DynamoDB Streams
Copies incoming data to a different table in real-time
Blog post published describing the application
“DynamoDB delivers even more stability, redundancy, and speed for
our document-based data and requires less hands-on administration,
so we can focus on building the software that powers Mapbox.”
![Page 18: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/18.jpg)
View Type Record Content
Old image—before update RecordID = 1, Name = Alice
New image—after update RecordID = 1, Name = Alison
Old and new images RecordID = 1, Name = Alice
RecordID = 1, Name = Alison
Keys only RecordID = 1
View types
UpdateItem: (RecordID = 1, Name = Alice) (RecordID = 1, Name = Alison)
Entire image of the item is returned, not just the changes
![Page 19: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/19.jpg)
Stream
Table
Partition 1
Partition 2
Partition 3
Partition 4
Partition 5
Table
Shard 1
Shard 2
Shard 3
Shard 4
KCL
Worker
KCL
Worker
KCL
Worker
KCL
Worker
Amazon Kinesis Client
Library Application
DynamoDB
Client Application
Updates
Amazon DynamoDB Streams and
Amazon Kinesis Client Library
![Page 20: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/20.jpg)
Analytics with
Amazon DynamoDB Streams
Collect and de-dupe data in DynamoDB
Aggregate data in-memory and flush periodically
Performing real-time aggregation
and analytics
![Page 21: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/21.jpg)
DynamoDB Streams
Open source cross-region
replication library
Asia Pacific (Sydney) EU (Ireland) Replica
US East (N. Virginia)
Cross-region replication
![Page 22: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/22.jpg)
Cross-region replication library
Horizontal scaling
Bootstrapping existing items
Fault tolerance
Optimized for cost and performance
![Page 23: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/23.jpg)
AWS Lambda
![Page 24: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/24.jpg)
AWS Lambda
Event-driven compute in the cloud
Lambda functions: stateless, request-driven code
execution
• Triggered by events in other services: e.g., write to an
Amazon DynamoDB table
Ability to transform data as it reaches the cloud
![Page 25: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/25.jpg)
AWS Lambda
Customers can focus on business logic, no need to
manage infrastructure
Lambda scales to match the event rate
Pay for only what you use
• Fine-grained pricing: purchase compute time in 100ms
increments
• No hourly, daily, or monthly minimums
![Page 26: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/26.jpg)
Popular usage scenarios
Data triggers with Amazon S3 or Amazon DynamoDB
Audit and notify
• Monitor AWS CloudTrail logs and use Lambda functions to send
Amazon SNS notifications
Custom deployment rules using AWS CloudFormation
Validate cross-device sync data with Amazon Cognito
Custom events by publishing to Amazon Kinesis
![Page 27: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/27.jpg)
Amazon DynamoDB Streams and Lambda
![Page 28: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/28.jpg)
Sample Scenario
Amazon DynamoDB table
and Streams AWS Lambda
Send notification via
Amazon SNS
Update another table
![Page 29: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/29.jpg)
Building
an event-driven application
![Page 30: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/30.jpg)
Popular usage scenarios
Mobile gaming application
GameScoreRecords table
• Records of game scores
• Want to implement a list of aggregate scores for each user
GameScoresByUser table updated by AWS Lambda
![Page 31: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/31.jpg)
Self-paced Hands-on Lab
Zip file included with the workshop package
Use lab guide to complete each exercise in your group
Solutions provided in a separate directory – do NOT use
unless truly stuck
![Page 32: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/32.jpg)
Remember to complete
your evaluations!
![Page 33: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/33.jpg)
Use QR code or link above to download the
group activity package and follow the lab guide.
http://reinventworkshop.s3.amazonaws.com/workshop.zip
![Page 34: (WRK302) Event-Driven Programming](https://reader031.fdocuments.in/reader031/viewer/2022030211/58a2fae31a28ab5d1c8b6731/html5/thumbnails/34.jpg)
Thank you!
After the break, there will be a group-based activity
to build an event-driven application