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
910
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
AWS DevOps AgentのAzure接続機能を検証して見えた活用法/Use Cases Verified for the AWS DevOps Agent's Azure Connectivity Feature
masakiokuda
1
260
【デモ】Kiroで体験する仕様駆動開発|設計からコーディングまでAIと進める開発フロー
cmkudo
0
320
S3 を使うアプリケーションをローカル完結で動かすことに全力を注いでみた / Running S3 Apps Offline
contour_gara
0
570
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
5
500
楽しそうなつよつよエンジニアと目が死んでる僕/A brilliant engineer having a blast, and dead-eyed me.
3l4l5
2
230
変わらないものが、変わるものを決める — 意図駆動開発 × イベントソーシング × イミュータブル | What Doesn't Change Decides What Can — IDD × Event Sourcing × Immutability
tomohisa
0
1.8k
PostgreSQL 18で考えるUUID主キー
kazuhiro1982
0
500
freeeにおけるEvalsの実践例の紹介
freee
PRO
0
140
【QA Test Talk Vol.8】AI-DLC による Whole Team Approach の加速
pkshadeck
PRO
0
220
30年振りにコンパイラの定数整数除算を改善した
herumi
8
3.8k
プロポーザルを書いてもらう
pvcresin
0
570
AI Readyの正体はデータマネジメントだ メダリオン2.0の最前線
freee
PRO
0
350
Featured
See All Featured
Side Projects
sachag
455
43k
The Language of Interfaces
destraynor
162
27k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
57k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.1k
My Coaching Mixtape
mlcsv
0
260
Discover your Explorer Soul
emna__ayadi
2
1.3k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4.2k
So, you think you're a good person
axbom
PRO
2
2.1k
Visualization
eitanlees
152
17k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
New Earth Scene 8
popppiees
3
2.5k
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