(WRK302) Event-Driven Programming

Post on 14-Feb-2017

3.729 views 0 download

Transcript of (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

What to Expect from the Session

Why Amazon DynamoDB?

Amazon DynamoDB Basics

Amazon DynamoDB Streams

AWS Lambda

Building an event-driven application

Summary

Why Amazon DynamoDB?

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

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

Amazon DynamoDB Basics

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

Data model

RecordID PlayerName Score NickName

1 Alice 999 AliceTheMalice

2 Bob 888

Sample table: GameRecords

• Note attributes can be optional

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

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

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

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

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

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

Amazon DynamoDB Streams

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

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.”

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

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

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

DynamoDB Streams

Open source cross-region

replication library

Asia Pacific (Sydney) EU (Ireland) Replica

US East (N. Virginia)

Cross-region replication

Cross-region replication library

Horizontal scaling

Bootstrapping existing items

Fault tolerance

Optimized for cost and performance

AWS Lambda

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

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

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

Amazon DynamoDB Streams and Lambda

Sample Scenario

Amazon DynamoDB table

and Streams AWS Lambda

Send notification via

Amazon SNS

Update another table

Building

an event-driven application

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

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

Remember to complete

your evaluations!

Use QR code or link above to download the

group activity package and follow the lab guide.

http://reinventworkshop.s3.amazonaws.com/workshop.zip

Thank you!

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

to build an event-driven application