Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Serverless for mere mortals
Search
Nikolay Sverchkov
April 22, 2019
Programming
1k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Serverless for mere mortals
Nikolay Sverchkov
April 22, 2019
More Decks by Nikolay Sverchkov
See All by Nikolay Sverchkov
Authorization in the GraphQL era
ssnickolay
0
1.1k
Rails 6: ActiveRecord
ssnickolay
0
54
Serverless Is Ruby Future
ssnickolay
0
900
Anything new Rails 6?
ssnickolay
1
1.5k
Evolution of Rails application architecture: 14 years in production
ssnickolay
3
1.5k
A Healthy Monolith
ssnickolay
0
1.6k
Elixir. There and Back Again
ssnickolay
0
860
Other Decks in Programming
See All in Programming
Building a Meta Ray-Ban display app
akkeylab
0
160
引き算の組織 ― アウトカムとAIに全振りするために辞めたこと ― / Organization by Subtraction
hirokiyamamoto14
PRO
0
230
React本体のコードリーディング
high_g_engineer
1
150
楽しそうなつよつよエンジニアと目が死んでる僕/A brilliant engineer having a blast, and dead-eyed me.
3l4l5
2
190
今さら聞けない .NET CLI
htkym
0
200
VibeCodingからAgenticWorkflowへ
starfish719
0
620
Jindong: Introducing Declarative Haptics in Compose Multiplatform
l2hyunwoo
0
120
SlackアプリとLambdaの 連携を構築した話
pawn_4_s
1
120
KotlinConf Extended South Korea 2026 Keynote
l2hyunwoo
0
110
AIを紡ぐPMのお話
swdtkuy
0
110
「人を評価する AI」の設計と実装
ryoyanara
0
210
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
4
460
Featured
See All Featured
Building an army of robots
kneath
306
46k
How GitHub (no longer) Works
holman
316
150k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
How to build a perfect <img>
jonoalderson
1
5.9k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.5k
Ethics towards AI in product and experience design
skipperchong
2
350
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.2k
A designer walks into a library…
pauljervisheath
211
24k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
410
Exploring anti-patterns in Rails
aemeredith
3
470
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
Transcript
SERVERLESS FOR MERE MORTALS NIKOLAY SVERCHKOV @ssnickolay
None
evilmartians.com
None
SERVERLESS FOR MERE MORTALS
OR
HOW I REDUCED COSTS BY 250 TIMES USING SERVERLESS
PAST
IN-HOUSE SERVER A LONG TIME AGO
A LONG TIME AGO MAINTAINABILITY IN-HOUSE SERVER
DEDICATED SERVER NOT SO LONG AGO ovh.com
DEDICATED SERVER NOT SO LONG AGO SCALABILITY
CLOUDS NOT SO LONG AGO
CLOUDS NOT SO LONG AGO
CLOUDS NOT SO LONG AGO
CLOUDS NOT SO LONG AGO ? CPU ? RAM ?
DISK
http://bit.ly/2X9qnp5
None
?
KANYE WEST
None
~66.500 RUB
LOAD 0 25 50 75 100 0 1 2 3
4 5 6 7
LOAD 0 25 50 75 100 0 1 2 3
4 5 6 7
CAPACITY PLANNING
None
CLOUDS NOT SO LONG AGO CAPACITY PLANNING
MORE ABSTRACTION! move away from server assessment models
None
2014
HOW IT WORKS
; ; ; AWS Lambda 1. SLEEP SOURCE_CODE.ZIP S3
AWS Lambda 1. SLEEP 2. EVENT
AWS Lambda 1. SLEEP 2. EVENT 3. HANDLE
function(event, context) { console.log(event.key1); return “Success”; }
AWS Lambda 1. SLEEP 2. EVENT 3. HANDLE 4. RESPONSE
; ; ; AWS Lambda 1. SLEEP 2. EVENT 3.
HANDLE 4. RESPONSE 5. -> 1
HTTP ❤ EVENT
BLOG + STATIC PAGES RAILS 3; DO; NGINX ASSETS PIPELINE
User Lambda Markdown -> HTML
User S3 Lambda Assets Markdown -> HTML CloudFront
User S3 Lambda Assets Markdown -> HTML + CloudFront
NOTE 1: THE RIGHT SOLUTION (JAMstack) +
NOTE 2: CAUTION OPEN FLAME
NOTE 2: CAUTION OPEN FLAME
CAUTION OPEN FLAME
⏱ DEC 2018 % +
DEC 2018 MARCH 2019 % +
VS http://bit.ly/2Dd7R7X
SERVERLESS
SERVERLESS $ serverless create !--template aws-go !!==> serverless.yml
SERVERLESS $ serverless deploy $ serverless rollback !--timestamp timestamp $
serverless invoke -f functionName $ serverless logs -f functionName $ serverless create !--template aws-go !!==> serverless.yml
SERVERLESS $ serverless deploy $ serverless rollback !--timestamp timestamp $
serverless invoke -f functionName $ serverless logs -f functionName $ serverless ???? $ serverless create !--template aws-go !!==> serverless.yml
SAM(-CLI) https://github.com/lambci/docker-lambda Docker images and test runners that replicate the
live AWS Lambda environment
TEMPLATES https://github.com/serverless/examples
serverless.yml functions: post: !<<: *default handler: bin/post events: - http:
path: blog/{slug} method: get
post.go package main import ( "github.com/aws/aws-lambda-go/events" "github.com/aws/aws-lambda-go/lambda" ) func Handler(ctx
context.Context, request Request) (events.APIGatewayProxyResponse, error) { !// put code here } func main() { lambda.Start(Handler) }
post.go func Handler(_, request Request) (_, _) { var buf
bytes.Buffer var slug = request.PathParameters["slug"] … }
post.go func Handler(_, request Request) (_, _) { var buf
bytes.Buffer var slug = request.PathParameters["slug"] post !:= lib.NewPost(slug, lambdaF.Config) if err !:= post.Render(&buf, lambdaF.Renderer); err !!= nil { return events.APIGatewayProxyResponse{StatusCode: 404}, err } … }
post.go func Handler(_, request Request) (_, _) { var buf
bytes.Buffer var slug = request.PathParameters["slug"] post !:= lib.NewPost(slug, lambdaF.Config) if err !:= post.Render(&buf, lambdaF.Renderer); err !!= nil { return events.APIGatewayProxyResponse{StatusCode: 404}, err } return lambdaF.Render(&buf) }
REDUCED x250 0.02$ vs 5$
+VAT = 6$!!! REDUCED x300
SERVERLESS UNDER THE MICROSCOPE
LOG $ serverless invoke -f index -l
LOG $ serverless invoke -f index -l START RequestId: <UUID>
Version: $LATEST END RequestId: <UUID> REPORT RequestId: <UUID> Duration: 1.48 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 31 MB
LOG $ serverless invoke -f index -l START RequestId: <UUID>
Version: $LATEST END RequestId: <UUID> REPORT RequestId: <UUID> Duration: 1.48 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 31 MB
LOG $ serverless invoke -f index -l START RequestId: <UUID>
Version: $LATEST END RequestId: <UUID> REPORT RequestId: <UUID> Duration: 1.48 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 31 MB
WHAT ABOUT CPU?
AWS Lambda allocates CPU power proportional to the memory… (с)
Amazon Docs
AWS Lambda allocates CPU power proportional to the memory… For
example, if you allocate 256 MB memory, your Lambda function will receive twice the CPU share than if you allocated only 128 MB. (с) Amazon Docs
https://aws.amazon.com/lambda/pricing/
6$ / PRICE(100ms) = 28.8M!
MEMORY / CST http://bit.ly/2UZ7xDL Completely Fair Scheduler (CFS)
FREE!
CST https://amzn.to/2Ix2t2O
COST !!<=> LANGUAGE
$ wrk -t10 -c400 -d20s -R40000 http://endpoint.url http://github.com/ssnickolay/dump2019
$ wrk -t10 -c400 -d20s -R40000 $ curl http://endpoint.url/[ruby/go] !=>
{“success”: true} http://github.com/ssnickolay/dump2019
0 250 500 750 1000 0 1 2 4 5
MAX AVG AVG ~80ms BATTLE (128 MB)
0 250 500 750 1000 0 1 2 4 5
MAX AVG AVG ~80ms 31712 requests in 20.01s Requests/sec: 1584.83 BATTLE (128 MB)
0 250 500 750 1000 0 1 2 4 5
0 250 500 750 1000 0 1 2 4 5 MAX AVG AVG ~1.8ms AVG ~80ms 31712 requests in 20.01s Requests/sec: 1584.83 50347 requests in 20.00s Requests/sec: 2517.30 BATTLE (128 MB)
0 250 500 750 1000 0 1 2 4 5
0 250 500 750 1000 0 1 2 4 5 MAX AVG AVG ~1.8ms AVG ~80ms 31712 requests in 20.01s Requests/sec: 1584.83 50347 requests in 20.00s Requests/sec: 2517.30 W IN BATTLE (128 MB)
0 250 500 750 1000 0 1 2 4 5
0 250 500 750 1000 0 1 2 4 5 MAX AVG AVG ~1.8ms AVG ~80ms 31712 requests in 20.01s Requests/sec: 1584.83 50347 requests in 20.00s Requests/sec: 2517.30 W IN + BATTLE (128 MB)
0 250 500 750 1000 0 1 2 4 5
0 250 500 750 1000 0 1 2 4 5 MAX AVG AVG ~1.8ms AVG ~80ms 31712 requests in 20.01s Requests/sec: 1584.83 50347 requests in 20.00s Requests/sec: 2517.30 BATTLE (128 MB)
BATTLE (128 MB) AVG ~1.8ms AVG ~80ms “Billed Duration: 100
ms” (c) Lambda Log
COST !!<=> PACKAGE SIZE
5.4MB 140KB COST !!<=> PACKAGE SIZE MIN SIZE
Go + WRK2 Milliseconds (AVG) 1,7 1,775 1,85 1,925 2
Megabytes 6 25 150 220
5.4MB 140KB COST !!<=> PACKAGE SIZE
http://rubyonjets.com/
COST !!<=> DEPENDENCIES
MINIMIZE THE COMPLEXITY OF YOUR DEPENDENCIES
MINIMIZE THE COMPLEXITY OF YOUR DEPENDENCIES
MINIMIZE THE COMPLEXITY OF YOUR DEPENDENCIES
WAIT A MINUTE
https://stackoverflow.com/q/54110697
When you write your Lambda function code, do not assume
that AWS Lambda automatically reuses the execution context for subsequent function invocations…. (c) AWS Docs AWS LAMBDA EXECUTION CONTEXT
func Fibonacci(n uint) uint { if n !<= 1 {
return n } return Fibonacci(n-1) + Fibonacci(n-2) } OPTION 1
func Fibonacci(n uint) uint { if n !<= 1 {
return n } return Fibonacci(n-1) + Fibonacci(n-2) } func main() { lambda.Start(Handler) } OPTION 1
func Fibonacci(n uint) uint { if n !<= 1 {
return n } return Fibonacci(n-1) + Fibonacci(n-2) } !// Handler is our lambda handler invoked by !// the `lambda.Start` function call func Handler(ctx context.Context) (Response, error) { Fib !:= Fibonacci(33) !// noise here resp !:= Response{ Body: Fib } … } func main() { lambda.Start(Handler) } OPTION 1
Milliseconds (AVG) 0 250 500 750 1000 1 2 3
4 Golang (128MB + Fib) ~ 400ms Option1
var Fib uint func Fibonacci(n uint) uint {…} func Handler(ctx
context.Context) (Response, error) { … } func init() { Fib = Fibonacci(33) !// noise here } func main() {…} OPTION 2
Milliseconds (AVG) 0 250 500 750 1000 1 2 3
4 Golang (128MB + Fib) ~ 400ms Option1 Option2
COST SPEED ⏫
0 15000 30000 45000 60000 0 1 2 4 5
50347 requests in 20.00s Requests/sec: 2517.30 LOAD !!<=> $$$
0 15000 30000 45000 60000 0 1 2 4 5
50347 requests in 20.00s Requests/sec: 2517.30 PROTECTION 1: X-API-KEY
0 15000 30000 45000 60000 0 1 2 4 5
PROTECTION 2: AWS SHIELD 50347 requests in 20.00s Requests/sec: 2517.30
0 15000 30000 45000 60000 0 1 2 4 5
50347 requests in 20.00s Requests/sec: 2517.30 62808 requests in 20.01s Non-2xx or 3xx responses: 62127 1 1000 CONCURRENCY PROTECTION 3: THROTTLING
PREDICTABLY
PREDICTABLY
PREDICTABLY
RECIPE
1. TAKE SERVERLESS
1. TAKE SERVERLESS 2. FOLLOW RULES
1. TAKE SERVERLESS 2. FOLLOW RULES 3. ????
1. TAKE SERVERLESS 2. FOLLOW RULES 3. ???? 4. PROFIT
WHAT ABOUT PRODUCTION?
WHAT ABOUT PRODUCTION? Tung Nguyen
@ssnickolay Nikolay Sverchkov @ssnickolay THANK YOU! @evilmartians http://evl.ms/blog http://evl.ms/telegram