Sie sind auf Seite 1von 102

Event sourcing and CQRS

from the trenches

S I D NEY SHEK ARC H IT ECT AT L ASS IA N @ S ID NEY S HEK

Universe of Users
users
Id

Name

Username

APIKey

Homer

homer

d0a

Bart

bart

f00

Maggie

maggie

baa

Universe of Users
users
Id

Name

Username

APIKey

Homer

homer

d0a

Bart

bart

f00

Lisa Jr

maggie

baa

Universe of Users
users
Id

Name

Username

APIKey

Homer

homers

d0a

Bart

bart

f00

Lisa Jr

maggie

baa

Universe of Users
users
Id

Name

Username

APIKey

Homer

homer

d0a

Bart

bart

f00

Maggie

maggie

baa

events
Seq

Event

Time

123

SetUsername(3, Maggie)

Universe of Users
users
Id

Name

Username

APIKey

Homer

homer

d0a

Bart

bart

f00

Lisa Jr

maggie

baa

events
Seq

Event

Time

123

SetUsername(3, Maggie)

124

SetName(3, Lisa Jr)

10

Universe of Users
users
Id

Name

Username

APIKey

Homer

homers

d0a

Bart

bart

f00

Lisa Jr

maggie

baa

events
Seq

Event

Time

123

SetUsername(3, Maggie)

124

SetName(3, Lisa Jr)

10

125

SetUsername(1, homers)

15

Universe of Users
users
Id

Name

Username

APIKey

Homer

homers

d0a

Bart

bart

f00

Lisa Jr

maggie

baa

users_new
Id

Name

Derived

Homer

Homer1

Bart

Bart2

Lisa Jr

Lisa Jr3

events
Seq

Event

Time

123

SetUsername(3, Maggie)

124

SetName(3, Lisa Jr)

10

125

SetUsername(1, homers)

15

Universe of Users
users
Id

Name

Username

APIKey

Homer

homers

d0a

Bart

bart

f00

Lisa Jr

maggie

baa

users_new
Id

Name

Derived

Homer

Homer1

Bart

Bart2

Lisa Jr

Lisa Jr3

events
Seq

Event

Time

123

SetUsername(3, Maggie)

124

SetName(3, Lisa Jr)

10

125

SetUsername(1, homers)

15

Our Identity System requirements

Our Identity System requirements


Users, groups and memberships

Our Identity System requirements


Users, groups and memberships
Searching for users

Our Identity System requirements


Users, groups and memberships
Searching for users
Retrieve by email

Our Identity System requirements


Users, groups and memberships
Searching for users
Retrieve by email

High volume low latency reads

Our Identity System requirements


Users, groups and memberships
Searching for users
Retrieve by email
Incremental synchronisation

High volume low latency reads

Our Identity System requirements


Users, groups and memberships
Searching for users
Retrieve by email
Incremental synchronisation

Audit trails for changes

High volume low latency reads

Our Identity System requirements


Users, groups and memberships

High volume low latency reads

Searching for users


Retrieve by email
Incremental synchronisation

Highly available
Disaster recovery
Zero-downtime upgrades

Audit trails for changes

Our Identity System requirements


Users, groups and memberships

High volume low latency reads

Searching for users


Retrieve by email
Incremental synchronisation

Highly available
Disaster recovery
Zero-downtime upgrades

Audit trails for changes


Testing with production-like data

Evolving the
architecture

REST calls
e.g. Add User

Core app
Services

SaveAPI

Query
Query
Query
views
views
views

EventStream

DynamoDB

REST calls
e.g. Add User

Core app
Services

SaveAPI

Query
Query
Query
views
views
views

EventStream

DynamoDB

REST calls
e.g. Add User

Core app
Services

SaveAPI

Query
Query
Query
views
views
views

EventStream

DynamoDB

REST calls
e.g. Add User

Core app
Services

SaveAPI

Query
Query
Query
views
views
views

EventStream

DynamoDB

REST calls
e.g. Add User

Core app
Services

SaveAPI

Query
Query
Query
views
views
views

EventStream

DynamoDB

REST calls
e.g. Add User

Core app
Services

SaveAPI

Query
Query
Query
views
views
views

EventStream

DynamoDB

REST calls
e.g. Add User

Core app
Services

SaveAPI

Query
Query
Query
views
views
views

EventStream

DynamoDB

REST calls
e.g. Add User

Core app
Services

SaveAPI

Query
Query
Query
views
views
views

EventStream

DynamoDB

REST calls
e.g. Add User

Core app
Services

Commands

EventStream
EventStream
EventStream

DynamoDB

Query
Query
Query
views
views
views

REST calls
e.g. Add User

Core app
Services

Commands

EventStream
EventStream
EventStream

DynamoDB

Query
Query
Query
views
views
views

REST calls
e.g. Add User

Core app
Services

Commands

Query
sync

EventStream
EventStream
EventStream

DynamoDB

Query
Query
Query
views
views
views

REST calls
e.g. Add User

Core app
Services

Commands

Query
sync

EventStream
EventStream
EventStream

DynamoDB

Query
Query
Query
views
views
views

REST calls
e.g. Add User

Core app
Services

Commands

Query
sync

EventStream
EventStream
EventStream

DynamoDB

Query
Query
Query
views
views
views

REST calls
e.g. Add User

Core app
Services

Commands

Query
sync

EventStream
EventStream
EventStream

DynamoDB

Query
Query
Query
views
views
views

REST calls
e.g. Add User

Core app
Services

Commands

Query
sync

EventStream
EventStream
EventStream

DynamoDB

Query
Query
Query
views
views
views

REST calls
e.g. Add User

Core app
Services

Commands

Query
sync

Query
Query
Query
views
views
views

EventStream
EventStream
EventStream

Events
DynamoDB

Lambda

Kinesis

REST calls
e.g. Add User

Core app
Services

Commands

Query
sync

Query
Query
Query
views
views
views

EventStream
EventStream
EventStream

ElasticSearch

Events
DynamoDB

Lambda

Kinesis

REST calls
e.g. Add User

Core app
Services

Commands

Query
sync

Query
Query
Query
views
views
views

EventStream
EventStream
EventStream

ElasticSearch

Events
DynamoDB

Lambda

Kinesis

REST calls
e.g. Add User

Groups
Services

Commands

Query
sync

Query
Query
Query
views
views
views

EventStream
EventStream
EventStream

Platform
Events
DynamoDB

Lambda

Kinesis

REST calls
e.g. Add User

Groups
Services

Commands

Query
sync

Query
Query
Query
views
views
views

EventStream
EventStream
EventStream

Platform
Events

Users
DynamoDB

Lambda

Kinesis

REST calls
e.g. Add User

Groups
Services

Commands

Query
sync

Query
Query
Query
views
views
views

EventStream
EventStream
EventStream
External
Events
Platform
Events

Users
DynamoDB

Lambda

Kinesis

Event
Txf

Kinesis

Delving into
code

Core app stack


Web

Services

Command
handlers

Query
handlers

Core app stack


Web

Services

Command
handlers
command.Op ~>
Result

Query
handlers
query.Op ~>
Result

case class Save[A](...)


extends command.Op[A]
case class UserByName[A](...)
extends query.Op[A]

Core app stack


Web

def addUser: AppOp[A]

Services

Command
handlers
command.Op ~>
Result

Query
handlers
query.Op ~>
Result

type AppOp[A] = Free[App, A]


type App[A] =
Coproduct[command.Op, query.Op, A]
case class Save[A](...)
extends command.Op[A]
case class UserByName[A](...)
extends query.Op[A]

Core app stack


post { entity(as[NewUser]) { u =>
runApi(UserService.addUser(u))
}

Web

val runApi: AppOp[A] => Route


def addUser: AppOp[A]

Services

Command
handlers
command.Op ~>
Result

Query
handlers
query.Op ~>
Result

type AppOp[A] = Free[App, A]


type App[A] =
Coproduct[command.Op, query.Op, A]
case class Save[A](...)
extends command.Op[A]
case class UserByName[A](...)
extends query.Op[A]

Commands save events to streams


object UserCommandHandler {
def apply(streams: Streams): command.Op ~> Result =
new (command.Op ~> Result) {
def apply[X](a: command.Op[X]): Result[X] =
a match {
case Save(u) =>

case ...

Commands save events to streams


object UserCommandHandler {
def apply(streams: Streams): command.Op ~> Result =
new (command.Op ~> Result) {
def apply[X](a: command.Op[X]): Result[X] =
a match {
case Save(u) =>
streams.allUsers.save(SingleStreamId, SetName(u.id, u.name))
.then(streams.userProfile.save(u.id, SetProfile(u))
.thenReturn(u.id)
case ...
}
}
}

Query synchroniser is a scalaz-stream Channel


class QuerySynchroniser(db: DB) {
def synchronise(s: Process[Task, StreamEvent]) =

Query synchroniser is a scalaz-stream Channel


class QuerySynchroniser(db: DB) {
def synchronise(s: Process[Task, StreamEvent]) =
(s through writeEvent).run
private val writeEvent: Channel[Task, StreamEvent, Unit] =
Process.constant { e: StreamEvent =>

Query synchroniser is a scalaz-stream Channel


class QuerySynchroniser(db: DB) {
def synchronise(s: Process[Task, StreamEvent]) =
(s through writeEvent).run
private val writeEvent: Channel[Task, StreamEvent, Unit] =
Process.constant { e: StreamEvent =>
for {
r1 <- StreamState.transitionSeq(db, e.id.key, e.id.seq)

} yield ()

Query synchroniser is a scalaz-stream Channel


class QuerySynchroniser(db: DB) {
def synchronise(s: Process[Task, StreamEvent]) =
(s through writeEvent).run

private val writeEvent: Channel[Task, StreamEvent, Unit] =


Process.constant { e: StreamEvent =>
for {
r1 <- StreamState.transitionSeq(db, e.id.key, e.id.seq)
_ <- if (r1.success) db.process(e) else db.rollback
} yield ()
}

that you pass it an event stream


class KinesisProcessor(qs: QuerySynchroniser) extends IRecordProcessor {
override def processRecords(rs: ProcessRecordInput) = {

}
}

that you pass it an event stream


class KinesisProcessor(qs: QuerySynchroniser) extends IRecordProcessor {
override def processRecords(rs: ProcessRecordInput) = {
// Either emit events from Kinesis, or read events from Dynamo
val stream = Process.emitAll { rs.getRecords. ... }
}
}

that you pass it an event stream


class KinesisProcessor(qs: QuerySynchroniser) extends IRecordProcessor {
override def processRecords(rs: ProcessRecordInput) = {
// Either emit events from Kinesis, or read events from Dynamo
val stream = Process.emitAll { rs.getRecords. ... }
}
}

qs.synchronise(stream).attemptRunFor(...)

Events as an API

Insert / Update Delta

vs

Set events

Insert / Update Delta


AddUser(id, name, email1)

vs

Set events

Insert / Update Delta


AddUser(id, name, email1)
UpdateUser(id, email = Some(email))

vs

Set events

Insert / Update Delta


AddUser(id, name, email1)
UpdateUser(id, email = Some(email))

vs

Set events
SetUserName(id, name)
SetUserEmail(id, email1)

Insert / Update Delta


AddUser(id, name, email1)
UpdateUser(id, email = Some(email))

vs

Set events
SetUserName(id, name)
SetUserEmail(id, email1)
SetUserEmail(id, email2)

Insert / Update Delta


AddUser(id, name, email1)
UpdateUser(id, email = Some(email))

vs

Set events
SetUserName(id, name)
SetUserEmail(id, email1)
SetUserEmail(id, email2)

Fits nicely with CRUD + PATCH

Insert / Update Delta


AddUser(id, name, email1)
UpdateUser(id, email = Some(email))

vs

Set events
SetUserName(id, name)
SetUserEmail(id, email1)
SetUserEmail(id, email2)

Fits nicely with CRUD + PATCH


Assume insert before update

Insert / Update Delta


AddUser(id, name, email1)
UpdateUser(id, email = Some(email))

vs

Set events
SetUserName(id, name)
SetUserEmail(id, email1)
SetUserEmail(id, email2)

Fits nicely with CRUD + PATCH


Assume insert before update

Encourages idempotent processing

Insert / Update Delta


AddUser(id, name, email1)
UpdateUser(id, email = Some(email))

vs

Set events
SetUserName(id, name)
SetUserEmail(id, email1)
SetUserEmail(id, email2)

Fits nicely with CRUD + PATCH

Encourages idempotent processing

Assume insert before update

Single code path for query sync

Insert / Update Delta


AddUser(id, name, email1)
UpdateUser(id, email = Some(email))

vs

Set events
SetUserName(id, name)
SetUserEmail(id, email1)
SetUserEmail(id, email2)

Fits nicely with CRUD + PATCH

Encourages idempotent processing

Assume insert before update

Single code path for query sync


Minimally sized events to avoid conflict

Insert / Update Delta


AddUser(id, name, email1)
UpdateUser(id, email = Some(email))

vs

Set events
SetUserName(id, name)
SetUserEmail(id, email1)
SetUserEmail(id, email2)

Fits nicely with CRUD + PATCH

Encourages idempotent processing

Assume insert before update

Single code path for query sync


Minimally sized events to avoid conflict

Single stream

Multiple streams

Single stream
Transactions and
consistent data
resolution

Multiple streams

Single stream

Multiple streams

Transactions and
consistent data
resolution

Sharding for
throughput

Rules for splitting streams


1. Place independent events on different streams

Rules for splitting streams


1. Place independent events on different streams
2. Split streams by event type and unique Id

Rules for splitting streams


1. Place independent events on different streams
2. Split streams by event type and unique Id
3. Identify the transactions you really need

Rules for splitting streams


1. Place independent events on different streams
2. Split streams by event type and unique Id
3. Identify the transactions you really need
4. Use hierarchical streams to maximise number of streams

Rules for splitting streams


1. Place independent events on different streams
2. Split streams by event type and unique Id
3. Identify the transactions you really need
4. Use hierarchical streams to maximise number of streams
5. Splitting and joining streams later is possible

Let go of
transactions
and consistency

Query views get populated eventually

Query views get populated eventually


No guaranteed order between events on different streams

Query views get populated eventually


No guaranteed order between events on different streams
A field should only be updated by a single event stream

Query views get populated eventually


No guaranteed order between events on different streams
A field should only be updated by a single event stream
No foreign key constraints

Query views get populated eventually


No guaranteed order between events on different streams
A field should only be updated by a single event stream
No foreign key constraints
Unique or data constraints enforced on write

Tokens to emulate transactions and consistency


User: homer (id 4)
All Users: Seq 100
User 4: Seq 23

Tokens to emulate transactions and consistency


User: homer (id 4)
All Users: Seq 100
User 4: Seq 23

Returned on read and write via ETag

Tokens to emulate transactions and consistency


User: homer (id 4)

Returned on read and write via ETag

All Users: Seq 100


User 4: Seq 23

Pass as request header for:

Tokens to emulate transactions and consistency


User: homer (id 4)

Returned on read and write via ETag

All Users: Seq 100


User 4: Seq 23

Pass as request header for:


Condition write (transaction)

Tokens to emulate transactions and consistency


User: homer (id 4)

Returned on read and write via ETag

All Users: Seq 100


User 4: Seq 23

Pass as request header for:


Condition write (transaction)
Force query view update (consistency)

Tokens to emulate transactions and consistency


User: homer (id 4)

Returned on read and write via ETag

All Users: Seq 100


User 4: Seq 23

Pass as request header for:


Condition write (transaction)
Force query view update (consistency)
Caching

Using tokens to enforce state


Client

Core App

Using tokens to enforce state


Core App

Client
Create User A

Using tokens to enforce state


Core App

Client
Create User A
Token TU1

Using tokens to enforce state


Core App

Client
Create User A
Token TU1
Create Group B

Using tokens to enforce state


Core App

Client
Create User A
Token TU1
Create Group B
Token TG1

Using tokens to enforce state


Core App

Client
Create User A
Token TU1
Create Group B
Token TG1
Add User A to Group B (tokens TU1, TG1)

Using tokens to enforce state


Core App

Client
Create User A
Token TU1
Create Group B
Token TG1
Add User A to Group B (tokens TU1, TG1)

Force query view


to TU1, TG1

Using tokens to enforce state


Core App

Client
Create User A
Token TU1
Create Group B
Token TG1
Add User A to Group B (tokens TU1, TG1)

Force query view


to TU1, TG1
Token TG2

Key takeaways

Key takeaways
Start small and challenge everything!

Key takeaways
Start small and challenge everything!
Incremental architecture for incremental demos

Key takeaways
Start small and challenge everything!
Incremental architecture for incremental demos
Think Events as an API

Key takeaways
Start small and challenge everything!
Incremental architecture for incremental demos
Think Events as an API
Accept weaker transactions and eventual consistency

We should using
event sourcing more
than we do
Martin Fowler (very loosely paraphrased)

event sourcing lib:

bitbucket.org/atlassianlabs/eventsrc