70
A SOCIAL NETWORK ON STREAMS DRIVETRIBE ENGINEERING

A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

A SOCIAL NETWORK ON STREAMS

DRIVETRIBE ENGINEERING

Page 2: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

DRIVETRIBE

▸ The world biggest motoring community.

▸ A social platform for petrolheads.

▸ By Clarkson, Hammond and May.

2

Page 3: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

DRIVETRIBE

▸ A content destination at the core.

▸ Users consume feeds of content: images, videos, long-form articles.

▸ Content is organised in homogenous categories called “tribes”.

▸ Different users have different interests and the tribe model allows to mix and match at will.

3

Page 4: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

DRIVETRIBE ARTICLE

▸ Single article by James May.

▸ Contains a plethora of content and engagement information.

▸ What do we need to compute an aggregate like this?

4

Page 5: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

DRIVETRIBE ARTICLE

▸ getUser(id: Id[User])

5

Page 6: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

DRIVETRIBE ARTICLE

▸ getUser(id: Id[User])

▸ getTribe(id: Id[Tribe])

6

Page 7: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

DRIVETRIBE ARTICLE

▸ getUser(id: Id[User])

▸ getTribe(id: Id[Tribe])

▸ getArticle(id: Id[Article])

7

Page 8: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

DRIVETRIBE ARTICLE

▸ getUser(id: Id[User])

▸ getTribe(id: Id[Tribe])

▸ getArticle(id: Id[Article])

▸ countViews(id: Id[Article])

8

Page 9: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

DRIVETRIBE ARTICLE

▸ getUser(id: Id[User])

▸ getTribe(id: Id[Tribe])

▸ getArticle(id: Id[Article])

▸ countViews(id: Id[Article])

▸ countComments(id: Id[Article])

9

Page 10: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

DRIVETRIBE ARTICLE

▸ getUser(id: Id[User])

▸ getTribe(id: Id[Tribe])

▸ getArticle(id: Id[Article])

▸ countViews(id: Id[Article])

▸ countComments(id: Id[Article])

▸ countBumps(id: Id[Article])

10

Page 11: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

DRIVETRIBE FEED OF ARTICLES

▸ rankArticles(forUserId).flatMap { a => … }

▸ getUser(id: Id[User])

▸ getTribe(id: Id[Tribe])

▸ getArticle(id: Id[Article])

▸ countViews(id: Id[Article])

▸ …

11

Page 12: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

QUINTESSENTIAL PREREQUISITES

▸ Scalable. Jeremy Clarkson has 7.2M Twitter followers. Cannot really hack it and worry about it later.

▸ Performant. Low latency is key and mobile networks add quite a bit of it.

▸ Flexible. Almost nobody gets it right the first time around. The ability to iterate is paramount.

▸ Maintainable. Spaghetti code works like interest on debt.

12

Page 13: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

THREE TIER APPROACH

▸ Clients interact with a fleet of stateless servers (aka “API” servers or “Backend”) via HTTP (which is stateless).

▸ Global shared mutable state (aka the Database).

▸ Starting simple: Store data in a DB.

▸ Starting simple: Compute the aggregated views on the fly.

13

Page 14: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

DRIVETRIBE ARTICLE

▸ getUser(id: Id[User])

▸ getTribe(id: Id[Tribe])

▸ getArticle(id: Id[Article])

▸ countComments(id: Id[Article])

▸ countBumps(id: Id[Article])

▸ countViews(id: Id[Article])

14

Page 15: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

READ TIME AGGREGATION

▸ (6 queries per Item) x (Y items per page)

▸ Cost of ranking and personalisation.

▸ Quite some work at read time.

▸ Slow. Not really Performant.

15

Page 16: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

WRITE TIME AGGREGATION

▸ Compute the aggregation at write time.

▸ Then a single query can fetch all the views at once. That scales.

16

Page 17: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

WRITE TIME AGGREGATION

▸ Compute the aggregation at write time.

▸ Then a single query can fetch all the views at once. That scales.

17

Page 18: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

WRITE TIME AGGREGATION

▸ Compute the aggregation at write time.

▸ Then a single query can fetch all the views at once. That scales.

18

Page 19: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

WRITE TIME AGGREGATION

▸ Compute the aggregation at write time.

▸ Then a single query can fetch all the views at once. That scales.

19

Page 20: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

WRITE TIME AGGREGATION - EVOLUTION

▸ sendNotification

20

Page 21: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

WRITE TIME AGGREGATION - EVOLUTION

▸ sendNotification

▸ updateUserStats

21

Page 22: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

WRITE TIME AGGREGATION - EVOLUTION

▸ sendNotification

▸ updateUserStats.

▸ What if we have a cache?

▸ Or a different database for search?

22

Page 23: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

WRITE TIME AGGREGATION

▸ A simple user action is triggering a potentially endless sequence of side effects.

▸ Most of which need network IO.

▸ Many of which can fail.

23

Page 24: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

ATOMICITY

▸ What happens if one of them fails? What happens if the server fails in the middle?

▸ We may have transaction support in the DB, but what about external systems?

▸ Inconsistent.

24

Atomicity?

Page 25: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

CONCURRENCY

▸ Concurrent mutations on a global shared state entail race conditions.

▸ State mutations are destructive and can not be (easily) undone.

▸ A bug can corrupt the data permanently.

25

Concurrency?

Page 26: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

ITALIAN PASTA

▸ Model evolution becomes difficult. Reads and writes are tightly coupled.

▸ Migrations are scary.

▸ This is neither Extensible nor Maintainable.

26

Extensibility?

Page 27: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

DIFFERENT APPROACH

▸ Let’s take a step back and try to decouple things.

▸ Clients send events to the API: “John liked Jeremy’s post”, “Maria updated her profile”

▸ Events are immutable. They capture a user action at some point in time.

▸ Every application state instance can be modelled as a projection of those events.

27

Page 28: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

▸ Persisting those yields an append-only log of events.

▸ An event reducer can then produce application state instances.

▸ Even retroactively. The log is immutable.

▸ This is event sourcing.

28

Page 29: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

▸ The write-time model (command model) and the read time model (query model) can be separated.

▸ Decoupling the two models opens the door to more efficient, custom implementations.

▸ This is known as Command Query Responsibility Segregation aka CQRS.

29

Page 30: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING APPROACH

30

Like!!

Page 31: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING APPROACH

31

Store Like Event

Like!!

Page 32: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING APPROACH

32

Store Like Event ArticleStatsReducer

Like!!

Page 33: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING APPROACH

33

Store Like Event ArticleStatsReducerNotificationReducer

Like!!

Page 34: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING APPROACH

34

Store Like EventArticleStatsReducer

NotificationReducer

UserStatsReducer

Like!!

Page 35: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING APPROACH

35

Store Like EventArticleStatsReducer

NotificationReducer

UserStatsReducer

And so on..

Like!!

Page 36: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING APPROACH

36

Store Like EventArticleStatsReducer

NotificationReducer

UserStatsReducer

Sky Is the limitGet Articles

Like!!

Page 37: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING APPROACH

37

Store Like EventArticleStatsReducer

NotificationReducer

UserStatsReducer

Sky Is the limitGet Articles

Like!! Extensibility?Maintainability?

Page 38: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING APPROACH

38

Store Like EventArticleStatsReducer

NotificationReducer

UserStatsReducer

Sky Is the limitGet Articles

Like!!

Performance?

Page 39: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING APPROACH

39

Store Like EventArticleStatsReducer

NotificationReducer

UserStatsReducer

Sky Is the limitGet Articles

Like!! Atomicity?

Page 40: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING APPROACH

40

Store Like EventArticleStatsReducer

NotificationReducer

UserStatsReducer

Sky Is the limitGet Articles

Like!!

Concurrency?

Page 41: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

IMPLEMENTATION?

Page 42: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

WE NEED A LOG

Page 43: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

APACHE KAFKA

▸ Distributed, fault-tolerant, durable and fast append-only log.

▸ Can scale the thousands of nodes, producers and consumers.

▸ Each business event type can be stored in its own topic.

43

Page 44: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

WE NEED A STREAM PROCESSOR

Page 45: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

APACHE FLINK

▸ Scalable, performant, mature.

▸ Elegant high level APIs in Scala.

▸ Powerful low level APIs for advanced tuning.

▸ Multiple battle-tested integrations.

▸ Very nice and active community.

45

Page 46: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

WE NEED DATASTORE

Page 47: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

ELASTICSEARCH

▸ Horizontally scalable document store.

▸ Rich and expressive query language.

▸ Dispensable. Can be replaced.

47

Page 48: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

WE NEED AN API

Page 49: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

AKKA HTTP

▸ Asynchronous web application framework.

▸ Written in Scala.

▸ Very expressive routing DSL.

▸ Any modern web application framework would do.

49

Page 50: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING IN PRACTICE

50

Store Raw events Consume raw events

Produce aggregated views

Retrieve aggregated views

Page 51: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING IN PRACTICE

51

Store Raw events Consume raw events

Produce aggregated views

Retrieve aggregated views

Stateful

Page 52: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING IN PRACTICE

52

Store Raw events Consume raw events

Produce aggregated views

Retrieve aggregated views

Page 53: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

BLUE/GREEN APPROACH

53

MIRROR

Page 54: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

A REAL WORLD EXAMPLE

Page 55: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

COUNTING BUMPS

▸ Thousands of people like the fact that Jeremy Clarkson is a really tall guy

▸ Users can “bump” a post if they like it

55

Page 56: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

EVENT SOURCING IN PRACTICE

56

Store Raw events Consume raw events

Produce aggregated views

Retrieve aggregated views

Page 57: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

COUNTING BUMPS

57

Page 58: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

COUNTING BUMPS - FIRST ATTEMPT

58

Page 59: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

COUNTING BUMPS - FIRST ATTEMPT

59

Page 60: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

COUNTING BUMPS - FIRST ATTEMPT

60

▸ Use Flink with at least once semantics

Page 61: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

COUNTING BUMPS - FIRST ATTEMPT

61

▸ Use Flink with at least once semantics

▸ Our system is eventually consistent

Page 62: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

WHAT DO WE KNOW ABOUT OUT COUNTER?

62

▸ we know we’re doing some kind of combine operation over States

Page 63: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE 63

▸ we know we’re doing some kind of combine operation over States

▸ we want our counter to be idempotent: a |+| a === a

WHAT DO WE KNOW ABOUT OUT COUNTER?

Page 64: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE 64

▸ we know we’re doing some kind of combine operation over States

▸ we want our counter to be idempotent: a |+| a === a

▸ we also want our counter to be associative: a |+| (b |+| c) === (a |+| b) |+| c

WHAT DO WE KNOW ABOUT OUT COUNTER?

Page 65: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

BAND ALGEBRA

65

▸ Closed

▸ Idempotent

▸ Associative

Page 66: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

COUNTING BUMPS - SECOND ATTEMPT

66

Page 67: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

COUNTING BUMPS - SECOND ATTEMPT

67

▸ if all components of a case class have a band then so does the case class

▸ would normally bring in Set implementation from a library

▸ normally that library would have a law testing module

Page 68: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

COUNTING BUMPS - PUTTING IT TOGETHER

68

Page 69: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

DRIVETRIBE

OTHER ALGEBRAS WE USE

69

▸ Adding events: Semigroup/Monoid

▸ Duplicate events: Band

▸ Out of order and duplicate events: Semilattice

Page 70: A SOCIAL NETWORK ON STREAMSThis is event sourcing. 28. DRIVETRIBE ... This is known as Command Query Responsibility Segregation aka CQRS. 29. DRIVETRIBE EVENT SOURCING APPROACH 30

FIN