Top Banner
@chbatey Microservices in the real world Christopher Batey Freelancer / The Last Pickle @chbatey
61

LJC: Microservices in the real world

Feb 10, 2017

Download

Engineering

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: LJC: Microservices in the real world

@chbatey

Microservices in the real world

Christopher Batey Freelancer / The Last Pickle @chbatey

Page 2: LJC: Microservices in the real world

@chbatey

S!*t happens when you put a network between your classes

Christopher Batey Freelancer / The Last Pickle @chbatey

Page 3: LJC: Microservices in the real world

@chbatey

Who am I?

• Freelance consultant: Docker, Kubernetes, Cassandra, JVM

• Work on a OS testing library for Cassandra

• Twitter: @chbatey

Heavily recruiting!!

Page 4: LJC: Microservices in the real world

@chbatey

Background

Page 5: LJC: Microservices in the real world

@chbatey

Background

Page 6: LJC: Microservices in the real world

@chbatey

Background

Page 7: LJC: Microservices in the real world

@chbatey

Background

Page 8: LJC: Microservices in the real world

@chbatey

Background

Page 9: LJC: Microservices in the real world

@chbatey

But why?????

Page 10: LJC: Microservices in the real world

@chbatey

Small horizontal scalable services

• Move to small services independently deployed

- Login service

- Device service

- etc

• Move to a horizontally scalable Database that can run active active in multiple data centres

Page 11: LJC: Microservices in the real world

@chbatey

Half way point

Page 12: LJC: Microservices in the real world

@chbatey

So... what do systems look like now?

Page 13: LJC: Microservices in the real world

@chbatey

Tech used

• All examples are on github

• Technologies used:

- Dropwizard

- Wiremock

- Hystrix

- Saboteur

Page 14: LJC: Microservices in the real world

@chbatey

Java microservice definition?

A microservice in Java == no dependency injection framework

Page 15: LJC: Microservices in the real world

@chbatey

Testing microservices

• You don’t know a service is fault tolerant if you don’t test faults

Page 16: LJC: Microservices in the real world

@chbatey

Isolated service tests

Prime

Movie servicePlay Movie

Test

Real HTTP/TCP

Test

Page 17: LJC: Microservices in the real world

@chbatey

Fault tolerance1.Don’t take forever - Timeouts2.Don’t try if you can’t succeed 3.Don’t whack a dead horse4.Turn broken stuff off

Page 18: LJC: Microservices in the real world

@chbatey

1 - Don’t take forever

• If at first you don’t succeed, don’t take forever to tell someone

• Timeout and fail fast

Page 19: LJC: Microservices in the real world

@chbatey

Which timeouts?

• Socket connection timeout

• Socket read timeout

Page 20: LJC: Microservices in the real world

@chbatey

Your service hung for 30 seconds :(

Customer

You :(

Page 21: LJC: Microservices in the real world

@chbatey

Which timeouts?

• Socket connection timeout

• Socket read timeout

• Resource acquisition

Page 22: LJC: Microservices in the real world

@chbatey

Your service hung for 10 minutes :(

Page 23: LJC: Microservices in the real world

@chbatey

Let’s think about this

Page 24: LJC: Microservices in the real world

@chbatey

Adding an automated test

• Vagrant - launches + provisions local VMs

• Saboteur - uses tc, iptables to simulate network issues

• Wiremock - used to mock HTTP dependencies

Page 25: LJC: Microservices in the real world

@chbatey

I can write an automated test for that?

Wiremock: •User Service •Device Service •Pin Service

S a b o t e u r

Vagrant + Virtual box

Test meAcceptance

prime to drop traffic

reset

Page 26: LJC: Microservices in the real world

@chbatey

Dependency VM

Wiremock Stubbed Cassandra

192.168.2.2

Page 27: LJC: Microservices in the real world

@chbatey

Wiremock

192.168.2.2

App under test

localhost

Page 28: LJC: Microservices in the real world

@chbatey

HITTING THE SERVICE

PRIMING

Page 29: LJC: Microservices in the real world

@chbatey

DEMO TIME

Page 30: LJC: Microservices in the real world

@chbatey

A little more detail

Page 31: LJC: Microservices in the real world

@chbatey

Wiremock

192.168.2.2

App under test

localhost 192.168.2.1

Delay is here

Page 32: LJC: Microservices in the real world

@chbatey

Wiremock

192.168.2.2

App under test

localhost 192.168.2.1

Delay is here

Page 33: LJC: Microservices in the real world

@chbatey

Wiremock

192.168.2.2

App under test

localhost 192.168.2.1

Delay is here

Page 34: LJC: Microservices in the real world

@chbatey

Wiremock

192.168.2.2

App under test

localhost 192.168.2.1

Delay is here

Page 35: LJC: Microservices in the real world

@chbatey

Implementing reliable timeouts

Page 36: LJC: Microservices in the real world

@chbatey

Implementing reliable timeouts

• Protect the container thread!

• Homemade: Worker Queue + Thread pool (executor)

Page 37: LJC: Microservices in the real world

@chbatey

Implementing reliable timeouts

• Protect the container thread!

• Homemade: Worker Queue + Thread pool (executor)

• Hystrix

• Spring cloud Netflix

Page 38: LJC: Microservices in the real world

@chbatey

A simple Spring RestController

@RestControllerpublic class Resource { private static final Logger LOGGER = LoggerFactory.getLogger(Resource.class); @Autowired private ScaryDependency scaryDependency; @RequestMapping("/scary") public String callTheScaryDependency() { LOGGER.info("Resource later: I wonder which thread I am on!"); return scaryDependency.getScaryString(); }}

Page 39: LJC: Microservices in the real world

@chbatey

Scary dependency

@Componentpublic class ScaryDependency { private static final Logger LOGGER = LoggerFactory.getLogger(ScaryDependency.class); public String getScaryString() { LOGGER.info("Scary Dependency: I wonder which thread I am on! Tomcats?”); if (System.currentTimeMillis() % 2 == 0) { return "Scary String"; } else { Thread.sleep(5000) return “Slow Scary String"; } }}

Page 40: LJC: Microservices in the real world

@chbatey

All on the tomcat thread

13:47:20.200 [http-8080-exec-1] INFO info.batey.examples.Resource - Resource later: I wonder which thread I am on!13:47:20.200 [http-8080-exec-1] INFO info.batey.examples.ScaryDependency - Scary Dependency: I wonder which thread I am on! Tomcats?

Page 41: LJC: Microservices in the real world

@chbatey

Scary dependency@Componentpublic class ScaryDependency { private static final Logger LOGGER = LoggerFactory.getLogger(ScaryDependency.class); @HystrixCommand() public String getScaryString() { LOGGER.info("Scary Dependency: I wonder which thread I am on! Tomcats?”); if (System.currentTimeMillis() % 2 == 0) { return "Scary String"; } else { Thread.sleep(5000) return “Slow Scary String"; } }}

Page 42: LJC: Microservices in the real world

@chbatey

What an annotation can do...

13:51:21.513 [http-8080-exec-1] INFO info.batey.examples.Resource - Resource later: I wonder which thread I am on!13:51:21.614 [hystrix-ScaryDependency-1] INFO info.batey.examples.ScaryDependency - Scary Dependency: I wonder which thread I am on! Tomcats? :P

Page 43: LJC: Microservices in the real world

@chbatey

Async libraries are your friend

• DataStax Java Driver

- Guava ListenableFuture

Page 44: LJC: Microservices in the real world

@chbatey

Drive this via requirements

• Reliable timeout

• Throttling

• Monitoring of failures / successes

Page 45: LJC: Microservices in the real world

@chbatey

Timeouts take home

• You can’t use network level timeouts for SLAs

• Test your SLAs - if someone says you can’t, hit them with a stick

• Scary things happen without network issues

Page 46: LJC: Microservices in the real world

@chbatey

Fault tolerance1.Don’t take forever - Timeouts2.Don’t try if you can’t succeed 3.Don’t whack a dead horse4.Turn broken stuff off

Page 47: LJC: Microservices in the real world

@chbatey

2 - Don’t try if you can’t succeed

Page 48: LJC: Microservices in the real world

@chbatey

Complexity

“When an application grows in complexity it will eventually start sending emails”

Page 49: LJC: Microservices in the real world

@chbatey

Complexity

“When an application grows in complexity it will eventually start using queues and thread pools”

Page 50: LJC: Microservices in the real world

@chbatey

Don’t try if you can’t succeed

Page 51: LJC: Microservices in the real world

@chbatey

Don’t try if you can’t succeed

• Executor Unbounded queues :(

- newFixedThreadPool

- newSingleThreadExecutor

- newThreadCachedThreadPool

• Bound your queues and threads

• Fail quickly when the queue / maxPoolSize is met

• Know your drivers

Page 52: LJC: Microservices in the real world

@chbatey

This is a functional requirement

• Set the timeout very high

• Use Wiremock to add a large delay to the requests

• Set queue size and thread pool size to 1

• Send in 2 requests to use the thread and fill the queue

• What happens on the 3rd request?

Page 53: LJC: Microservices in the real world

@chbatey

Fault tolerance1.Don’t take forever - Timeouts2.Don’t try if you can’t succeed 3.Don’t whack a dead horse4.Turn broken stuff off

Page 54: LJC: Microservices in the real world

@chbatey

5 - Don’t whack a dead horse

Movie Player

User Service

Device Service

Pin Service

Page 55: LJC: Microservices in the real world

@chbatey

What to do…

• Yes this will happen…

• Mandatory dependency - fail *really* fast

• Throttling

• Fallbacks

Page 56: LJC: Microservices in the real world

@chbatey

Circuit breaker pattern

(Picture from Akka docs)

Page 57: LJC: Microservices in the real world

@chbatey

Fault tolerance1.Don’t take forever - Timeouts2.Don’t try if you can’t succeed 3.Don’t whack a dead horse4.Turn broken stuff off

Page 58: LJC: Microservices in the real world

@chbatey

6 - Turn off broken stuff

• The kill switch

Page 59: LJC: Microservices in the real world

@chbatey

To recap

1.Don’t take forever - Timeouts2.Don’t try if you can’t succeed 3.Don’t whack a dead horse4.Turn broken stuff off

Page 60: LJC: Microservices in the real world

@chbatey

Links

• Examples:

- https://github.com/chbatey/spring-cloud-example

- https://github.com/chbatey/dropwizard-hystrix

- https://github.com/chbatey/vagrant-wiremock-saboteur

• Tech:

- https://github.com/Netflix/Hystrix

- https://www.vagrantup.com/

- http://wiremock.org/

- https://github.com/tomakehurst/saboteur

Page 61: LJC: Microservices in the real world

@chbatey

Questions?

Thanks for listening!Questions later? @[email protected]://christopher-batey.blogspot.co.uk/