Serverless and Java in the Real World @johnchapin | - - PowerPoint PPT Presentation

serverless and java in the real world
SMART_READER_LITE
LIVE PREVIEW

Serverless and Java in the Real World @johnchapin | - - PowerPoint PPT Presentation

Serverless and Java in the Real World @johnchapin | john@symphonia.io Fearless AWS Lambdas QCon NYC 2017 https://bit.ly/symph-qcon-fearless Learning Lambda Mike Roberts mike@symphonia.io https://bit.ly/symph-ll Agenda 1. Choosing Java for


slide-1
SLIDE 1

Serverless and Java in the Real World

@johnchapin | john@symphonia.io

slide-2
SLIDE 2
slide-3
SLIDE 3

Fearless AWS Lambdas

QCon NYC 2017 https://bit.ly/symph-qcon-fearless

slide-4
SLIDE 4

Learning Lambda

Mike Roberts mike@symphonia.io https://bit.ly/symph-ll

slide-5
SLIDE 5

Agenda

  • 1. Choosing Java for Serverless (and AWS Lambda)
  • 2. Structuring a Serverless Java project
  • 3. Logging and metrics
  • 4. Building and deploying
  • 5. Live examples
slide-6
SLIDE 6

Choosing Java for Serverless
 (and AWS Lambda)

slide-7
SLIDE 7

AWS Lambda runtimes

  • Node.js
  • Python
  • Java (and Scala, Clojure, Kotlin...)
  • Go
  • C#
  • Anything else you want, via a Node.js shim
slide-8
SLIDE 8

How do we choose a runtime?

slide-9
SLIDE 9

Business/Technical Decision

Is there a requirement for long-tail, low-latency, synchronous

  • perations?

Probably not Java (or C#).

slide-10
SLIDE 10

Team Decision

Does the team have (or want to have) experience with a specific runtime? Make the team happy.

slide-11
SLIDE 11

The best use case for Serverless Java

slide-12
SLIDE 12

Kinesis processing

Latency tolerant Regular, frequent invocations Not particularly bursty Computationally intensive

slide-13
SLIDE 13

Asynchronous, high-throughput

slide-14
SLIDE 14

Structuring a Serverless Java Project

slide-15
SLIDE 15

What is a project?

slide-16
SLIDE 16
slide-17
SLIDE 17

Domain-Driven Design

Eric Evans, 2003

slide-18
SLIDE 18

One service = one project

  • Service = bounded context (probably)
  • A "service" in the AWS Serverless world might be made up
  • f several Lambda functions, and some associated

infrastructure.

  • The "backbone" infrastructure of the system may not

belong to a specific service (but should be owned by a single team).

slide-19
SLIDE 19
slide-20
SLIDE 20

Multi-module Maven project

  • Hierarchical POM files
  • Parent
  • Lambdas
  • Libraries
  • AWS Bill of Materials

(BOM) at top-level

slide-21
SLIDE 21

The Lambda diet

Fewer classes = faster startup

  • Ruthlessly cull dependencies
  • AWS libraries can be bloated!

mvn dependency:tree, sbt dependencyStats

slide-22
SLIDE 22

Other useful libraries

  • https://github.com/aws/aws-lambda-java-libs
  • Recently updated, more events and Log4J2 support!
  • https://github.com/symphoniacloud/lambda-monitoring
  • Logging + metrics, PRs welcome!
slide-23
SLIDE 23

Logging and Metrics

slide-24
SLIDE 24

The Present and Future of Serverless Observability

  • Yan Cui, yesterday here at QCon
slide-25
SLIDE 25

Logging

  • System.out/err goes to CloudWatch Logs
  • One “log group” per Lambda (by default)
  • Within “log group”, one “log stream” per container
  • From CloudWatch, can aggregate/forward
slide-26
SLIDE 26

System.out.nope

  • System.out.println is bad for the normal reasons
  • *Real* logging is better
  • Lambda runtime can add RequestId to Log4J logs
  • NEW! aws-lambda-java-log4j uses Log4J 2
slide-27
SLIDE 27

lambda-logging

  • SLF4J + Logback
  • Sane default configuration w/ AWS RequestId
  • Open Source (Apache 2 license)
  • io.symphonia/lambda-logging “1.0.1”
  • - github.com/symphoniacloud/lambda-monitoring/
slide-28
SLIDE 28

package io.symphonia; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LoggingLambda { Logger LOG = LoggerFactory.getLogger(LoggingLambda.class); public void handler(String input) { LOG.info("Hello, {}", input); } } START RequestId: 084c7cbf Version: $LATEST [2017-04-02 00:32:10.486] 084c7cbf INFO i.s.LoggingLambda - Hello, John END RequestId: 084c7cbf REPORT RequestId: 084c7cbf Duration: 1.52 ms Billed Duration: 100 ms …

slide-29
SLIDE 29

CloudWatch Metrics

  • No built-in business metrics
  • Lambda platform metrics
  • Errors, Duration, Invocations, Throttles
  • Naive metrics collection approach is dangerous!
  • Cloudwatch has account-level API limits 🔦
slide-30
SLIDE 30

CloudWatch Metric Filters

  • Built into Cloudwatch! Scalable!
  • Scrape Cloudwatch Logs data using special (finicky)

patterns

  • Generates and posts Cloudwatch metrics in batch
slide-31
SLIDE 31

lambda-metrics

  • Codahale metrics and lambda-logging
  • Maven plugin builds Metric Filters to scrape logs, post to

CloudWatch Metrics

  • Open Source (Apache 2 license)
  • io.symphonia/lambda-metrics “1.0.1”
  • github.com/symphoniacloud/lambda-monitoring
slide-32
SLIDE 32

package io.symphonia; import com.codahale.metrics.Counter; import io.symphonia.lambda.annotations.CloudwatchMetric; import io.symphonia.lambda.metrics.LambdaMetricSet; import org.slf4j.*; public class MetricLambda { Logger LOG = LoggerFactory.getLogger(MetricLambda.class); private class Metrics extends LambdaMetricSet { @CloudwatchMetric Counter inputBytes = new Counter(); } public void handler(String input) { Metrics metrics = new Metrics(); metrics.inputBytes.inc(input.length()); metrics.report(LOG); } }

slide-33
SLIDE 33

START RequestId: 084c7cbf Version: $LATEST 084c7cbf METRIC i.s.Lambda type COUNTER name \ io.symphonia.Lambda.Metrics/inputBytes count 3 END RequestId: 084c7cbf REPORT RequestId: 084c7cbf Duration: 1.52 ms Billed \ Duration: 100 ms … [request_id, level=METRIC, logger, type_label, type=COUNTER, name_label, name=”io.symphonia.Lambda.Metrics/inputBytes”, count_label, count]

slide-34
SLIDE 34

START RequestId: 084c7cbf Version: $LATEST 084c7cbf METRIC i.s.Lambda type COUNTER name \ io.symphonia.Lambda.Metrics/inputBytes count 3 END RequestId: 084c7cbf REPORT RequestId: 084c7cbf Duration: 1.52 ms Billed \ Duration: 100 ms … [request_id, level=METRIC, logger, type_label, type=COUNTER, name_label, name=”io.symphonia.Lambda.Metrics/inputBytes”, count_label, count]

slide-35
SLIDE 35

START RequestId: 084c7cbf Version: $LATEST 084c7cbf METRIC i.s.Lambda type COUNTER name \ io.symphonia.Lambda.Metrics/inputBytes count 3 END RequestId: 084c7cbf REPORT RequestId: 084c7cbf Duration: 1.52 ms Billed \ Duration: 100 ms … [request_id, level=METRIC, logger, type_label, type=COUNTER, name_label, name=”io.symphonia.Lambda.Metrics/inputBytes”, count_label, count]

slide-36
SLIDE 36

Building and Deploying

slide-37
SLIDE 37

Continuous Delivery in an Ephemeral World

O'Reilly SACON NYC 2018 https://conferences.oreilly.com/software-architecture/sa-ny/public/schedule/detail/63860

slide-38
SLIDE 38

Build pipeline

  • Infrastructure as code!
  • AWS CodePipeline / CodeBuild
  • Separate repository
  • e.g., "serverless-app-build" repository
  • Cache dependencies (via CodeBuild caching)
slide-39
SLIDE 39

Application and infrastructure

  • Continuously deploy code *and* infrastructure
  • Alongside the application source code
  • buildspec.yml
  • sam.yml
slide-40
SLIDE 40

SAM vs Serverless Framework

  • Serverless Application Model
  • AWS-specific
  • Different flavor of CloudFormation
  • Serverless Framework
  • 3rd-party, supports multiple cloud vendors
  • CloudFormation under the hood (for AWS)
  • Plugin architecture
slide-41
SLIDE 41

Reproducible builds

  • Maven-produced JAR files are non-deterministic
  • Datetime stamp in "pom.properties"
  • Other timestamps, file ordering, etc
  • SAM relies on file hashes to detect updates
slide-42
SLIDE 42

Examples

https://github.com/symphoniacloud/qcon-london-2018

slide-43
SLIDE 43

Flash sale!

http://bit.ly/symph-qcon-2018-demo

slide-44
SLIDE 44

In conclusion

  • For the right use-case, choose the Java runtime!
  • Services = multi-module Maven projects
  • Log and collect metrics correctly and scalably
  • Infrastructure as code, continous delivery are paramount
slide-45
SLIDE 45

Questions?

@johnchapin | john@symphonia.io