iot & microservices… a perfect match · 2019-09-23 · iot real time data. collect store...

51
A PERFECT MATCH IoT & Microservices… @perrynfowler

Upload: others

Post on 06-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

A P E R F E C T M AT C HI o T & M i c r o s e r v i c e s …

@ p e r r y n f o w l e r

Page 2: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

N O W E V E RY B U I L D I N G C A N B E S M A RT

Perryn Fowler is Head of Analytics at Urbanise

Page 3: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

D E M O

Page 4: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

I o TS E N S O R S , F I R M W A R E , A R D U I N O S , H A R D W A R E …

What is IoT - a lot of the time talks on IoT are about Gadgets:- sensors, arduinos, building them, programming them- basically about the T…

Page 5: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

I o TR E A L T I M E D A TA .

C o l l e c t

S t o re

E n r i c h

A n a l y s e

U p d a t e

P re s e n t

But I’m going to talk about the I. Thats about data. real time data.All these gadgets are going to be sending us data of the internet in real time.And we need to collect it, store it, enrich it from other data sources, analyse it, present it and update all of it in real time.

Page 6: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

I o TR E A L T I M E D A TA .

S c a l a b l e & F a u l t To l e r a n t

Oh and you are going to have millions an millions of devices out there, so it needs to be scalable.And this is the internet, so it all also needs to be fault tolerant…

ELEPHANT IN THE ROOM is security

Page 7: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

M I C R O S E R V I C E S R A N TA S H O R T D E T O U R …

💩 💩💩💩💩

💩 💩💩💩

💩

- So how do you a design an architecture for IoT that is all those things?- Well at this point I’m going to take a small detour into Microservices…- And first of allow should talk briefly about what micro services actually are:

Page 8: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

A u t o - a n t o n y m yM I C R O S E R V I C E S … W H A T E V E N A R E T H E Y ?

L i t e r a l l y.

So there is this strange phenomenon in language called Auto-antonymy where over a long periods some words come to mean the exact opposite of their original meaning. Usually this takes a long time.

Page 9: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

A u t o - a n t o n y m yM I C R O S E R V I C E S … W H A T E V E N A R E T H E Y ?

L i t e r a l l y.A g i l e

D e v O p s

For some reason in software development it seems to take just a few short years.

Page 10: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

A u t o - a n t o n y m yM I C R O S E R V I C E S … W H A T E V E N A R E T H E Y ?

S O A

So you find yourself trying to talk about a concept that has somehow come to mean “put a lot of spaghetti logic inside some kind of propriety magic Enterprise Service Bus… whats a “thought leader” to do?

Page 11: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

A u t o - a n t o n y m yM I C R O S E R V I C E S … W H A T E V E N A R E T H E Y ?

M i c ro s e r v i c e s

Need to come up with a new word… an the whole process starts again.

So Microservice Architecture is basically Service Oriented Architecture with a new catchy name - and perhaps with slightly more emphasis on small services.But that doesn’t help much since SOA has lost its meaning

Page 12: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

S M A L L , W E L L D E F I N E D & I N D E P E N D E N T

M I C R O S E R V I C E S A R E :

So when I say ‘Microservices’ I mean services that are both RELATIVELY small and independent.

- NOT TOO SMALL - I’ve heard some horror stories about people making 500 micro services to build a website. Don’t go crazy. Maybe more DUPLO than LEGO.

Page 13: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

I N D E P E N D E N T LY…T H I S M E A N S M I C R O S E R V I C E S C A N B E :

Developed

- hold what they do and how they work in your head- choose the best technology for that specific job, instead of ‘one size fits all’.- if worst comes to worst you can throw them away and rewrite them

So the theory goes that because micro services are small and well defined. They can be developed independently. They are small & independent enough that you can hold what they do and how they work in your head - you don’t need to worry so much that you have broken some other piece of itThey are small & independent enough that you can choose the best technology for that specific job, instead of ‘one size fits all’.Also, they are small & independent enough that if worst comes to worst you can throw them away and rewrite them. This is great if you want to experiment with new tech without fully committing to it.

Page 14: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

I N D E P E N D E N T LY…T H I S M E A N S M I C R O S E R V I C E S C A N B E :

Developed

Deployed

-The theory also goes that if you have these services that are small and well defined, you can deploy them independently of each other. -You don’t have this huge ball of mud that you need to test end-to-end before you can change it. - - And if it breaks you need to roll-back the whole shebang- You can deploy this small isolated change, and if it breaks, just roll that back.

Page 15: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

I N D E P E N D E N T LY…T H I S M E A N S M I C R O S E R V I C E S C A N B E :

Developed

Deployed

Scaled

-The theory also goes that when we are terribly successful and a part of our system need to handle more load- We can scale just that part, instead of the whole shebang- And if we know which parts will need to be especially scalable, we can focus effort on making just those parts scalable.

Page 16: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

I N D E P E N D E N T LY…T H I S M E A N S M I C R O S E R V I C E S A R E :

Developed

Deployed

Scaled

Available

Lastly, if our services are nicely independent then if one of them goes down it doesn’t bring the whole system to a screaming halt.

There are probably a lot of other attributes that define micro services, but these will do for now…

Page 17: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

E A S I E R T O O P E R AT E ? ?T H I S M E A N S M I C R O S E R V I C E S A R E :

Developed

Deployed

Scaled

Available

- So an interesting observation about these is that a lot of this is about making the system easier to OPERATE. Which is interesting because there seems to be a strong perception that micro services are harder to operate.- In fact, I’d say that for a lot of so called “microservice” architectures out there, do actually make some or all of these things harder. Harder to develop. Harder to Deploy. Harder to scale etc etc

Page 18: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

L I B R A RY S Y S T E ME X A M P L E

- So how does this happen?- well lets walk through an exampleGuess where I was when I did these slides?

Page 19: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

L I B R A RY S Y S T E ME X A M P L E

Catalogue Loan Registry

Book

Loan

Member

So we decide to have 2 services for our system- The catalogue system records all the books the library has, makes them searchable etc etc- The Loan Registry allows people to take out loans, do returns, renewals, send out overdue reminders etc etc

Page 20: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

L I B R A RY S Y S T E ME X A M P L E

Catalogue Loan Registry

Book

Loan

Member

This seems like a reasonable decomposition because these are quite different functions.Search vs registry. You don’t need to be a member to search the catalogue but you do need to in order to borrow etc. Catalogue doesn’t change as much as the loan registry - and can be cached out the wazoo. Maybe not really ‘small’ enough, but ok for this example…

Page 21: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

T R O U B L E B R E W I N G …E X A M P L E

Catalogue Loan Registry

Book

Loan

Member

AP I

So when we come to implement it, we find that when you come to take out a loan the registry wants to know some information about the thing that is being loaned…So we build an API and let the Loan Registry query the catalogue

Page 22: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

T R O U B L E B R E W I N G …E X A M P L E

Catalogue Loan Registry

Book

Loan

Member

AP I

AP I

Then, someone comes up with a new requirement…“Wouldn’t it be nice” Someone says “If when you looked up a book in the catalogue it could tell you if any copies of that book was currently available.- So we build another API and make another call…

Page 23: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

R U H R O HE X A M P L E

Page 24: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

AVA I L A B L E ?I N D E P E N D E N T LY …

Catalogue Loan Registry

Book

Loan

Member

AP I

AP I

- Not really, because each service depends on the other…

Page 25: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

S C A L A B L E ?I N D E P E N D E N T LY …

Catalogue Loan Registry

Book

Loan

Member

AP I

AP I

- Not really, because now traffic to one service generates traffic on the other…

Page 26: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

D E P L O YA B L E ?I N D E P E N D E N T LY …

Catalogue Loan Registry

Book

Loan

Member

AP I

AP I

Suddenly…- Not really independently deployable, because unless you make absolutely sure versions of those APIs are always backwards compatible, you are going to have to deploy these things in the right order something is going to break.

Page 27: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

D E V E L O P E D ?I N D E P E N D E N T LY & E A S I LY …

Catalogue Loan Registry

Book

Loan

Member

AP I

AP I

So with enough effort you can address some of those assertions I just made:- You can build in circuit breakers to address availability- You can put caches in to limit load impact ( remember that cache invalidation is one of the 2 hard things in CS)- You can make carefully design your API semantics to make them backwards compatible as often as humanly possible. REST will help here to some extent.- Not really the nice simple development experience we were promised either.

Page 28: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

W H AT W E N T W R O N G ?

Page 29: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

W R O N G S E R V I C E B O U N D A RY !

Page 30: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

W R O N G S E R V I C E B O U N D A RY !

Catalogue Loan Registry

Book

Loan

Member

AP I

AP I

Book and Loan are too closely coupled to put in different services.“So what concepts in my domain are a candidate for separating?”Well, you probably shouldn’t break up domain concepts like that, you need a different type of‘Bounded Context’, have you read Domain Driven Design?

Page 31: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

A G I L E A R C H I T E C T U R E ?

- but it seemed like a reasonable service decomposition when we started.- I am made uneasy by any architectural approach that allows means I can paint myself into a corner if I don’t make exactly the right decisions up front- So does this mean I think a micro service architecture is just a bad idea? well I wouldn't say that

Page 32: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

“ I f y o u r ‘ m i c ro s e r v i c e s ’ m a k e h t t p c a l l s t o e a c h o t h e r y o u a c t u a l l y j u s t h a v e a d i s t r i b u t e d m o n o l i t h . ” - @ p e r r y n f o w l e r

H O W T O M A K E E V E R Y T H I N G W O R S E …

💩 💩💩 💩

💩 💩💩💩

💩

Here’s a controversial statement I like to make. Its under 140 characters hint hint.

or if I'm feeling particularly mean “congratulations you have just reinvented Enterprise Java Beans!”

So is there another way to do this micro service stuff?

Page 33: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

A S Y N C H R O N O U S E V E N T SS O W H A T I N S T E A D ?

Catalogue Loan Registry

Book Loan

Member

Book

Event Stream

So how about instead of making synchronous we calls, we publish an event.The catalogue can publish an event when it is updated and the registry can subscribe to that event and update any local information it needs.

Page 34: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

A S Y N C H R O N O U S E V E N T SS O W H A T I N S T E A D ?

Catalogue Loan Registry

Book Loan

Member

Book

Event Stream

And the loan registry can publish an event when a book is borrowed and the catalogue can update its availability count

Page 35: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

I D E M P O T E N T P R O C E S S I N GS O W H A T I N S T E A D ?

Catalogue Loan Registry

Book Loan

Member

Book

Events

And what if we design our processing so that its idempotent. That is you can process the same event multiple times and it will have the same effects once. So for example, if we process the same loan event twice, we don’t decrement the availability counter twice.

Page 36: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

I N D E P E N D E N T LY…G O A L S …

Developed

Deployed

Scaled

Available

- Available: if none service stops processing events for a while it has no effect on the other- Scaled: Extra traffic on one service does not generate traffic on another. Except maybe for more events to process. Much more resilient to BURSTY traffic - just work

through the backlog. And if your events are shardable - just scale the streams by adding more.- Deployed: you can deploy these things independently. If worse comes to worse and event processing starts failing. nothing goes down for a bit, just gets out of date

and you cane re-process later.- Developed. You only need to worry about other services when you are making changes to the events you publish. Also don’t need to know about all consumers - e.g.

could add an email service listening to loan events.

Page 37: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

D u p l i c a t i o n ?B U T B U T B U T …

So something that worries people about this approach is that I seem to be duplicating data everywhere - I know have Book in both services. My advice is don’t worry about it - disk space is cheap and whats more you’ll probably find that its not really duplication. Each place you store this data has a different use and is likely to have a different shape if its optimised for that use - and you sendup with something that is actually BETTER than the God model that tries to be all things to everyone.

Page 38: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

C o n s i s t e n c y ?B U T B U T B U T …

Unless your building a high volume trading system or something - this is probably much less of a problem than you think…Eventually Consistent is fine.

Page 39: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

L O G B A S E D A R C H I T E C T U R ED U R A B L E E V E N T S T R E A M S , M U LT I P L E I D E M P O T E N T C O N S U M E R S

So this sort of approach where you have DURABLE EVENTS STREAMS - by durable I mean the event sticks around in the stream for a while. Doesn’t have to be forever, just long enough for you to be able to replay, say, the last hours events.and MULTIPLE CONSUMERS is known (at least in some circles) as LOG BASED ARCHITECTURE. (because if you think about it a simple way to implement this is to write the events to a log file and have each consumer tailing it)and IMNSHO is the only way to build a MS architecture.

Page 40: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

I o TR E A L T I M E D A TA .

C o l l e c t

S t o re

E n r i c h

A n a l y s e

U p d a t e

P re s e n t

Ok, so thats my micro services rant - what was I talking about?

Oh yea - IoT - what has this all got to do with the I in Iot and how we can deal with it?

Well I glossed over how much commitment you need to use this approach. You need to decide to publish events and what events you are going to publish.

Page 41: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

R E A D Y M A D E E V E N T SI O T

well for a LOG based architecture you need to publish events, but with IoT devices we already have that. Each device is out there sending us nice independent little packets of information, so LOG based architecture fits really well, and we can get all those benefits we talked about

If you think about it a lot of mobile app interactions are events too..

Page 42: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

S C A L A B L ED E C O U P L E D

- processing decoupled from collection ( so if processing gets slow…)So everyone talks about the coming explosion of IoT devices - so you want to make sure you can scale with that. LOG BASED arch means that your ability to accept data is decoupled from your ability to process it.…

Page 43: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

S C A L A B L ES H A R D A B L E

- data from devices is usually very shardable into seperate streamsSo everyone talks about the coming explosion of IoT devices - so you want to make sure you can scale with that. LOG BASED arch means that your ability to accept data is decoupled from your ability to process it. Also events from individual devices, phones etc are immediately shareable. You can create a stream per device if you need to…

Page 44: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

FA U LT T O L E R A N TD U R A B L E S T R E A M S , I D E M P O T E N T P R O C E S S O R S

- If the processor goes down, we don't stop accepting data- The processor develops a transient fault, it can just stop for a while try again later- If we break the processor we can fix it and replay the events..

Page 45: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

F L E X I B L EM U LT I P L E I N D E P E N D E N T C O N S U M E R S

Theres all sorts of direct tech out there that might help, and its hard to choose the one that will best suit your requirements - well you can try them all if you want. And you can try it with your data.

Or if you are writing your own stuff, you can choose different languages for different problems. 9ruby, clojure, R whatever)

Page 46: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

F L E X I B L ET R A N S F O R M A N D R E P U B L I S H

Page 47: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

N O W E V E RY B U I L D I N G C A N B E S M A RT

So what do we actually do at Urbanise

Page 48: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

U R B A N I S E

- we use kinesis for event streams. (another option is Apache Kafka)- we use a number of different technologies for implementing our services- and at the moment we’re using either dynamodb or postgres for implementing persistence(whichever suits the problem best)

Page 49: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

U R B A N I S E

Page 50: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

A N D T H E N ?

- Kinesis Analytics (not even on slide)- Machine Learning!

Page 51: IoT & Microservices… A PERFECT MATCH · 2019-09-23 · IoT REAL TIME DATA. Collect Store Enrich Analyse Update Present But I’m going to talk about the I. Thats about data. real

@ p e r r y n f o w l e rQ U E S T I O N S ?

REMEMBER TO REPEAT QUESTION