SlideShare a Scribd company logo
Serverless Patterns
Cliff Chao-kuan Lu
AWS User GroupTaiwan
Apr. 27th,AWS Connect TPE
Cliff Chao-kuan Lu
•Sr. Architect @ 104 Corp.
•AWS Community Hero
•Organizer, AWS User GroupTaiwan
•Certified AWS ALL-5
•Loves Cloud Computing
Serverless
Serverless Definitions
•Operation
•20 ms / 0.5 s
•PaaS
•Pricing model
•Pay per call
•Architecture
•PaaS for (computational) resources
Lambda ECS
Function-as-a-Service Docker Scheduler
Infrastructure Managed EC2 Instances
Housekeeping Concurrency
Auto Scaling
Healthcheck
Reservation / Spot
Access Control IAM / VPC
Monitoring Cloudwatch / Cloudwatch Logs
vs.
Common Considerations
•Cost
•Availability
•Scalability
Cost Comparison
•t2.nano: $0.008
•Lambda – 128 mb / 100 ms:
•36000 Calls – $0.0072
•450 GB-s – $0.0075
•Managed service = Highly available + Scalable
* 2 (scaling factor)
* 0.25 (Spot)
* 0.7 (RI)
/ 5% (CPU Credit)
Common Considerations
•Capability
•EC2 term management
•ASG Guru
•Design
•Tenancy
•Limits
Lambda Runtime Limits
•Time
• Max timeout = 300 s*
•Environment
• Max RAM = 1,536 MB
• Max Storage = 500 MB**
•Payload: 6 MB
•Concurrency: 100 600 (per account-region)
Lambda Event Source and Retry
•Stream-based Source
•Synchronous invocation
•Asynchronous invocation
Lambda Event Source
•Stream-based Source
•Kinesis, DynamoDB Stream
•Others
•Synchronous invocation
• API Gateway, SDK
•Asynchronous invocation
• Cloudwatch Events
Lambda Permission Model
•Stream-based Source (Pull)
•Kinesis, DynamoDB Stream
•Others (Push)
•Synchronous invocation
• API Gateway, SDK
•Asynchronous invocation
• Cloudwatch Events
Lambda Retry on Error
•Stream-based Source
•Cursor-based reads, spontaneous retry
•Synchronous invocation
•Client retry
•Asynchronous invocation
��Retry twice -> DLQ {SQS, SNS}
Serverless Framework
•FLOSS (MIT)
•Led by Austen Collins / full-time team
•Under active development
•Written in Node.js
•Multiple programming languages
Serverless Patterns
Serverless-Golang
Serverless-OpenWhisk
Serverless-PHP
Common Lambda Patterns
•Direct invocation
•Crontab
•Event-driven
•Streaming
•Streaming -> micro-batch
Crontab
•CloudWatch Event: Schedule
•Minimal interval: 1min
cron(fields)
Minutes 0-59 , - * /
Hours 0-23 , - * /
Day-of-month 1-31 , - * ? / L W
Month 1-12 or JAN-DEC , - * /
Day-of-week 1-7 or SUN-SAT , - * ? / L
Year 1970-2199 , - * /
Crontab Use Cases
•Health checks
•Crawler
•Recurring reports
Serverless Patterns
webmon Metrics
Codility-helper Daily Report
Common Lambda Patterns
•Direct invocation
•Crontab
•Event-driven
•Streaming
•Streaming -> micro-batch
Event-Driven
•Base of all patterns
•Triggered by CloudWatch Events
•Supports EBS, EC2, EMR,AutoScaling, CodeDeploy,
Console Sign-in, Health, KMS,Trusted Advisor, …
•`PutEvent`
Event-driven
Image
Bucket
1. Upload image
Lambda
4. Fetch
image
CloudWatch
Event
2.
3.
5. Convert
thumbnail
6. Store
thumbnail
Event-driven
APITier
DynamoDB
Table
1. Request upload
2. S3 Signed URL
3. Put metadata
Workers
Source
Bucket
4. Upload data
Lambda
5. PutObjcet
completed
6.Verify
metadata
Queue
7. Schedule
conversion
Destination
Bucket
8. Poll for
work
9. Convert
10. Upload
Common Lambda Patterns
•Direct invocation
•Crontab
•Event-driven
•Streaming
•Streaming -> micro-batch
Streaming
•Polls from Kinesis / DynamoDB stream
•Invoke function if data persists
•Scales with source stream
•Spontaneous retry
Streaming
Source
DynamoDB
Table
Lambda
1.
2.
Kinesis Stream Destination
DynamoDB
Table
Streaming
Source
DynamoDB
Table
Lambda
1.
2.
Kinesis Stream Destination
DynamoDB
Table
CloudSearch
3.
Common Lambda Patterns
•Direct invocation
•Crontab
•Event-driven
•Streaming
•Streaming -> micro-batch
Streaming -> Micro-Batch
•Data aggregation
•Reduce volume
•Windowed analysis
•Reasonably high frequency
•Reduce latency
•Preserve features
Streaming -> Micro-Batch
•CloudWatch Event: Schedule
CloudWatch
Event: Schedule
Lambda
Kinesis Stream
1.
2.
3.
S34.
Streaming -> Micro-Batch
•CloudWatch Event: Schedule
CloudWatch
Event: Schedule
Lambda
Kinesis Stream
1.
3.
S34.
2.
KinesisAnalytics
Gotcha
•Lambda scales. How about your resources?
•Connection pool
•EC2-based resources that scales much slower
•Throttle, queue, and retry
Dispatcher /Worker Pattern
Queue
Dispatcher
CloudWatch
Event: Schedule
1.
2. Poll for
task
Workers
Protected
Resources
4.
Gotcha
•How do I know if I’m throttled?
Gotcha
•How do I know if I’m throttled?
•Feature request:
Per-function concurrency cap!
Gotcha
•How to keep State among calls
•Externalize
•DynamoDB / S3
•Step Functions
Gotcha
•Elevated latency after deployment
•A-B deployment withTags
•Lazy resource initialization
Take Home Message
•Lambda is a glue service
•Event-driven and streaming
•Public endpoint <->VPC / EC2
•The only limit is your imagination
Questions and Answers
ThankYou

More Related Content

Serverless Patterns

Editor's Notes

  1. PaaS: Automation, Elasticity, Monitoring, Network, Scalability according to NIST Defn
  2. Lambda Request: $2E-7 calls Duration: $1.667E-5 / GB-s
  3. Workarounds: Divide and conquer * InvokeAsync or Step Function ** Pack static files with function
  4. Pattern 之間不互斥
  5. 最基本的模式,one-off jobs Cron 的寫法與 crontab 稍有不同,DoM 與 DoW 互斥,提供一者時,另外一項要寫 ?
  6. Pattern 之間不互斥
  7. Pattern 之間不互斥
  8. Poll interval: 1s 1 process per shard
  9. Pattern 之間不互斥
  10. 透過 Schedule 觸發,可自定 Interval 與輸出資料量;但仍有
  11. 透過 Schedule 觸發,可自定 Interval 與輸出資料量;但仍有
  12. Black belt question. Especially after deploying new versions. * A-B Deploy: leverage container reuse