Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and...

64
1 Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap

Transcript of Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and...

Page 1: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

1

Beyond Microservices:Streams, State and Scalability

Gwen Shapira, Engineering Manager@gwenshap

Page 2: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

22

In the beginning…

Page 3: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

3

We Have Microservices

3

Page 4: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

4

We Microservices

4

Page 5: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

5

They need to communicate

5

Page 6: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

6

6

I know! I’ll use REST APIsOrders

Returns?

Inventory Fulfill Order

Validate Order

Page 7: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

7

Synchronous request-response communicationLeads to

Tight point-to-point coupling

7

Page 8: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

8Clients know too much

Orders

Inventory

Returns

Validate

Fulfill

Page 9: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

9

9

shifts in responsibility, redundancy

Fail

Retry?

Retry?

Retry?

Page 10: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

10

10

Making Changes is Risky

Change

Page 11: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

11

11

Adding Services Requires Explicit CallsMistake Handler

Page 12: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

12

12

REST = HTTP + JSON = SLOW

Page 13: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

1313

We can do better.

Page 14: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

14

Nice to meet you!

● Moving data around for 20 years

● Engineering Manager at Confluent

● Apache Kafka Committer

● Wrote a book or two

● Tweets a lot - @gwenshap

Page 15: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

1515

API Gateway

Page 16: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

16Clients know too much

Orders

Inventory

Returns

Validate

Fulfill

Page 17: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

17Shift in responsibility, redundancy

Auth

Auth

Auth

Page 18: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

18API Gateway

Returns

Inventory

Orders

APIGateway

Page 19: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

1919

API Gateway Responsibility

● Authentication ● Routing

● Rate Limiting ● Logging and analytics

Page 20: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

20

Anti-pattern

Page 21: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

2121

Service Mesh

Page 22: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

22

North-South Traffic

Page 23: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

23

East-West Traffic

Page 24: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

24Side-car

Page 25: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

26

Proxy as sidecar:Proxy

Proxy

Proxy

Proxy

Proxy

Page 26: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

27

Proxy

I have a new IP now. YOLO!

Who cares?

I magically know all about it!

Page 27: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

28

Proxy

I can recover from errors without drowning

Error? No worries! Lets retry every millisecond forever

LOL. I’m dropping 99% of the retries.

Page 28: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

2929

Event Driven

Page 29: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

30

30

Making Changes is Risky

Change

Page 30: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

31

31

Adding Services Requires Explicit CallsMistake Handler

Page 31: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

32

Logic

Logic

Logic

Logic

1. Tell others what to do(commands)

2. Ask questions (queries)Logic

Logic

Logic

Logic

Broadcast what I do

Kafka

Others work out what to do

Queries use local cache

Page 32: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

33

Events are both facts and triggers

Page 33: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

34

Buying an iPad (with REST)- Orders Service calls Shipping

Service to tell it to ship item.

- Shipping service looks up

address to ship to (from

Customer Service)

SubmitOrder

shipOrder() getCustomer()

OrdersService

ShippingService

CustomerService

Webserver

Page 34: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

35

Using events for Notification- Orders Service no longer knows

about the Shipping service (or

any other service). Events are

fire and forget.

SubmitOrder

Order Created

getCustomer()REST

Notification

OrdersService

ShippingService

CustomerService

Webserver

Page 35: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

36

Using events to share facts- Call to Customer service is gone.

- Instead data in replicated, as

events, into the shipping service,

where it is queried locally. . Customer Updated

SubmitOrder

Order Created

Data is replicated

OrdersService

ShippingService

CustomerService

Webserver

Page 36: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

3737

DB for Each Microservice?

● It is safe:They are all derived from same stream of events

● Custom projection just the data each service needs.

● Reduced dependencies

● Low latency

Page 37: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

38

Event Driven Microservices are

Stateful

Page 38: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

39

Page 39: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

4040

Schema

Page 40: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

41

Logic

Logic

Logic

Logic

1. Tell others what to do(commands)

2. Ask questions (queries)Logic

Logic

Logic

Logic

Broadcast what I do

Kafka

Others work out what to do

Queries use local cache

Page 41: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

42

The medium is not the message.

Page 42: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

4343

This is a message{

sessionId: 676fc8983gu563,

timestamp: 1413215458,

viewType: "propertyView",

propertyId: 7879,

loyaltyId: 6764532

origin: "promotion",

...... lots of metadata....

}

Page 43: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

44

44

REST = HTTP + JSON = SLOW

Page 44: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

45

45

Making Changes is Risky

Change

Page 45: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

46

There are lots of dependencies

Bookingservice

{user_id: 53, timestamp: 1497842472}

create table (use_id number,timestamp number)

new Date(timestamp)

Attributionservice

Page 46: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

47

Bookingservice

{user_id: 53, timestamp: “June 28, 2017 4:00pm”}

create table (use_id number,timestamp number)

new Date(timestamp)

Attributionservice

Page 47: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

48

Moving fast and breaking things

Bookingservice

{user_id: 53, timestamp: “June 28, 2017 4:00pm”}

create table (use_id number,timestamp number)

new Date(timestamp)

Attributionservice

Page 48: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

49

APIs between services are ContractsIn Event Driven World – Event Schemas ARE the API

Page 49: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

50

Page 50: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

51

So the flow is…

Dev Nightly build / merge Prod

Test Registry Prod Registry

Test

MVN Plugin

Page 51: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

5252

Serverless

Page 52: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

53

Function as a Service

Event < / >

ResponseVM Function

Instance

< / >

Launch

Page 53: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

54

When nothing happens

Event < / >

ResponseVM Function

Instance

< / >

Launch

Page 54: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

55

At scale

Event < / >

ResponseVM Function

Instance

< / >

Launch

VM Function Instance

< / >

VM Function Instance

< / >

VM Function Instance

< / >

Page 55: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

56

Consume Event

Produce

ContainerFunction Instance

< / >

Wait, this is super familiarKafka Partitions

ContainerOrder Service!

< / >

Page 56: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

5757

Up Next:Stateful Serverless

Page 57: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

58

State is required

● Dynamic Rules

● Event enrichment

● Joining multiple events

● Aggregation

Page 58: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

59

How You Probably Do StateCreate Order < / >

Order CreatedVM Function

Instance

< / >

Launch

Cloud Data Storeselect from

inventory…insert intoOrders…

Page 59: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

60

We can do a bit better

Select order_id, customer_name, product, quantity, price, state from orders where state != “CLOSED”

< / >

VM Function Instance

< / >Cloud Data

Store

What’s the latest with my order?

Maybe check DB?

Order is processingNot shipped yet.

Page 60: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

61But I really want this back:

Page 61: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

62

VM

Create Order < / >

Order Status < / >

Validate Order

< / >

Inventory,Rules

Stateful Serverless

Page 62: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

64

● Durable functions everywhere

● Triggers and data from data stores to functions

What’s Still missing?

● Unified view of current state

Page 63: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

65

Page 64: Beyond Microservices: Streams, State and Scalability · Beyond Microservices: Streams, State and Scalability Gwen Shapira, Engineering Manager @gwenshap. 2 ... Fail Retry? Retry?

66

Resources● http://christophermeiklejohn.com/serverless/2019/05/25/stateful-serverless-bank-example.html

● https://www.infoq.com/articles/service-mesh-promise-peril/

● https://blog.getambassador.io/api-gateway-vs-service-mesh-104c01fa4784

● https://www.nginx.com/blog/building-microservices-using-an-api-gateway/

● https://wecode.wepay.com/posts/migrating-apis-from-rest-to-grpc-at-wepay

● https://content.pivotal.io/slides/microservices-events-and-breaking-the-data-monolith-with-kafka

● https://www.slideshare.net/ConfluentInc/event-sourcing-stream-processing-and-serverless-ben-stopford-confluent-kafka-summit-sf-2019