building event-driven services with apache kafka

68
Building Event-Driven Services with Apache Kafka Ben Stopford @benstopford

Upload: confluent

Post on 05-Apr-2017

343 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Building Event-Driven Services with Apache Kafka

1

Building Event-Driven Services with Apache Kafka

Ben Stopford @benstopford

Page 2: Building Event-Driven Services with Apache Kafka

2

In this talk

1.  How we traditionally build services with REST

2.  How Event Driven Services are different 3.  Building an Immutable, Shared Narrative. 4.  Leveraging Materialized Views. 5.  Relating to CQRS & Event Sourcing 6.  Pulling it all together

Page 3: Building Event-Driven Services with Apache Kafka

3

Companies evolve in different ways

•  Internet companies •  Enterprise companies

Page 4: Building Event-Driven Services with Apache Kafka

4

Internet companies typically grow from front-facing websites.

pic

Growing fast

Page 5: Building Event-Driven Services with Apache Kafka

5

Carve pieces off

Page 6: Building Event-Driven Services with Apache Kafka

6

If stateful, they might share a DB

Page 7: Building Event-Driven Services with Apache Kafka

7

Or they might use their own

Page 8: Building Event-Driven Services with Apache Kafka

8

Over time => more complex

Page 9: Building Event-Driven Services with Apache Kafka

9

Over time => more async (async w.r.t. user)

Page 10: Building Event-Driven Services with Apache Kafka

10

Chained blocking commands

Buffering, handling failure, backpressure, scaling etc all get

pushed into service’s responsibilities

Page 11: Building Event-Driven Services with Apache Kafka

11

The synchronous world of request response protocols leads to tight, point-to-point

couplings.

Page 12: Building Event-Driven Services with Apache Kafka

12

Enterprises are typically different

Page 13: Building Event-Driven Services with Apache Kafka

13

pic

Enterprise companies form as heterogeneous application

conglomerates, often in sylos

Page 14: Building Event-Driven Services with Apache Kafka

14

The start life as independent islands

Page 15: Building Event-Driven Services with Apache Kafka

15

Linked together by files or via messaging

pic

Page 16: Building Event-Driven Services with Apache Kafka

16

Interactions are async from day-1

pic

Page 17: Building Event-Driven Services with Apache Kafka

17

Forwarding of data gets messy: Chinese Whispers / Telephone game

pic

Page 18: Building Event-Driven Services with Apache Kafka

18

Both these approaches often end up at the same point, but for different

reasons

Page 19: Building Event-Driven Services with Apache Kafka

19

Data is herded into specific services

Enterprise

Page 20: Building Event-Driven Services with Apache Kafka

20

Data is herded into specific services

Data Services

Stateless Services

Internet Co

Page 21: Building Event-Driven Services with Apache Kafka

21

Lots of Data Services => Distributed Join Problem

pic

This is a problem databases find hard, and they’re highly tuned for it!

Payments Service

Orders Service

Customers Service

Client Service performs join

Page 22: Building Event-Driven Services with Apache Kafka

22

Lots of Data Services => Distributed Join Problem

pic

TESTING BECOMES DIFFICULT!

Client Service performs join

Payments Service

Orders Service

Customers Service

Page 23: Building Event-Driven Services with Apache Kafka

23

In short: the high degree of “connectedness” makes it hard to evolve independently and to scale

Page 24: Building Event-Driven Services with Apache Kafka

24

LETS TRY

EVENT DRIVEN!

Page 25: Building Event-Driven Services with Apache Kafka

25

Interact through

Events,

Don't talk to

Services

Page 26: Building Event-Driven Services with Apache Kafka

26

This

Payments Service

Purchase Requested Events

Listen & React to Events

Payment Completed Events

Page 27: Building Event-Driven Services with Apache Kafka

27

Not this

Service Payment Service

http://payments/ID/42

Don’t Request/Command from individual services.

Page 28: Building Event-Driven Services with Apache Kafka

28

Let’s take an example...

Page 29: Building Event-Driven Services with Apache Kafka

29

UI Service

Payment Service

Stock Service

(1) Process Payment (2) Contact

Payment Provider

(3) Update stock

(4) Maybe order more

stock

Request Response Example

Page 30: Building Event-Driven Services with Apache Kafka

30

The Event Driven Way

UI Service

Payment Service

Stock Service

Payment Provider

Maybe order more stock

Page 31: Building Event-Driven Services with Apache Kafka

31

UI Service

Payment Service

Stock Service

Payment Provider

Kafka Topics: (1)  Purchases (2)  Payments

Maybe order more stock

The Event Driven Way

Page 32: Building Event-Driven Services with Apache Kafka

32

UI Service

Payment Service

Stock Service

Payment Provider

Maybe order more stock

New Order -> PurchaseRequested Event

Raise PurchaseRequested

Event

Page 33: Building Event-Driven Services with Apache Kafka

33

UI Service

Payment Service

Stock Service

Payment Provider

Maybe order more stock

Payment Service Listens In

Raise PurchaseRequested

Event

Listen to PurchaseRequested Events

Page 34: Building Event-Driven Services with Apache Kafka

34

UI Service

Payment Service

Stock Service

Payment Provider

Maybe order more stock

When Complete -> PaymentProcessed

Raise PurchaseRequested

Event

Listen to PurchaseRequested Events

Raise PaymentProcessed

Event

*Some might use the Command/Query Pattern

Page 35: Building Event-Driven Services with Apache Kafka

35

UI Service

Payment Service

Stock Service

Payment Provider

Maybe order more stock

Stock service hooks in too!

Raise PurchaseRequested

Event

Listen to PurchaseRequested Events

Raise PaymentProcessed

Event

Listen to PaymentProcessed

events

Page 36: Building Event-Driven Services with Apache Kafka

36

UI Service

Payment Service

Stock Service

Payment Provider

Maybe order more stock

Payment service listens

Raise PurchaseRequested

Event

Listen to PurchaseRequested Events

Raise PaymentProcessed

Event

Listen to PaymentProcessed

events

Listen to PaymentProcessed

Event

Page 37: Building Event-Driven Services with Apache Kafka

37

UI Service

Payment Service

Stock Service

Payment Provider

Stock service entirely owns its flow control

Maybe order more stock

Flow Control is Receiver Driven

Page 38: Building Event-Driven Services with Apache Kafka

38

UI Service

Payment Service

Stock Service

Quite different to Synchronous Model

Stock service flow control owned by payments service

Page 39: Building Event-Driven Services with Apache Kafka

39

1. Events are Immutable Facts (state changes taken from the real world and journalled)

UI Service

Purchases

Payments

Purchase Requested

Payment Completed

All the benefits of “Event Sourcing”

Page 40: Building Event-Driven Services with Apache Kafka

40

2. Services couple only to data flows

No knowledge of contributing services (RDFC)

UI Service

Purchases

Payments

Purchase Requested

Payment Completed

Page 41: Building Event-Driven Services with Apache Kafka

41

Because coupling is only to Events, not services

Purchase Requests

PurchaseRequest Topic UI Service

Payment Service

Stock Service

Page 42: Building Event-Driven Services with Apache Kafka

42

The architecture is “Pluggable”

Purchase Requests

PurchaseRequest Topic UI Service

Payment Service

Stock Service

Fraud Service

Page 43: Building Event-Driven Services with Apache Kafka

43

Communication & State Concepts Merge

•  The concept of Data & Events become one.

•  Kafka is both Event Store and Communication Channel

Page 44: Building Event-Driven Services with Apache Kafka

44

The Events form a Canonical, Shared Narrative

Payments

Orders

Shipments Customers

Evolving state of the system over time

Page 45: Building Event-Driven Services with Apache Kafka

45

Scaling is a concern of the broker, not “upstream” services

Purchase Requests

PurchaseRequest Topic UI Service

Payment Service

Stock Service

Fraud Service

Kafka •  Linearly scalable •  Fault Tolerant •  Multi Tennant

Page 46: Building Event-Driven Services with Apache Kafka

46

But there is something missing!

How do we look things up? (i.e. the Q in CQRS) •  What is the address for this customer? •  Is this user allowed to view this stock? •  What is the contents of this user’s shopping basket?

Page 47: Building Event-Driven Services with Apache Kafka

47

With REST Lookups are natural, if Remote

UI Service

Customer Service

http://customers/ID/42

Go to the relevant service and ask!

Page 48: Building Event-Driven Services with Apache Kafka

48

Which creates the dependency problem

Many services tightly coupled to one another

Page 49: Building Event-Driven Services with Apache Kafka

49

With Event Driven we create “views” or “projections” inside each bounded context

Projection of data from Outside

Context Boundary

Shipping Service

Page 50: Building Event-Driven Services with Apache Kafka

50

Pattern 1: Local KTables

Shipping Service

Customers Events

Customers By ID (saved by KStreams

using RocksDB)

Kafka

Page 51: Building Event-Driven Services with Apache Kafka

51

Pattern 1: Local KTables

Shipping Service

Customers Events

Customers By ID KTable customers = builder.table(

CustomerId, Customer, “customers-topic”, “customer-store”);

customers = streams.store("customers-store”…);

customer = customers.get(42)

Page 52: Building Event-Driven Services with Apache Kafka

52

As we’re using a streaming engine, we can translate into any Domain Model

(projection) we wish

Shipping Service

Customers Events

ShippingCustomers By CustomerName

Page 53: Building Event-Driven Services with Apache Kafka

53

Combine different streams from different services

Fraud Service

Payment Events

Purchase Events

PurchasePayments

payments.join(“purchases”,..).groupByKey().reduce(newValueReducer, “PurcahsePaymentsStore”)

Page 54: Building Event-Driven Services with Apache Kafka

54

Pattern 1: Local KTables

•  Simple to create and query •  Local to each service so fast •  Powerful DSL for transformation •  Inbuilt high availability •  No external database •  Controlled entirely within service’s bounded

context •  KTable and GlobalKTable allow scale out

Page 55: Building Event-Driven Services with Apache Kafka

55

Pattern 2(a): Queryable Interface

Fraud Service

Kafka

Fraud Service (Creates streaming projection)

Query via Rest Interface

User Interface

Known as queryable state in Kafka

Context Boundary

Page 56: Building Event-Driven Services with Apache Kafka

56

Pattern 2(b): Query Service

Kafka

Bespoke View Service (Creates streaming

projection)

Rest Interface

Fraud Service

Context Boundary

Page 57: Building Event-Driven Services with Apache Kafka

57

Pattern 2: Exposing Materialised Projections

•  Similar to Pattern 1 but with a “Query Interface” (using Kafka’s Queryable State feature)

•  Commonly used with UI’s

Page 58: Building Event-Driven Services with Apache Kafka

58

Pattern 3: External DB

DB of choice

Kafka Connect

API Fraud Service

Context Boundary

Page 59: Building Event-Driven Services with Apache Kafka

59

Pattern 3: External DB

•  Again similar, but heavier weight •  Use when you cannot pre-compute

the view (i.e. to do ad hoc queries) •  Trick: start with only the data

(fields) you need today. You can always go back for more.

Page 60: Building Event-Driven Services with Apache Kafka

60

Pattern 4: Hybrid

Fraud Service

Kafka

Rest Interface

Context Boundary

Booking Service

NB this breaks the async model, but can be a useful compromise in some cases

Page 61: Building Event-Driven Services with Apache Kafka

61

Pattern 4: Hybrid

•  Quite common in practice •  Allows you to break out of the

async world •  Use sparingly, but do use when

appropriate (e.g. login service)

Page 62: Building Event-Driven Services with Apache Kafka

62

Benefits

•  Forms a central, immutable narrative •  Communication Protocol and Event Store

become one •  Better decoupling (RDFC) •  Excellent scalability •  Several approaches for managing queries,

lightweight => heavy weight •  Event sourcing & CQRS at its core

Page 63: Building Event-Driven Services with Apache Kafka

63

Interact through

Events,

Don't talk to

Services

Page 64: Building Event-Driven Services with Apache Kafka

64

But it's ok to break

the rules!

Page 65: Building Event-Driven Services with Apache Kafka

65

Pulling it all together

Page 66: Building Event-Driven Services with Apache Kafka

66

All order logic Orders-by-Customer view

(KStreams + Queryable State API)

UI Service Stream Maintenance

Fulfillment uses local state store, which is replicated to a

redundant instance

UI uses Orders-by-Customer View directly

via Queryable State

History Service pushes data to a local Elastic Search Instance

Orders Service

Derived View

UI joins data Tables & Streams

Fulfillment Service

History Service

Fully redundant instance

Orders Product Custo-mers KTables

KTables KTables

Schemas

Page 67: Building Event-Driven Services with Apache Kafka

67

Thank You! @benstopford

Page 68: Building Event-Driven Services with Apache Kafka

68

Discount code: kafcom17

Use the Apache Kafka community discount code to get $50 off www.kafka-summit.org Kafka Summit New York: May 8 Kafka Summit San Francisco: August 28

Presented by