(WRK302) Event-Driven Programming

34
© 2015, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Daniela Miao, Software Engineer, AWS, DynamoDB October 2015 WRK302 Event-Driven Programming With Amazon DynamoDB Streams and AWS Lambda

Transcript of (WRK302) Event-Driven Programming

Page 1: (WRK302) Event-Driven Programming

© 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

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

Why Amazon DynamoDB?

Page 4: (WRK302) Event-Driven Programming

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

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

Amazon DynamoDB Basics

Page 7: (WRK302) Event-Driven Programming

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

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

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

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

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

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

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

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

Amazon DynamoDB Streams

Page 16: (WRK302) Event-Driven Programming

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

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

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

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

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

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

Cross-region replication library

Horizontal scaling

Bootstrapping existing items

Fault tolerance

Optimized for cost and performance

Page 23: (WRK302) Event-Driven Programming

AWS Lambda

Page 24: (WRK302) Event-Driven Programming

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

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

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

Amazon DynamoDB Streams and Lambda

Page 28: (WRK302) Event-Driven Programming

Sample Scenario

Amazon DynamoDB table

and Streams AWS Lambda

Send notification via

Amazon SNS

Update another table

Page 29: (WRK302) Event-Driven Programming

Building

an event-driven application

Page 30: (WRK302) Event-Driven Programming

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

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

Remember to complete

your evaluations!

Page 33: (WRK302) Event-Driven Programming

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

Thank you!

After the break, there will be a group-based activity

to build an event-driven application