mucon 2014 "building java microservices for the cloud"

80
Building Java (micro)services for the Cloud The DHARMA principles Daniel Bryant Principal Consultant, Open Credo [email protected] @danielbryantuk

Upload: daniel-bryant

Post on 13-Jul-2015

1.487 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: muCon 2014 "Building Java Microservices for the Cloud"

Building Java (micro)services for the CloudThe DHARMA principles

Daniel BryantPrincipal Consultant, Open Credo

[email protected]@danielbryantuk

Page 2: muCon 2014 "Building Java Microservices for the Cloud"

Who Am I?

• London Java Community Associate

• Adopt OpenJDK and JSR

27/11/2014 @danielbryantuk

• Principal Consultant at OpenCredo

Agile transformations

DevOps methodologies

Microservices and Cloud

Page 3: muCon 2014 "Building Java Microservices for the Cloud"

The Current Industry Wish List…

• Service-Oriented Architecture

• Cloud-based deployments

• DevOps Culture

@danielbryantuk27/11/2014

Page 4: muCon 2014 "Building Java Microservices for the Cloud"

The Current Industry Wish List…

• Service-Oriented Architecture (microservices)

– Today!

• Cloud-based deployments

– Today!

• DevOps Culture

– “Moving to DevOps” @ DevoxxUK bit.ly/1BylnZb

@danielbryantuk27/11/2014

Page 5: muCon 2014 "Building Java Microservices for the Cloud"

The obligatory microservice definition…

27/11/2014 @danielbryantuk

Page 6: muCon 2014 "Building Java Microservices for the Cloud"

Microservices…

• “SOA done right”

• “SRP” services– “Java, The Unix Way” (bit.ly/1cX8VsS)

• “Small” codebase services – 1000 LOC… 100… 10…?

• My personal opinion…

27/11/2014 @danielbryantuk

Page 7: muCon 2014 "Building Java Microservices for the Cloud"

“Can I fit the service in my head?”

27/11/2014 @danielbryantuk

Page 8: muCon 2014 "Building Java Microservices for the Cloud"

Common Cloud Problems

TL;DR…

27/11/2014 @danielbryantuk

Page 9: muCon 2014 "Building Java Microservices for the Cloud"

27/11/2014 @danielbryantuk

Not respecting the underlying environment

Page 10: muCon 2014 "Building Java Microservices for the Cloud"

27/11/2014 @danielbryantuk

Lack of application/platform monitoring…

Page 11: muCon 2014 "Building Java Microservices for the Cloud"

27/11/2014 @danielbryantuk

Bizarre failure modes…

Three certainties in life: Taxes, death and failure in production…

Page 12: muCon 2014 "Building Java Microservices for the Cloud"

27/11/2014 @danielbryantuk

Difficulty in understandingthe new architecture

Page 13: muCon 2014 "Building Java Microservices for the Cloud"

27/11/2014 @danielbryantuk

Confusion over environment provisioning and config

Page 14: muCon 2014 "Building Java Microservices for the Cloud"

27/11/2014 @danielbryantuk

Not testing in the Cloud…(hint: here be dragons!)

Page 15: muCon 2014 "Building Java Microservices for the Cloud"

We’ve created the “Cloud DHARMA Principles” to act as a checklist when building Cloud apps

27/11/2014 @danielbryantuk

Page 16: muCon 2014 "Building Java Microservices for the Cloud"

dharma/ˈdɑːmə,ˈdəːmə/

noun

1. Signifies behaviors that are considered to be in accord with order that makes life and universe possible (Hinduism)

2. "cosmic law and order”, but is also applied to the teachings of the Buddha (Buddhism)

27/11/2014 @danielbryantuk

Page 17: muCon 2014 "Building Java Microservices for the Cloud"

Documented (just enough)

Highly cohesive/loosely coupled (all the way down)

Automated from commit to Cloud

Resource aware

Monitored thoroughly

Antifragile

27/11/2014 @danielbryantuk

Page 18: muCon 2014 "Building Java Microservices for the Cloud"

Documented (just enough)

Highly cohesive/loosely coupled (all the way down)

Automated from commit to Cloud

Resource aware

Monitored thoroughly

Antifragile

27/11/2014 @danielbryantuk

Page 19: muCon 2014 "Building Java Microservices for the Cloud"

27/11/2014 @danielbryantuk

Page 20: muCon 2014 "Building Java Microservices for the Cloud"

Documentation (just enough)

• Provide a map for developers (and QA, Ops)

• Component purpose and interface/contract

• Initialisation instructions (mocks/stubs)

• Highlight areas of operational risk

27/11/2014 @danielbryantuk

Page 21: muCon 2014 "Building Java Microservices for the Cloud"

27/11/2014 @danielbryantuk27/11/2014 @danielbryantuk

leanpub.com/software-architecture-for-developers

Page 22: muCon 2014 "Building Java Microservices for the Cloud"

Simon Brown’s C4 Model

27/11/2014 @danielbryantuk

@simonbrownwww.codingthearchitecture.com

Page 23: muCon 2014 "Building Java Microservices for the Cloud"

27/11/2014 @danielbryantuk

Page 24: muCon 2014 "Building Java Microservices for the Cloud"

API Docs with Swagger

27/11/2014 @danielbryantuk

helloreverb.com/developers/swagger

Page 25: muCon 2014 "Building Java Microservices for the Cloud"

API Docs with Swagger

27/11/2014 @danielbryantuk

helloreverb.com/developers/swagger

Page 26: muCon 2014 "Building Java Microservices for the Cloud"

Create a PACT

27/11/2014 @danielbryantuk

github.com/DiUS/pact-jvm

martinfowler.com/articles/consumerDrivenContracts.html

Page 27: muCon 2014 "Building Java Microservices for the Cloud"

Documented (just enough)

Highly cohesive/loosely coupled (all the way down)

Automated from commit to Cloud

Resource aware

Monitored thoroughly

Antifragile

27/11/2014 @danielbryantuk

Page 28: muCon 2014 "Building Java Microservices for the Cloud"

High Cohesion / Loose Coupling(all the way down…)

“Fit for purpose architecture,

throughout the system”

1. Architect for encapsulation

2. Architect for scalability

3. Architect for comprehension

27/11/2014 @danielbryantuk

Page 29: muCon 2014 "Building Java Microservices for the Cloud"

Encapsulation: High Cohesion/Loose Coupling

• Code

• Modules– Components (bit.ly/1n7D0vp)– Services (bounded contexts)

• Public APIs– PayPal (bit.ly/1hnZNly)

• Deployment– 12factor.net

27/11/2014 @danielbryantuk

Page 30: muCon 2014 "Building Java Microservices for the Cloud"

Are Microservices a Silver Bullet?

• Single Responsibility Principle– Enforce service boundaries

– Bounded contexts (DDD)

• Separation of Concerns– Encapsulate what varies

– Easier to scale/tune independently

• Is this a free-lunch? (bit.ly/1gSw4L7)

27/11/2014 @danielbryantuk

Page 31: muCon 2014 "Building Java Microservices for the Cloud"

No!! Keep searching…

It’s all too easy to push complexity into orchestration and communication

27/11/2014 @danielbryantuk

www.codingthearchitecture.com/2014/07/06/distributed_big_balls_of_mud.html

Page 32: muCon 2014 "Building Java Microservices for the Cloud"

Scalability: The Three Ways

27/11/2014 @danielbryantuk

Page 33: muCon 2014 "Building Java Microservices for the Cloud"

The Scaling Cube

27/11/2014 @danielbryantuk

Page 34: muCon 2014 "Building Java Microservices for the Cloud"

The Scaling Cube

27/11/2014 @danielbryantuk

Cloning / ReplicationYou’re overly successful(and in trouble!)

Easy to implementCostly to run

Splitting aka ‘Microservices’Scaling requirements vary by ‘service’Rate of code change varies by ‘service’Uber-flexible distribution/scalingModeling & implementation non-trivial!

ShardingScaling requirements vary by data Current data store unit at capacityCan be non-invasiveThe decision of what to shard on?

Page 35: muCon 2014 "Building Java Microservices for the Cloud"

Comprehension: Smashing the Monolith…

• Business functionality -“Cart Service”– Noun, verb, SRP (slidesha.re/1owdJhh)

• Technology chunk - “Email Service”

• Vertical Slice - “Service per page” – Groupon (vimeo.com/105880150)

• Horizontal Slice - “User Repo” – An anti-pattern?

27/11/2014 @danielbryantuk

Page 36: muCon 2014 "Building Java Microservices for the Cloud"

DZone’s Enterprise Integration Guide

27/11/2014 @danielbryantuk

dzone.com/research/guide-to-enterprise-integration

Page 37: muCon 2014 "Building Java Microservices for the Cloud"

Documented (just enough)

Highly cohesive/loosely coupled (all the way down)

Automated from commit to Cloud

Resource aware

Monitored thoroughly

Antifragile

27/11/2014 @danielbryantuk

Page 38: muCon 2014 "Building Java Microservices for the Cloud"

Automated from Commit to Cloud

27/11/2014 @danielbryantuk

• Continuous Integration

• Continuous Deployment

• Continuous Delivery

Page 39: muCon 2014 "Building Java Microservices for the Cloud"

Build Pipeline

• Component Build

– Compile

– Unit Tests e.g. Maven Surefire

– Integration Tests (in-process) e.g. Maven Failsafe

• Deployment onto QA Cloud

– Probe health check endpoints

– Serverspec serverspec.org

27/11/2014 @danielbryantuk

Page 40: muCon 2014 "Building Java Microservices for the Cloud"

Build Pipeline

• Acceptance Tests– Cucumber (and Webdrivers)

– Use a Cloud environment!

• Performance Tests– Jmeter + Jenkins performance plugin

– Make sure environment and data is realistic!!

• Live Deployment?

27/11/2014 @danielbryantuk

Page 41: muCon 2014 "Building Java Microservices for the Cloud"

Microservice Pipeline

If you can’t deploy a service individually,

you’re probably aren’t creating ‘microservices’

Beware of the distributed monolith

“If you can't build a monolith, what makes you think microservices are the answer?”

@simonbrown bit.ly/1n7D0vp

27/11/2014 @danielbryantuk

Page 42: muCon 2014 "Building Java Microservices for the Cloud"

Infrastructure: Say No To Snowflakes!

• Automate all provisioning (store in SCM)

• Link infrastructure code to service

– Take care with versioning service/infra code

• Approaches…

– Separate infra repo (tagged with service version)

– Include in service repo (e.g. DockerFile)

27/11/2014 @danielbryantuk

Page 43: muCon 2014 "Building Java Microservices for the Cloud"

Infrastructure: Say No To Snowflakes!

• Fry...

– Chef, Puppet, SaltStack, Ansible

– Bash, Python (Fabric)

– Vendor APIs

• …or bake?

– Packer.io

– Netflix Aminator

27/11/2014 @danielbryantuk

Page 44: muCon 2014 "Building Java Microservices for the Cloud"

Infrastructure: Say No To Snowflakes!

• Doing “Proper Development”

– Gareth Rushgrove at Craft Conf (bit.ly/1njuc49)

– Chef Conf (www.youtube.com/user/getchef)

• Local tooling/testing

– Vagrant (www.vagrantup.com)

– Docker (www.docker.io)

– Fig (www.fig.sh)

27/11/2014 @danielbryantuk

Page 45: muCon 2014 "Building Java Microservices for the Cloud"

Configuring Apps in the Cloud

• Bundle config with app artifact

– Re-deploy entire app on change (easier with Docker?)

• Inject to app container on demand

– Deploy new local config file with each change

• Store externally

– Zookeeper & Curator curator.apache.org

– etcd github.com/coreos/etcd

27/11/2014 @danielbryantuk

Page 46: muCon 2014 "Building Java Microservices for the Cloud"

Automating QA

• Intra-component integration testing– Utilise embedded datastore/middleware

– Cucumber (typically via API/Webdriver & Serenity)

– Consider test UI for exploratory testing?

• Fault-tolerance– Chris Batey’s Skillscast (bit.ly/1tU6wZj)

– WireMock + Saboteur (wiremock.org)

– “Scassandra” (github.com/scassandra)

27/11/2014 @danielbryantuk

Page 47: muCon 2014 "Building Java Microservices for the Cloud"

Automating QA

• Inter-component integration testing

– The hardest part of SOA…

– Consider ‘synthetic txns’ (active monitoring)

– Consumer-driven Contracts github.com/DiUS/pact-jvm

• Service virtualisation

– VCR and Betamax (github.com/vcr/vcr)

– Mountebank (www.mbtest.org)

– Mock external services (e.g. Spring profiles)

27/11/2014 @danielbryantuk

Page 48: muCon 2014 "Building Java Microservices for the Cloud"

QA: Further Inspiration

27/11/2014 @danielbryantuk

Toby Clemson @ martinfowler.com/articles/microservice-testing

Page 49: muCon 2014 "Building Java Microservices for the Cloud"

Security: The Forgotten Part of QA

• Test credentials during automated acceptance– Target third-party integration points

• OWASP top ten– Zed Attack Proxy (bit.ly/1fjloVy, bit.ly/11Og39A)– Exploratory (penetration testing)

• Stand on the shoulders of giants– Creating your own crypto is not ‘cool’– Neither is inventing a ‘new’ security algorithm

27/11/2014 @danielbryantuk

Page 50: muCon 2014 "Building Java Microservices for the Cloud"

Documented (just enough)

Highly cohesive/loosely coupled (all the way down)

Automated from commit to Cloud

Resource aware

Monitored thoroughly

Antifragile

27/11/2014 @danielbryantuk

Page 51: muCon 2014 "Building Java Microservices for the Cloud"

Deployment Platform: What you’ve got…

27/11/2014 @danielbryantuk

Page 52: muCon 2014 "Building Java Microservices for the Cloud"

What you think you want…

27/11/2014 @danielbryantuk

Page 53: muCon 2014 "Building Java Microservices for the Cloud"

What you actually get…

27/11/2014 @danielbryantuk

Fact: 9 out of 10 cheetahs prefer thetaste of an Ops team over tinned food

Page 54: muCon 2014 "Building Java Microservices for the Cloud"

Thou Shalt Know they Cloud…

“Everything fails all the time [in the cloud]”

Werner Vogels, CTO, Amazon.com

• Everything is ephemeral

• Volatility

• Noisy (virtual) neighbours– bit.ly/1w1HQy7

27/11/2014 @danielbryantuk

Page 55: muCon 2014 "Building Java Microservices for the Cloud"

Thou Shalt Know thy Cloud…

• AWS “Magnetic” EBS 100 IOPS

– New SSD EBS 3K IOPS (burst, PIOPS available)

– My Mac SSD does 49K IOPS

• 1000Mbps network max transfer ~125MB/s

– My Mac does 400+ MB/s Sequential Write to SSD

Reference for Mac statistics: bit.ly/1ftJZH8

27/11/2014 @danielbryantuk

Page 56: muCon 2014 "Building Java Microservices for the Cloud"

Cultivating “Mechanical Sympathy”

• Understand the underlying Cloud fabric

• Virtualisation

– Tech Target (bit.ly/1kDVqyG)

• Networking

– ‘Unix and Linux System Administration Handbook’

– AWS docs aws.amazon.com/documentation

27/11/2014 @danielbryantuk

Page 57: muCon 2014 "Building Java Microservices for the Cloud"

Mechanical Sympathy by the Numbers

27/11/2014 @danielbryantuk

www.eecs.berkeley.edu/~rcs/research/interactive_latency.html

Page 58: muCon 2014 "Building Java Microservices for the Cloud"

Thinking/Acting Operationally

• You write it, you run it… (“dev on call”)

• Learn Linux fundamentals

• Diagnostic skills

– top, netstat, vmstat, tcpdump

– Java utils: jps, jstat, jmap, jhat

– “DevOps Troubleshooting” by K. Rankin

27/11/2014 @danielbryantuk

Page 59: muCon 2014 "Building Java Microservices for the Cloud"

Documented (just enough)

Highly cohesive/loosely coupled (all the way down)

Automated from commit to Cloud

Resource aware

Monitored thoroughly

Antifragile

27/11/2014 @danielbryantuk

Page 60: muCon 2014 "Building Java Microservices for the Cloud"

Monitor All The Things!

• Infrastructure monitoring– Nagios / Zabbix

– New Relic / AppDynamics

• Distributed Tracing – twitter.github.io/zipkin

• Centralised Logging– logstash.net

27/11/2014 @danielbryantuk

Page 61: muCon 2014 "Building Java Microservices for the Cloud"

The ‘ELK’ Stack

27/11/2014 @danielbryantuk

blog.comperiosearch.com/blog/2014/08/14/elk-one-vagrant-box

Page 62: muCon 2014 "Building Java Microservices for the Cloud"

Component Metrics

27/11/2014 @danielbryantuk

• Dropwizard’s Metrics– metrics.codahale.com

– Spring Boot (bit.ly/1rGo76V)

• Netflix’s Servo– github.com/Netflix/servo

• Etsy’s StatsD– github.com/etsy/statsd/wiki

Page 63: muCon 2014 "Building Java Microservices for the Cloud"

Health Checks

27/11/2014 @danielbryantuk

Page 64: muCon 2014 "Building Java Microservices for the Cloud"

Gauges, Counters, Meters, Timers…

27/11/2014 @danielbryantuk

Page 65: muCon 2014 "Building Java Microservices for the Cloud"

Graph It!

27/11/2014 @danielbryantuk

dashing.io

Page 66: muCon 2014 "Building Java Microservices for the Cloud"

27/11/2014 @danielbryantuk

Phrase borrowed from Etsy!

Page 67: muCon 2014 "Building Java Microservices for the Cloud"

27/11/2014 @danielbryantuk

Page 68: muCon 2014 "Building Java Microservices for the Cloud"

Documented (just enough)

Highly cohesive/loosely coupled (all the way down)

Automated from commit to Cloud

Resource aware

Monitored thoroughly

Antifragile

27/11/2014 @danielbryantuk

Page 69: muCon 2014 "Building Java Microservices for the Cloud"

Antifragile

• The opposite of fragile?

– Robust…

– Antifragile…

• Netflix are best-in-class

– bit.ly/1gs5n3q

• System must be robust first!

27/11/2014 @danielbryantuk

Page 70: muCon 2014 "Building Java Microservices for the Cloud"

Design for Failure

• Distributed Computing Principles

– Jeff Hodges ‘Distributed Systems’ (bit.ly/1FeaVtt)

– Scalable Web Architecture (bit.ly/1tt703O)

– ‘For young bloods’ (bit.ly/1pKVepz)

• Design patterns

– Timeouts / retries

– Bulkheads / circuit-breakers

27/11/2014 @danielbryantuk

Page 71: muCon 2014 "Building Java Microservices for the Cloud"

Timeouts

27/11/2014 @danielbryantuk

docs.guava-libraries.googlecode.com/…/concurrent/SimpleTimeLimiter.html

Page 72: muCon 2014 "Building Java Microservices for the Cloud"

Retries

27/11/2014 @danielbryantuk

github.com/rholder/guava-retrying

Page 73: muCon 2014 "Building Java Microservices for the Cloud"

27/11/2014 @danielbryantuk

github.com/Netflix/Hystrixprojects.spring.io/spring-cloud/

Circuit-breaker/bulkhead

Page 74: muCon 2014 "Building Java Microservices for the Cloud"

Antifragile Patterns: Elastic Scaling

• Scalability Rules

– Design for scaling: D 30x, I 3x, D 1.5x

– Strive for statelessness

– Cache appropriately (and on own tier)

– Expose load metrics

– Separate deploy and release

– Favour async communication

– Avoid coordination and ACID

27/11/2014 @danielbryantuk

Page 75: muCon 2014 "Building Java Microservices for the Cloud"

Antifragile Patterns: Elastic Scaling

Stateless components

Distributed data stores / caches

27/11/2014 @danielbryantuk

Page 76: muCon 2014 "Building Java Microservices for the Cloud"

• Asynchronous Communication

– Message queue www.rabbitmq.com

– Pub/sub redis.io

• Take it to the next level

– Reactive github.com/ReactiveX/RxJava

– CQRS/ES martinfowler.com/bliki/CQRS.html

Antifragile Patterns: Async FTW

27/11/2014 @danielbryantuk

Page 77: muCon 2014 "Building Java Microservices for the Cloud"

Antifragile Patterns: Respect the CAP

Eventual consistency (ACID vs BASE)

27/11/2014 @danielbryantuk

en.wikipedia.org/wiki/CAP_theoremcloudshankar.blogspot.co.uk/2013/05/eventual-consistency.htmlwww.dataversity.net/acid-vs-base-the-shifting-ph-of-database-transaction-processing/

Page 78: muCon 2014 "Building Java Microservices for the Cloud"

Documented (just enough)

Highly cohesive/loosely coupled (all the way down)

Automated from commit to Cloud

Resource aware

Monitored thoroughly

Antifragile

27/11/2014 @danielbryantuk

So, Cloud services are ‘done’ when…

Page 79: muCon 2014 "Building Java Microservices for the Cloud"

27/11/2014 @danielbryantuk

Page 80: muCon 2014 "Building Java Microservices for the Cloud"

Thanks For Listening!

• Massive thanks

– OpenCredo (@OpenCredo)

– www.notonthehighstreet.com

• Questions / comments?

[email protected]

– @danielbryantuk

27/11/2014 @danielbryantuk