+ All Categories
Home > Software > Event Sourcing and Functional Programming

Event Sourcing and Functional Programming

Date post: 23-Feb-2017
Category:
Upload: globallogic-ukraine
View: 777 times
Download: 1 times
Share this document with a friend
195
Functional Programming & Event Sourcing A pair made in heaven twitter: @rabbitonweb, email: [email protected]
Transcript
Page 1: Event Sourcing and Functional Programming

Functional Programming & Event Sourcing

A pair made in heaven

twitter: @rabbitonweb, email: [email protected]

Page 2: Event Sourcing and Functional Programming

Functional Programming

Page 3: Event Sourcing and Functional Programming

● no assignment statements

Page 4: Event Sourcing and Functional Programming

● no assignment statements● no variables

Page 5: Event Sourcing and Functional Programming

● no assignment statements● no variables ● once given a value, never

change

Page 6: Event Sourcing and Functional Programming

● no assignment statements● no variables ● once given a value, never

change● no side-effects at all

Page 7: Event Sourcing and Functional Programming

● no assignment statements● no variables ● once given a value, never

change● no side-effects at all

Page 8: Event Sourcing and Functional Programming

“The functional programmer sounds rather like a mediæval monk,

Page 9: Event Sourcing and Functional Programming

“The functional programmer sounds rather like a mediæval monk, denying himself the pleasures of life

Page 10: Event Sourcing and Functional Programming

“The functional programmer sounds rather like a mediæval monk, denying himself the pleasures of life in the hope that it will make him virtuous.”

Page 11: Event Sourcing and Functional Programming

Functional Programming

Page 12: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

Page 13: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache { def check(id: Long): Option[User] = ??? }

Page 14: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache { def check(id: Long): Option[User] = ??? }

case class UserRepo(cache: Cache) {

def retrieve(id: Long): User = ???

}

Page 15: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache { def check(id: Long): Option[User] = ??? }

case class UserRepo(cache: Cache) {

def retrieve(id: Long): User = ???

}

class UserFinder(cache: Cache, repo: UserRepo) {

}

Page 16: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache { def check(id: Long): Option[User] = ??? }

case class UserRepo(cache: Cache) {

def retrieve(id: Long): User = ???

}

class UserFinder(cache: Cache, repo: UserRepo) {

def findUser(id: Long): User = {

}

}

Page 17: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache { def check(id: Long): Option[User] = ??? }

case class UserRepo(cache: Cache) {

def retrieve(id: Long): User = ???

}

class UserFinder(cache: Cache, repo: UserRepo) {

def findUser(id: Long): User = {

val maybeUser: Option[User] = cache.check(id)

}

}

Page 18: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache { def check(id: Long): Option[User] = ??? }

case class UserRepo(cache: Cache) {

def retrieve(id: Long): User = ???

}

class UserFinder(cache: Cache, repo: UserRepo) {

def findUser(id: Long): User = {

val maybeUser: Option[User] = cache.check(id)

if (maybeUser.isDefined) {

maybeUser.get

}

}

}

Page 19: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache { def check(id: Long): Option[User] = ??? }

case class UserRepo(cache: Cache) {

def retrieve(id: Long): User = ???

}

class UserFinder(cache: Cache, repo: UserRepo) {

def findUser(id: Long): User = {

val maybeUser: Option[User] = cache.check(id)

if (maybeUser.isDefined) {

maybeUser.get

} else {

val user: User = repo.retrieve(id)

cache.insert(user.id, user)

}

}

}

Page 20: Event Sourcing and Functional Programming

No concept of ‘time’

Page 21: Event Sourcing and Functional Programming

f(input): output

Page 22: Event Sourcing and Functional Programming

input f(input): output

Page 23: Event Sourcing and Functional Programming

input f(input): output output

Page 24: Event Sourcing and Functional Programming

input f(input): output output g(input): output

Page 25: Event Sourcing and Functional Programming

input f(input): output output g(input): output output

Page 26: Event Sourcing and Functional Programming

input f(input): output output g(input): output output

Page 27: Event Sourcing and Functional Programming

h = g o f

input f(input): output output g(input): output output

Page 28: Event Sourcing and Functional Programming

Modularity & composition

Page 29: Event Sourcing and Functional Programming

h = g o f

input f(input): output output g(input): output output

Page 30: Event Sourcing and Functional Programming

/** * This function returns a reversed list * @param list A list to be reversed * @return A reversed list */public List<T> reverse(List<t> list) { ??? }

Page 31: Event Sourcing and Functional Programming
Page 32: Event Sourcing and Functional Programming

/** * This function returns a reversed list * @param list A list to be reversed * @return A reversed list */public List<T> reverse(List<t> list) { ??? }

Page 33: Event Sourcing and Functional Programming

/** * This function returns a reversed list * @param list A list to be reversedpublic List<T> reverse(List<t> list) { ??? }

Page 34: Event Sourcing and Functional Programming

/** * This function returns a reversed listpublic List<T> reverse(List<t> list) { ??? }

Page 35: Event Sourcing and Functional Programming

/**public List<T> reverse(List<t> list) { ??? }

Page 36: Event Sourcing and Functional Programming

public List<T> reverse(List<t> list) { ??? }

Page 37: Event Sourcing and Functional Programming

public List<T> reverse(List<t> list) { return list.sort();}

Page 38: Event Sourcing and Functional Programming

def smdfknmsdfp[A](a: A): A = ???

Page 39: Event Sourcing and Functional Programming

def smdfknmsdfp[A](a: A): A = a

Page 40: Event Sourcing and Functional Programming

def identity[A](a: A): A = a

Page 41: Event Sourcing and Functional Programming

def smdfknmsdfp[A](a: A): A = a

Page 42: Event Sourcing and Functional Programming

def smdfknmsdfp(a: Int): Int = ???

Page 43: Event Sourcing and Functional Programming

def smdfknmsdfp(a: Int): Int = a = a + 10 = 10

Page 44: Event Sourcing and Functional Programming

“Why Functional Programming Matters”J. Hughes

http://comjnl.oxfordjournals.org/content/32/2/98.full.pdf

Page 45: Event Sourcing and Functional Programming

“Why Functional Programming Matters”J. Hughes, Nov. 1988

http://comjnl.oxfordjournals.org/content/32/2/98.full.pdf

Page 46: Event Sourcing and Functional Programming

Soft introduction

“Why Functional Programming Matters”J. Hughes, Nov. 1988

http://comjnl.oxfordjournals.org/content/32/2/98.full.pdf

Page 47: Event Sourcing and Functional Programming

“Program Design by Calculation”J.N. Oliveira

http://www4.di.uminho.pt/~jno/ps/pdbc_part.pdf

Page 48: Event Sourcing and Functional Programming

“Program Design by Calculation”J.N. Oliveira, Draft

http://www4.di.uminho.pt/~jno/ps/pdbc_part.pdf

Page 49: Event Sourcing and Functional Programming

Patterns in FP World

“Program Design by Calculation”J.N. Oliveira, Draft

http://www4.di.uminho.pt/~jno/ps/pdbc_part.pdf

Page 50: Event Sourcing and Functional Programming

Patterns in FP World Math Matters

“Program Design by Calculation”J.N. Oliveira, Draft

http://www4.di.uminho.pt/~jno/ps/pdbc_part.pdf

Page 52: Event Sourcing and Functional Programming

Functional Programming

Page 53: Event Sourcing and Functional Programming

Functional Programming

Page 54: Event Sourcing and Functional Programming
Page 55: Event Sourcing and Functional Programming
Page 56: Event Sourcing and Functional Programming
Page 57: Event Sourcing and Functional Programming

How to do something useful?

Page 58: Event Sourcing and Functional Programming

How to do something useful?

Page 59: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache { def check(id: Long): Option[User] = ??? }

case class UserRepo(cache: Cache) {

def retrieve(id: Long): User = ???

}

class UserFinder(cache: Cache, repo: UserRepo) {

def findUser(id: Long): User = {

val maybeUser: Option[User] = cache.check(id)

if (maybeUser.isDefined) {

maybeUser.get

} else {

val user: User = repo.retrieve(id)

cache.insert(user.id, user)

}

}

}

Page 60: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

Page 61: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache {}

Page 62: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache {}

def check(id: Long)(cache: Cache): (Cache, Option[User]) = ...

Page 63: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache {}

def check(id: Long)(cache: Cache): (Cache, Option[User]) = ...

def retrieve(id: Long)(cache: Cache): (Cache, User) = ...

Page 64: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache {}

def check(id: Long)(cache: Cache): (Cache, Option[User]) = ...

def retrieve(id: Long)(cache: Cache): (Cache, User) = ...

def findUser(id: Long)(cache: Cache): (Cache, User) = {

}

}

Page 65: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache {}

def check(id: Long)(cache: Cache): (Cache, Option[User]) = ...

def retrieve(id: Long)(cache: Cache): (Cache, User) = ...

def findUser(id: Long)(cache: Cache): (Cache, User) = {

val (c, mu) = check(id)(cache)

}

}

Page 66: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache {}

def check(id: Long)(cache: Cache): (Cache, Option[User]) = ...

def retrieve(id: Long)(cache: Cache): (Cache, User) = ...

def findUser(id: Long)(cache: Cache): (Cache, User) = {

val (c, mu) = check(id)(cache)

mu match {

case Some(u) => (c, u)

}

}

}

Page 67: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache {}

def check(id: Long)(cache: Cache): (Cache, Option[User]) = ...

def retrieve(id: Long)(cache: Cache): (Cache, User) = ...

def findUser(id: Long)(cache: Cache): (Cache, User) = {

val (c, mu) = check(id)(cache)

mu match {

case Some(u) => (c, u)

case None => retrieve(id)(c)

}

}

}

Page 68: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache {}

def check(id: Long)(cache: Cache): (Cache, Option[User]) = ...

def retrieve(id: Long)(cache: Cache): (Cache, User) = ...

def findUser(id: Long)(cache: Cache): (Cache, User) = {

val (c, mu) = check(id)(cache)

mu match {

case Some(u) => (c, u)

case None => retrieve(id)(c)

}

}

}

Page 69: Event Sourcing and Functional Programming

S => (S, A)

Page 70: Event Sourcing and Functional Programming

State[S, A]S => (S, A)

Page 71: Event Sourcing and Functional Programming

State[S, A]S => (S, A)

.run(S)

Page 72: Event Sourcing and Functional Programming

State[S, A]S => (S, A)

.map(A => B): State[S, B]

Page 73: Event Sourcing and Functional Programming

State[S, A]S => (S, A)

.flatMap(A => State[S, B]): State[S,B]

Page 74: Event Sourcing and Functional Programming

object State {

def apply[S, A] (f: S => (S,A)): State[S, A] =

}

Page 75: Event Sourcing and Functional Programming

object State {

def apply[S, A] (f: S => (S,A)): State[S, A] =

new State[S, A] {

def run(s: S) = f(s)

}

}

Page 76: Event Sourcing and Functional Programming

object State {

def apply[S, A] (f: S => (S,A)): State[S, A] =

new State[S, A] {

def run(s: S) = f(s)

}

}

def check(id: String) =

Page 77: Event Sourcing and Functional Programming

object State {

def apply[S, A] (f: S => (S,A)): State[S, A] =

new State[S, A] {

def run(s: S) = f(s)

}

}

def check(id: String) =

(c: Cache) => (c, c.get(id))

Page 78: Event Sourcing and Functional Programming

object State {

def apply[S, A] (f: S => (S,A)): State[S, A] =

new State[S, A] {

def run(s: S) = f(s)

}

}

def check(id: String) = State[Cache, Option[User]].apply {

(c: Cache) => (c, c.get(id))

}

Page 79: Event Sourcing and Functional Programming

object State {

def apply[S, A] (f: S => (S,A)): State[S, A] =

new State[S, A] {

def run(s: S) = f(s(

}

}

def check(id: String) = State[Cache, Option[User]]{

(c: Cache) => (c, c.get(id))

}

Page 80: Event Sourcing and Functional Programming

trait State[S, +A] {

}

Page 81: Event Sourcing and Functional Programming

trait State[S, +A] {

def run(initial: S): (S, A)

}

Page 82: Event Sourcing and Functional Programming

trait State[S, +A] {

def run(initial: S): (S, A)

def map[B](f: A => B): State[S, B] =

}

}

Page 83: Event Sourcing and Functional Programming

trait State[S, +A] {

def run(initial: S): (S, A)

def map[B](f: A => B): State[S, B] =

State {

}

}

}

Page 84: Event Sourcing and Functional Programming

trait State[S, +A] {

def run(initial: S): (S, A)

def map[B](f: A => B): State[S, B] =

State { s0 =>

(_, _ )

}

}

}

Page 85: Event Sourcing and Functional Programming

trait State[S, +A] {

def run(initial: S): (S, A)

def map[B](f: A => B): State[S, B] =

State { s0 =>

(_, f(a))

}

}

}

Page 86: Event Sourcing and Functional Programming

trait State[S, +A] {

def run(initial: S): (S, A)

def map[B](f: A => B): State[S, B] =

State { s0 =>

val (s, a) = run(s0)

(_, f(a))

}

}

}

Page 87: Event Sourcing and Functional Programming

trait State[S, +A] {

def run(initial: S): (S, A)

def map[B](f: A => B): State[S, B] =

State { s0 =>

val (s, a) = run(s0)

(s, f(a))

}

}

}

Page 88: Event Sourcing and Functional Programming

trait State[S, +A] {

def run(initial: S): (S, A)

def map[B](f: A => B): State[S, B] =

State { s0 =>

val (s, a) = run(s0)

(s, f(a))

}

}

def flatMap[B](f: A => State[S,B]): State[S, B] =

}

}

Page 89: Event Sourcing and Functional Programming

trait State[S, +A] {

def run(initial: S): (S, A)

def map[B](f: A => B): State[S, B] =

State { s0 =>

val (s, a) = run(s0)

(s, f(a))

}

}

def flatMap[B](f: A => State[S,B]): State[S, B] =

f(a)

}

}

Page 90: Event Sourcing and Functional Programming

trait State[S, +A] {

def run(initial: S): (S, A)

def map[B](f: A => B): State[S, B] =

State { s0 =>

val (s, a) = run(s0)

(s, f(a))

}

}

def flatMap[B](f: A => State[S,B]): State[S, B] =

val (s, a) = run(s0)

f(a)

}

}

Page 91: Event Sourcing and Functional Programming

trait State[S, +A] {

def run(initial: S): (S, A)

def map[B](f: A => B): State[S, B] =

State { s0 =>

val (s, a) = run(s0)

(s, f(a))

}

}

def flatMap[B](f: A => State[S,B]): State[S, B] =

State { s0 =>

val (s, a) = run(s0)

f(a)

}

}

}

Page 92: Event Sourcing and Functional Programming

trait State[S, +A] {

def run(initial: S): (S, A)

def map[B](f: A => B): State[S, B] =

State { s0 =>

val (s, a) = run(s0)

(s, f(a))

}

}

def flatMap[B](f: A => State[S,B]): State[S, B] =

State { s0 =>

val (s, a) = run(s0)

f(a).run(s)

}

}

}

Page 93: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache {}

def check(id: Long)(cache: Cache): (Cache, Option[User]) = ???

def retrieve(id: Long)(cache: Cache): (Cache, User) = ???

def findUser(id: Long)(cache: Cache): (Cache, User) = {

val (c, mu) = check(id)(cache)

mu match {

case Some(u) => (c, u)

case None => retrieve(id)(c)

}

}

}

Page 94: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache {}

def check(id: Long)(cache: Cache): (Cache, Option[User]) = ???

def retrieve(id: Long)(cache: Cache): (Cache, User) = ???

def findUser(id: Long)(cache: Cache): (Cache, User) = {

val (c, mu) = check(id)(cache)

mu match {

case Some(u) => (c, u)

case None => retrieve(id)(c)

}

}

}

Page 95: Event Sourcing and Functional Programming

case class User(id: Long, fn: String, ln: String)

class Cache {}

def check(id: Long): State[Cache, Option[User]] = ???

def retrieve(id: Long): State[Cache, User] = ???

def findUser(id: Long): State[Cache, User] = {

for {

maybeUser <- check(id)

user <- maybeUser match {

case Some(u) => State { c => (c, u)}

case None => retrieve(id)

}

} yield (user)

}

Page 96: Event Sourcing and Functional Programming

Event Sourcing

Page 97: Event Sourcing and Functional Programming

Event Sourcing driven by business

Page 98: Event Sourcing and Functional Programming

Bloggers Conf App

Page 99: Event Sourcing and Functional Programming

Bloggers Conf App

● Can create an account

Page 100: Event Sourcing and Functional Programming

Bloggers Conf App

● Can create an account● List all bloggers already using the app

Page 101: Event Sourcing and Functional Programming

Bloggers Conf App

● Can create an account● List all bloggers already using the app● Mark/unmark other blogger as a friend

Page 102: Event Sourcing and Functional Programming

Bloggers Conf App

● Can create an account● List all bloggers already using the app● Mark/unmark other blogger as a friend● Mark/unmark other blogger as an enemy

Page 103: Event Sourcing and Functional Programming

Bloggers Conf App

● Can create an account● List all bloggers already using the app● Mark/unmark other blogger as a friend● Mark/unmark other blogger as an enemy● Deactivate its account

Page 104: Event Sourcing and Functional Programming

Bloggers Conf App

Bloggers

id first_name last_name active

1 Jan Kowalski T

2 Krystian Nowak T

3 Malgorzata Kucharska T

Page 105: Event Sourcing and Functional Programming

Bloggers Conf App

Bloggers

id first_name last_name active

1 Jan Kowalski T

2 Krystian Nowak T

3 Malgorzata Kucharska T

Friends

id friend_id

3 1

Page 106: Event Sourcing and Functional Programming

Bloggers Conf App

Bloggers

id first_name last_name active

1 Jan Kowalski T

2 Krystian Nowak T

3 Malgorzata Kucharska T

Friends

id friend_id

3 1

Enemies

id enemy_id

3 2

Page 107: Event Sourcing and Functional Programming

Bloggers Conf App

Bloggers

id first_name last_name active

1 Jan Kowalski T

2 Krystian Nowak T

3 Malgorzata Kucharska T

Friends

id friend_id

3 1

Enemies

id enemy_id

3 2

The Structure

Page 108: Event Sourcing and Functional Programming

Structure is not that important

Page 109: Event Sourcing and Functional Programming

Structure is not that important

Changes more often than behaviour

Page 110: Event Sourcing and Functional Programming

Start thinking about facts occurring

Page 111: Event Sourcing and Functional Programming

Start thinking about facts occurring

Derive structure from them

Page 112: Event Sourcing and Functional Programming

Blogger Account Created (id=3)

Page 113: Event Sourcing and Functional Programming

Blogger Account Created (id=3)

Befriended Blogger id=1

Page 114: Event Sourcing and Functional Programming

Blogger Account Created (id=3)

Befriended Blogger id=1

Made Enemy of Blogger id=2

Page 115: Event Sourcing and Functional Programming

Blogger Account Created (id=3)

Befriended Blogger id=1

Made Enemy of Blogger id=2

Bloggers

id first_name last_name active

1 Jan Kowalski T

2 Krystian Nowak T

Friends

id friend_id

Enemies

id enemy_id

Page 116: Event Sourcing and Functional Programming

Blogger Account Created (id=3)

Befriended Blogger id=1

Made Enemy of Blogger id=2

Bloggers

id first_name last_name active

1 Jan Kowalski T

2 Krystian Nowak T

3 Malgorzata Kucharska T

Friends

id friend_id

Enemies

id enemy_id

Page 117: Event Sourcing and Functional Programming

Blogger Account Created (id=3)

Befriended Blogger id=1

Made Enemy of Blogger id=2

Bloggers

id first_name last_name active

1 Jan Kowalski T

2 Krystian Nowak T

3 Malgorzata Kucharska T

Friends

id friend_id

3 1

Enemies

id enemy_id

Page 118: Event Sourcing and Functional Programming

Blogger Account Created (id=3)

Befriended Blogger id=1

Made Enemy of Blogger id=2

Bloggers

id first_name last_name active

1 Jan Kowalski T

2 Krystian Nowak T

3 Malgorzata Kucharska T

Friends

id friend_id

3 1

Enemies

id enemy_id

3 2

Page 119: Event Sourcing and Functional Programming

Blogger Account Created (id=3)

Befriended Blogger id=1

Made Enemy of Blogger id=2

Page 120: Event Sourcing and Functional Programming

Blogger Account Created (id=3)

Befriended Blogger id=1

Made Enemy of Blogger id=2

Befriended Blogger id=2

Unfriended Blogger id=2

Blogger Account Created (id=3)

Befriended Blogger id=1

Made Enemy of Blogger id=2

Page 121: Event Sourcing and Functional Programming

Event Sourcing driven by business

Page 122: Event Sourcing and Functional Programming

Event Sourcing The only model that does not lose data

Page 123: Event Sourcing and Functional Programming

“Enemy of my enemy is my friend”

Page 124: Event Sourcing and Functional Programming

Bloggers

id first_name last_name active

1 Jan Kowalski T

2 Krystian Nowak T

3 Malgorzata Kucharska T

Friends

id friend_id

3 1

Enemies

id enemy_id

3 2

Page 125: Event Sourcing and Functional Programming

Bloggers

id first_name last_name active

1 Jan Kowalski T

2 Krystian Nowak T

3 Malgorzata Kucharska T

4 Tomasz Młynarski T

Friends

id friend_id

3 1

Enemies

id enemy_id

3 2

4 2

Page 126: Event Sourcing and Functional Programming

Bloggers

id first_name last_name active

1 Jan Kowalski T

2 Krystian Nowak T

3 Malgorzata Kucharska T

4 Tomasz Młynarski T

5 Monika Jagoda T

Friends

id friend_id

3 1

Enemies

id enemy_id

3 2

4 2

2 5

Page 127: Event Sourcing and Functional Programming

id = 3

id = 2

id = 1

id = 4

id = 5

id = 6

Page 128: Event Sourcing and Functional Programming

id = 3

id = 2

id = 1

id = 4

id = 5

id = 6

Page 129: Event Sourcing and Functional Programming

id = 3

id = 2

id = 1

id = 4

id = 5

id = 6

Page 130: Event Sourcing and Functional Programming

id = 3

id = 2

id = 1

id = 4

id = 5

id = 6

Page 131: Event Sourcing and Functional Programming

id = 3

id = 2

id = 1

id = 4

id = 5

id = 6

Page 132: Event Sourcing and Functional Programming

id = 3

id = 2

id = 1

id = 4

id = 5

id = 6

Page 133: Event Sourcing and Functional Programming

id = 3

id = 2

id = 1

id = 4

id = 5

id = 6

Page 134: Event Sourcing and Functional Programming

id = 3

id = 2

id = 1

id = 4

id = 5

id = 6

Page 135: Event Sourcing and Functional Programming

Benefits of Event Sourcing

Page 136: Event Sourcing and Functional Programming

Benefits of Event Sourcing● Ability to go in time and figure out exactly what have

happened

Page 137: Event Sourcing and Functional Programming

Benefits of Event Sourcing● Ability to go in time and figure out exactly what have

happened● Scientific measurements over time, compare time

periods

Page 138: Event Sourcing and Functional Programming

Benefits of Event Sourcing● Ability to go in time and figure out exactly what have

happened● Scientific measurements over time, compare time

periods● Built-in audit log

Page 139: Event Sourcing and Functional Programming

Benefits of Event Sourcing● Ability to go in time and figure out exactly what have

happened● Scientific measurements over time, compare time

periods● Built-in audit log● Enables temporal querying

Page 140: Event Sourcing and Functional Programming

Benefits of Event Sourcing● Ability to go in time and figure out exactly what have

happened● Scientific measurements over time, compare time

periods● Built-in audit log● Enables temporal querying● Fits well with machine learning

Page 141: Event Sourcing and Functional Programming

Benefits of Event Sourcing● Ability to go in time and figure out exactly what have

happened● Scientific measurements over time, compare time

periods● Built-in audit log● Enables temporal querying● Fits well with machine learning● Preserves history - question not yet asked

Page 142: Event Sourcing and Functional Programming

Benefits of Event Sourcing● Ability to go in time and figure out exactly what have

happened● Scientific measurements over time, compare time

periods● Built-in audit log● Enables temporal querying● Fits well with machine learning● Preserves history - question not yet asked● Writing regression tests is easy

Page 143: Event Sourcing and Functional Programming

Benefits of Event Sourcing● Ability to go in time and figure out exactly what have

happened● Scientific measurements over time, compare time

periods● Built-in audit log● Enables temporal querying● Fits well with machine learning● Preserves history - question not yet asked● Writing regression tests is easy● Polyglot data

Page 144: Event Sourcing and Functional Programming

Drawbacks of Event Sourcing

Page 145: Event Sourcing and Functional Programming

Drawbacks of Event Sourcing● Historical record of your bad decisions

Page 146: Event Sourcing and Functional Programming

Drawbacks of Event Sourcing● Historical record of your bad decisions● Handling event duplicates

Page 147: Event Sourcing and Functional Programming

Drawbacks of Event Sourcing● Historical record of your bad decisions● Handling event duplicates● Data eventually consistent

Page 148: Event Sourcing and Functional Programming

How to implement it?

Page 149: Event Sourcing and Functional Programming

Events vs Commands

Page 150: Event Sourcing and Functional Programming

Journal

Page 151: Event Sourcing and Functional Programming

Journal

Page 152: Event Sourcing and Functional Programming

Journal

val id = “”val firstName: String = “”val lastName: String = “”val friends: List[String] = List()

Page 153: Event Sourcing and Functional Programming

Journal

val id = “”val firstName: String = “”val lastName: String = “”val friends: List[String] = List()

Page 154: Event Sourcing and Functional Programming

Journal

val id = “”val firstName: String = “”val lastName: String = “”val friends: List[String] = List()

Page 155: Event Sourcing and Functional Programming

Journal

val id = “”val firstName: String = “”val lastName: String = “”val friends: List[String] = List()

Initialized(“1”, “Jan”, “Kowalski”

Page 156: Event Sourcing and Functional Programming

Journal

val id = “”val firstName: String = “”val lastName: String = “”val friends: List[String] = List()

Initialized(“1”, “Jan”, “Kowalski”

Page 157: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List()

Page 158: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List()

Page 159: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List()

Page 160: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List()

Befriended(“10”)

Page 161: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List()

Befriended(“10”)

Page 162: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”)

Page 163: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”)

Page 164: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”)

Page 165: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”)

Page 166: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Page 167: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Page 168: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Befriend(“31”)

Page 169: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Befriend(“31”)

Page 170: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Befriend(“31”)

validation

Page 171: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Befriend(“31”)

validation

Page 172: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Page 173: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Befriend(“34”)

Page 174: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Befriend(“34”)

Page 175: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Befriend(“34”)

validation

Page 176: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Befriend(“34”)

validation

Page 177: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Befriend(“34”)

Befriended(“34”)

Page 178: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Befriend(“34”)

Befriended(“34”)

Page 179: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Befriend(“34”)

Befriended(“34”)

Page 180: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Befriend(“34”)

Befriended(“34”)

Page 181: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Befriend(“34”)

Page 182: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”)

Befriend(“34”)

ACK

Page 183: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”, “34”)

Befriend(“34”)

Page 184: Event Sourcing and Functional Programming

Journal

val id = “1”val firstName: String = “Jan”val lastName: String = “Kowalski”val friends: List[String] = List(“10”, “31”, “34”)

Page 185: Event Sourcing and Functional Programming

Let’s see some code!https://github.

com/rabbitonweb/es_cqrs_example

Page 186: Event Sourcing and Functional Programming

What we are missing?

Page 187: Event Sourcing and Functional Programming

What we are missing?

1. Read-model

Page 188: Event Sourcing and Functional Programming

What we are missing?

1. Read-model2. Validation

Page 189: Event Sourcing and Functional Programming

And that’s all folks!

Page 190: Event Sourcing and Functional Programming

Paweł Szulc

Page 191: Event Sourcing and Functional Programming

Paweł Szulchttp://rabbitonweb.com

Page 192: Event Sourcing and Functional Programming

Paweł Szulchttp://rabbitonweb.com

@rabbitonweb

Page 193: Event Sourcing and Functional Programming

Paweł Szulchttp://rabbitonweb.com

@rabbitonwebhttps://github.com/rabbitonweb/

Page 194: Event Sourcing and Functional Programming

Paweł Szulchttp://rabbitonweb.com

@rabbitonwebhttps://github.com/rabbitonweb/http://rabbitonweb.com/spark

Page 195: Event Sourcing and Functional Programming

Thank you!


Recommended