serverless and streams at
Yan Cui
http://theburningmonk.com
@theburningmonk
Principal Engineer @
Independent Consultant
“Netflix for sports”
offices in London, Leeds, Katowice and Amsterdam
available in Austria, Switzerland, Germany,
Japan, Canada, Italy, US, Spain and Brazil
available on 30+ platforms
~1,000,000 concurrent viewers
“Netflix for sports”
offices in London, Leeds, Katowice and Amsterdam
We’re hiring! Visit
engineering.dazn.com to
learn more.
follow @DAZN_ngnrs for
updates about the
engineering team.
we use a LOT
serverless-first
there are no silver bullets
serverless is not right for
every use case (yet)
~1,000,000 concurrent viewers
we use a LOT
API Gateway SQS SNS Step Functions
Kinesis
Kinesis
as a task queue
Kinesis
as a task queue as a transaction log
Kinesis
as a task queue as a transaction log
part of an
analytics pipeline
https://noti.st/chrismunns/uqUWri/slides
https://noti.st/chrismunns/uqUWri/slides
stream
stream
shard
shard
shard
stream
shard
shard
shard
PutRecord: {
PartitionKey: …
Data: …
}
stream
shard
shard
shard
PutRecord: {
PartitionKey: …
Data: …
}
stream
shard
shard
shard
doX
doX
doX
PutRecord: {
PartitionKey: …
Data: …
}
stream
shard
shard
shard
doX
doX
doX
PutRecord: {
PartitionKey: …
Data: …
}
ordering is preserved within
the same partition key
stream
shard
shard
shard
doX
doX
doX
PutRecord: {
PartitionKey: …
Data: …
}
ordering is preserved within
the same partition key
data is retained for up to 7
days, default is 24 hours
doX
oldest newest
shard
doX
oldest newest
shard
stream
shard
shard
shard
doX
doX
doX
stream
doY
doX
shard
doY
doX
doY
doX
shard
shard
stream
doY
doX
shard
doY
doX
doY
doX
shard
shard
every subscriber gets the same
data, in the same order
stream
doY
doX
shard
doY
doX
doY
doX
shard
shard
every subscriber gets the same
data, in the same order
each subscriber process the
stream at their own pace
stream
doY
doX
doY
doX
doY
doX
shard
shard
INGRESS
1MB/s data, 1000 records/s
shard
stream
doY
doX
doY
doX
doY
shard
INGRESS
1MB/s data, 1000 records/s
shard
EGRESS
2MB/s data per subscriber, 5 reads/s
doX
shard
stream
shard
shard
shard
shard
shard
horizontalscalability
stream as a task queue
vs. vs.
Kinesis SQS SNS
ordering
replay events
Kinesis SQS SNS
mode
retry
concurrency
subscribers
ordering
replay events
Kinesis SQS SNS
mode
retry
concurrency
subscribers many one-to-one many
ordering
replay events
Kinesis SQS SNS
by shard
none (standard)
global (FIFO)
none
mode
retry
concurrency
subscribers many one-to-one many
ordering
replay events
Kinesis SQS SNS
by shard
none (standard)
global (FIFO)
none
up to 7 days none none
mode
retry
concurrency
subscribers many one-to-one many
ordering
replay events
Kinesis SQS SNS
by shard
none (standard)
global (FIFO)
none
up to 7 days none none
mode
retry
batched batched (up to 10) singular
concurrency
subscribers many one-to-one many
ordering
replay events
Kinesis SQS SNS
by shard
none (standard)
global (FIFO)
none
up to 7 days none none
mode
retry
batched batched (up to 10) singular
retried until
success
retry + DLQ retry + DLQ
concurrency
subscribers many one-to-one many
ordering
replay events
Kinesis SQS SNS
by shard
none (standard)
global (FIFO)
none
up to 7 days none none
mode
retry
batched batched (up to 10) singular
retried until
success
retry + DLQ retry + DLQ
concurrency 1 per shard auto-scaled fan-out!!!
subscribers many one-to-one many
ordering
replay events
Kinesis SQS SNS
by shard
none (standard)
global (FIFO)
none
up to 7 days none none
mode
retry
batched batched (up to 10) singular
retried until
success
retry + DLQ retry + DLQ
concurrency 1 per shard auto-scaled fan-out!!!
subscribers many one-to-one many
use the concurrency model to amortize spikes in traffic
leveraging the retry-until-success behaviour
cost is scale-sensitive
1 msg/s for a month, 1KB per msg
1 x 60s x 60m x 24hr x 30days
@ $0.014 per mil
+
24hrs x 30days
@ $0.015 per hr
$10.836
1 x 60s x 60m x 24hr x 30days
@ $0.5 per mil
$1.296
1 x 60s x 60m x 24hr x 30days
@ $0.4 per mil (send)
+
1 x 60s x 60m x 24hr x 30days
@ 10 batch
@ 20s long polling
@ $0.4 per mil (receive)
$1.089
1K msg/s for a month, 1KB per msg
1k x 60s x 60m x 24hr x 30days
@ $0.014 per mil
+
24hrs x 30days
@ $0.015 per hr
$47.088
1k x 60s x 60m x 24hr x 30days
@ $0.5 per mil
$1296.00
1k x 60s x 60m x 24hr x 30days
@ $0.4 per mil (send)
+
1k x 60s x 60m x 24hr x 30days
@ 10 batch
@ 20s long polling
@ $0.4 per mil (receive)
$1041.98
stream as a transaction log
http://bit.ly/2Dpidje
events are an enabler for
COMPOSABILITY
AWS LAMBDA
is the...
Kinesis
Kinesis
API Gateway AWS Lambda API GatewayAWS Lambda
service-A service-B
Kinesis
API Gateway AWS Lambda API GatewayAWS Lambda
service-A service-B
Kinesis
API Gateway AWS Lambda API GatewayAWS Lambda
service-A service-B
AWS Lambda
AWS Lambda
AWS Lambda
Kinesis
API Gateway AWS Lambda API GatewayAWS Lambda
service-A service-B
AWS Lambda
AWS Lambda
AWS Lambda DynamoDBIOT
Kinesis
API Gateway AWS Lambda API GatewayAWS Lambda
service-A service-B
AWS Lambda
AWS Lambda
AWS Lambda DynamoDBIOT
Kinesis
API Gateway AWS Lambda API GatewayAWS Lambda
service-A service-B
AWS Lambda
AWS Lambda
AWS Lambda DynamoDBIOT
AWS Lambda AWS Lambda
build loosely-coupled system
through events
service A service B
service C service D
bounded context
bounded context
service A service B
service C service D
bounded context
bounded context
service A service B
service C service D
service A service B
service C service D
service A service B
service C service D
service A service B
service C service Dbackward-compatible?
bounded context
DON’T use events to
orchestrate workflows
within the same
bounded context
bounded context
adds unnecessary
complexity to logging,
tracing, and end-to-end
reporting
bounded context
the workflow doesn’t exist
as a standalone concept,
but as the sum of a series
of loosely connected parts
Step Functions
use Step Functions instead
Step Functions
don’t forget to emit events
from the workflow
Step Functions
so others can react to state
changes that happened as
part of the workflow
analytics pipeline
S3 Buckets
S3 BucketsKinesis Streams Kinesis Firehose
S3 BucketsKinesis Streams
AWS Lambda
Kinesis Firehose
S3 BucketsKinesis Streams
AWS Lambda AWS Lambda
Kinesis Firehose
S3 BucketsKinesis Streams
AWS Lambda AWS LambdaAWS Lambda
Kinesis Firehose
S3 BucketsKinesis Streams
AWS Lambda AWS LambdaAWS Lambda
DynamoDB
ElasticSearch
Kinesis Firehose
S3 BucketsKinesis Streams
AWS Lambda AWS Lambda
Athena QuickSight
AWS Lambda
Kinesis Firehose
S3 BucketsKinesis Streams
AWS Lambda AWS Lambda
Athena QuickSight
AWS Lambda
Kinesis Firehose
secure sensitive data both
at rest and in-transit
leverage server-side encryption
http://amzn.to/1N3Twb8
http://amzn.to/1xF41eX
http://amzn.to/2tgvFR2
http://amzn.to/2GCMbAT
principle of Least Privilege
keep functions single-purposed
& apply per function policies
http://bit.ly/2QIVYvL
https://lumigo.io/blog/lambda-and-kinesis-beware-of-hot-streams/
stream
doY
doX
doY
doX
doY
shard
shard
EGRESS
2MB/s data per subscriber,
doX
shard
5 reads/s
throughput is maintained as you
add more subscribers, but…
it’s difficult to add more than 15
subscriber functions to a stream
my microservices
user_events
order_events
promo_events
my microservices
user_events
order_events
promo_events
my microservices
still possible to have too many
subscribers for a stream
user_events
order_events
promo_events
my microservices
implicit coupling between
publisher and subscriber
user_events
order_events
promo_events
my microservices
lose ordering of related
events in different streams
lose ordering of related
events in different streams
preserve ordering of
related events
extra cost for these
consumer apps
http://bit.ly/2hxZGui
observability
{
error: …
message: …
correlation-id: …
}
{
message: …
correlation-id: …
}
https://github.com/getndazn/dazn-lambda-powertools
@theburningmonk
theburningmonk.com
github.com/theburningmonk

Streams and serverless at DAZN