clojure applications in building serverless · brief introduction to serverless architecture aws...

32
Building Serverless Applications in Clojure Stuart Robinson - Principal Engineer @ Funding Circle

Upload: others

Post on 17-Oct-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Building Serverless Applications in ClojureStuart Robinson - Principal Engineer @ Funding Circle

Page 2: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Overview

Serverless IntroBrief introduction to Serverless Architecture

AWS LambdaWorking with AWS Lambda, API Gateway and Clojure

Full Stack ArchitectureDeploying a full stack Clojure application to AWS

DemoA simple chat application

Lessons LearnedGood practices and things to avoid.

2

Page 3: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

What does “serverless” mean?

Page 4: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

What does “serverless” mean?

No more servers?

Page 5: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

What does “serverless” mean?

There are still servers but you can’t see them..

Page 6: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

“Build and run applications without thinking

about servers - Amazon

Page 7: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Comparison with server based model

Server based application deployment

● Servers need to be provisioned and configured

● Usually at least one instance running

● Pay for running instances

● Instances are generally long lived

Serverless application deployment

● No provisioning required

● May have zero instances running

● Pay when your code executes

● Instances are short lived

Page 8: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Serverless / FaaS Providers

Provider Language Support Pricing Model

AWS Lambda JavascriptPythonJavaC#Go

Per GB per second

Azure Functions JavascriptPython JavaC#PHPBash

Per GB per second

Google Cloud Functions Javascript Per invocation

Page 9: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Amazon Serverless Components

Kinesis Cognito

Page 10: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Building a Clojure API on AWS Lambda

Page 11: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

AWS Lambda

• AWS lambda is a serverless compute service

• Lambda functions are executed inside containers (VMs)

• Lambda containers are stateless*

• Containers are started and stopped on demand (freeze / thaw cycle)

• Containers that are no longer needed are destroyed

• Lambda automatically scales to meet demand (up to 1000 concurrent requests)

Page 12: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Writing a simple Lambda function in Clojure

Page 13: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Packaging and deployment

Using AWS command line tools:

lein uberjar

aws lambda create-function

--function-name clojure-serverless-demo

--handler clojure_serverless_demo.SimpleHander

--runtime java8

--memory 512

--timeout 10

--role arn:aws:iam::231321312:role/lambda_exec_role

--zip-file fileb://./target/clojure-serverless-demo-standalone.jar

Page 14: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Serverless Framework

14

• Command line tool for packaging and deploying serverless applications

• Support for many providers (Azure, Google, AWS)

• Allows you to codify all your serverless resources in a single serverless.yml file

• Extensible with plugins and templates

Page 15: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Serverless.yml

Page 16: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

HTTP with API Gateway

• Serverless API proxy

• Support for authentication, rate limiting and caching

• An event source for Lambda functions

• Can act as proxy for other APIs

• Support for multiple versions and environments as well as canary deployments

Page 17: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Configuring API gateway

Page 18: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

An API Gateway Request Event

Page 19: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Converting API Gateway request to a Ring request

Page 20: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

A RESTful API on Lambda

Page 21: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Back-end Architecture

Page 22: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Full stack Application Architecture

Page 23: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Demo

Page 24: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Links • Demo Address: http://chat.stuart.cloud

• Github: https://github.com/stuart-robinson/clojure-serverless-demo

Page 25: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Lessons Learned

Page 26: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Testing

• Separate your application core from the AWS plumbing code

• Test your core logic locally with unit tests

• Some of the AWS services have versions that can be run locally (Kinesis, Dynamodb etc) for integration style tests

• Test environments on AWS cost (next to) nothing while they are not in use

Page 27: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Monitoring and Maintenance

• Lots of monitoring provided by AWS out of the box (Cloudwatch, X-ray)

• Log as much as possible

• Setup alerts so you can determine when you’re being throttled

• Monitor your costs very carefully

• Watch for circular calls

Page 28: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Performance

• Time to serve request from cold start:

- Lambda Java Runtime: ~ 600ms

- Lambda Java Runtime + Clojure ~ 3000ms

- Lambda Java Runtime + Clojure + API Gateway ~ 6000ms

• Request time once warm: < 100ms

• Reduce colds start times by:

- Pruning unused libraries and excluding dev dependencies

- Allocating more memory (CPU is allocated proportionally to memory)

- Keep function warm by regular polling (every 15mins)

- Using ClojureScript on NodeJS runtime

Page 29: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Costs - 25 Million requests per month (~10/sec)

• AWS Lambda

- Cost = duration * memory usage (+ 0.20 per 1M invocations)

- Minimum billable duration = 100ms

- 512 Mb memory is sufficient for a small Clojure project

- £25.85 (excludes free tier allowance)

• DynamoDB

- 40 Reads/sec + 10 Writes/sec ~ £10

• API Gateway

- ~ £2.50/million requests

- £75 for 25 million requests

• Total Cost ~ £110

Page 30: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Summary

• Deploying Clojure apps to Lambda is pretty straightforward

• Working with Lambda enables you to focus on the application code without worrying about infrastructure

• Cold start times for Clojure are woeful compared to other languages

• Need to be monitor costs and optimize accordingly

30

Page 31: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying

Questions?

Page 32: Clojure Applications in Building Serverless · Brief introduction to Serverless Architecture AWS Lambda Working with AWS Lambda, API Gateway and Clojure Full Stack Architecture Deploying