In unserem Vortrag tauchen wir tiefer in die Serverless-Welt ein und zeigen wie eine produktionsreife Serverless-Anwendung mithilfe von AWS-Cloud mit dem Technologie-Stack API Gateway, SNS, Lambda and DynamoDB aufgebaut werden kann. Dabei gehen wir auf die Herausforderungen der jeweiligen Services ein, wie "cold start" bei Lamda oder "provisioned throughput" und "adaptice capacity" bei DynamoDB. Dabei zeigen wir, welche Strategien und Wege es gibt, damit umzugehen. Außerdem behandeln wir solche Themen wie Implementierung von Aggregationslogik und (Scheduled) Auto Scaling bei DynamoDB. Am Ende werfen wir einen Blick in die Zukunft und sprechen über die erste relationale serverless Datenbank "Aurora Serverless"
5. Revenue Share Calculator Service Requirements
1. Automatic calculation for the complete previous month on the 1st of each
month for each customer
2. Manual calculation via a simple GUI
• for the current month so far
• recalculation of a user selected month
3. Calculation results as downloads, download link per email
4. Archive with calculated results for each customer
5. For internal use only!
6. Serverless decision for RevShare project
• spike workloads only at the beginning of a month
• service is idle more than 99% of the time
➔ We use Function as a Service, to never pay for idle.
6
7. RS core
calculator
Turnover info
input data:
order data & calculation rules
output data:
RS calculation & input data
email with path to
download RS
calculation result
Central DB
Management
Server
extract customer, year
and month from the
file path
Key example: revsharetool-storage-demo/input-data/1000000/import/201907/revshare-final.json
RS calculator app
customer, year, month
12. Selecting memory size for a Lambda function
• Price 0,00001667 USD per GB-second
• CPU power allocation proportional to amount of memory
• more „GB“ will often just improve performance and not increase costs!
• cheaper GB setting may save costs, when lambda function
often has to wait
12
18. Lambda free tier
• 136170 sec. per month with the most powerful CPU setting
• = more than 1h per day
• with max 1 call every 2 sec.
18
19. Considerations for fast Lambda functions (<500ms)
• for billing, execution time will be rounded to 100 ms
• reducing real execution time from 105 ms to 95 ms can safe 50% costs
• might be possible by increasing memory by e.g. only 10%
• fee for the mere call: 0,20$ per million
• 1 million executions of 1 GB function with 1 sec runtime: > 16$
• 1 million executions of 128 MB function with 99 ms runtime: 0,208$
19
20. Amazon API
Gateway
• Expose Lambda functions as RESTful web
service
• API Management functionality built in:
• authorization and access control for API calls with IAM
• request monitoring and analytics
• overload protection
• version management
• APIs as products (SaaS, AWS Marketplace)
• SDK generation
Amazon API Gateway
21. API Gateway for different AWS services
• Access different AWS
services in a consistent
manner
22. https endpoints for Lambda functions
https://pe2r5mu2n3.execute-api.us-east-1.amazonaws.com/Beta/calculate
Application
Lambda
function
24. Cost influence of API gateway
24
0,408 1,032
1,656
2,28
2,904
3,528
4,152
4,776
3,908
4,532
5,156
5,78
6,404
7,028
7,652
8,276
100 MS 400MS 700MS 1000MS 1300MS 1600MS 1900MS 2200MS
duration per call
cost per million API calls in $ for a 128 MB
function
without API Gateway with API Gateway
35. Understanding DynamoDB Provisioned Throughput
DynamoDB works by provisioning throughput at the
table level. The throughput is set up as follows:
• Each write capacity unit (WCU) gives 1KB/s of
write throughput
• Each read capacity unit (RCU) gives 4KB/s of
read throughput
• Read and write throughput are independent
36. Partitioning Math
By Capacity = (Total RCU)/3000 + (Total WCU)/1000
By Size = Total size / 10 GB
Number of partitions = CEILING (MAX (Capacity, Size))
37. Partitioning Math
• Max RCUs per partition=3000
• Max WCUs per partition=1000
• Throughput (RCU & WCU) is uniformly spread
across partitions
• In case of exceeding throughput per partition -
ProvisionedThroughputExceededException
38. Partitioning Math
Table Size=40 GB , RCUs=1500, WCUs=400
By Capacity = 1500/3000 + 400/1000=0.9
By Size = 40 GB / 10 GB =4
Number of partitions = CEILING (MAX (0.9,4)) =4
RCUs per partition=1500/4 =375
WCUs per partition=400/4 =100
40. Configured Auto Scaling Example
Source: Yan Cui: "The problems with DynamoDB Auto Scaling and how it might be improved"
https://hackernoon.com/the-problems-with-dynamodb-auto-scaling-and-how-it-might-be-improved-a92029c8c10b
41. Scheduled Auto Scaling Example
aws application-autoscaling put-scheduled-action
--service-namespace dynamodb
--schedule "cron (50 1 1 * *)" // on the 1st of each month at 1:50 a.m.
--scheduled-action-name IncreaseReadCapacity
--resource-id arn:aws:dynamodb:region:account-id:your-table-name
--scalable-dimension dynamodb:table:ReadCapacityUnits
--scalable-targe-action MinCapacity=4,MaxCapacity=20
aws application-autoscaling delete-scheduled-action
--service-namespace dynamodb
--schedule "cron (30 3 1 * *)" // on the 1st of each month at 3:30 a.m.
--scheduled-action-name IncreaseReadCapacity
--resource-id arn:aws:dynamodb:region:account-id:your-table-name
--scalable-dimension dynamodb:table:ReadCapacityUnits
42. Database Capacity Planing Challenges
Source: „A Deep Dive into What's New for Amazon DynamoDB (DAT201) - AWS re:Invent 2018”
https://de.slideshare.net/AmazonWebServices/a-deep-dive-into-whats-new-for-amazon-dynamodb-dat201-aws-reinvent-2018
43. New Feature : DynamoDB on Demand Capacity
Source: https://aws.amazon.com/de/blogs/aws/amazon-dynamodb-on-demand-no-capacity-planning-and-pay-per-request-
pricing/
44. New Feature : DynamoDB on Demand Capacity
Source: „A Deep Dive into What's New for Amazon DynamoDB (DAT201) - AWS re:Invent 2018”
https://de.slideshare.net/AmazonWebServices/a-deep-dive-into-whats-new-for-amazon-dynamodb-dat201-aws-reinvent-2018
45. New Feature : DynamoDB on Demand Capacity
Source: „A Deep Dive into What's New for Amazon DynamoDB (DAT201) - AWS re:Invent 2018”
https://de.slideshare.net/AmazonWebServices/a-deep-dive-into-whats-new-for-amazon-dynamodb-dat201-aws-reinvent-2018
46. DynamoDB on Demand Capacity Limits
• 40,000 read request units and 40,000 write
request units per table in most regions
• DynamoDB On-Demand provisions capacity to
handle two times the past peak traffic
Source: „Understanding the scaling behaviour of DynamoDB OnDemand tables”
https://theburningmonk.com/2019/03/understanding-the-scaling-behaviour-of-dynamodb-ondemand-tables/
47. DynamoDB on Demand Capacity Use Cases
• You can’t predict your traffic patterns
• You are not worried about a runaway bill
(DynamoDB On-Demand pricing is about 6.94x
the cost of provisioned capacity)
Source: „DynamoDB On-Demand: When, why and how to use it in your serverless applications” by Alex DeBrie
https://serverless.com/blog/dynamodb-on-demand-serverless/
48. DynamoDB Capacities:
reserved vs provisioned vs on demand
1. If you have steady, predictable traffic, choose
reserved capacity
2. If you have variable, predictable traffic, choose
provisioned capacity
• Use (auto)-scaling
3. If you have variable, unpredictable traffic, choose
on-demand capacity
Source: „DynamoDB On-Demand: When, why and how to use it in your serverless applications” by Alex DeBrie
https://serverless.com/blog/dynamodb-on-demand-serverless/
49. DynamoDB canonical use cases
• Key-value lookups on well-distributed records
• Avoiding complex queries (and joins)
51. Challenges with DynamoDB
• Think of the access pattern in advance
• Difficult to change access patterns (partition&sort key)
& table structure afterwards
59. Use cases for Amazon Aurora Serverless
• Reasons to stay with relational database
• Seldom usage (weekly jobs) + additional spikes
• Dev/Test Database
• Noticable initial latencies acceptable
• Aurora and therefore Lambda are behind VPC
• Drastically increases also Lambda cold start
Source: Ajay Nair „Become a Serverless Black Belt” https://www.youtube.com/watch?v=oQFORsso2go
60. Amazon Aurora Serverless Data API
as beta for MySql available
Sources: https://docs.aws.amazon.com/de_de/AmazonRDS/latest/AuroraUserGuide/data-api.html
Jeremy Daly: „Aurora Serverless Data API: A First Look“ https://www.jeremydaly.com/aurora-serverless-data-api-a-first-look/
61. Amazon Aurora Serverless Data API Example
const AWS = require('aws-sdk')
const RDS = new AWS.RDSDataService()
exports.test = async (event, context) => {
const params = {
awsSecretStoreArn: 'arn:aws:secretsmanager:us-east-1:XXXXXXXXX:secret:test/data-api/mysql-test',
dbClusterOrInstanceArn: 'arn:aws:rds:us-east-1:XXXXXXXXXXX:cluster:test-data-api',
sqlStatements: `SELECT * FROM mytable WHERE id = :id`,
parameters: [ { name: 'id‘ , value: { “longValue“: 1 } } ],
database: 'test_data_api'
}
let data = await RDS.executeStatement(params).promise()
console.log(JSON.stringify(data, null, 2))
61
Source: Jeremy Daly: „Aurora Serverless Data API: An (Updated) First Look“ https://www.jeremydaly.com/aurora-serverless-data-
api-a-first-look/